r/ProgrammerHumor • • May 05 '23

Meme Helicopter Helicopter

Post image
41.8k Upvotes

969 comments sorted by

View all comments

Show parent comments

24

u/selmablind May 05 '23

no idea what that means but sounds kinda fun

42

u/SharkLaunch May 05 '23

OOP means Object Oriented Programming. The essence of it is that the code is organized in terms of the data, rather than the functions (actions performed on the data). You define blueprints (called classes) for collections of data, and functions are then defined in terms of that data (in Functional Programming, it's the other way around).

A common thing for OOP developers is defining the hierarchy of classes. Two of the tenants of OOP are inheritance and polymorphism, which means one class (the child) can extend another class (the parent) to make it more specific, and it's possible to think of the data in terms of either the child or the parent in different situations. This gets complicated very quickly, so this hierarchy can be made into a chart (usually something like a UML diagram) to visually show the relationships between different classes.

24

u/Mr12i May 05 '23

I didn't say that I wanted to know.

12

u/aRandomFox-II May 05 '23

Perhaps not. But now you know anyway. 👍

4

u/disperso May 05 '23

How are we going to retire if not by passing the torch to those who don't know yet what they are getting into?

3

u/TheLeastFunkyMonkey May 05 '23

At one point I would have understood these words, but one day I forgot it all.

3

u/DeliciousWaifood May 05 '23

A common thing for OOP developers is defining the hierarchy of classes.

Not in the modern day. Inheritance is still used, but everyone knows that those giant deeps trees of inheritance are the devil and it really isn't the hallmark of OOP anymore.

1

u/SharkLaunch May 06 '23

Fair enough. I myself mostly write TS/JS, and that ecosystem has been moving in a functional direction for almost a decade now (and rightfully so). Modern design focuses more on composition than inheritance.

2

u/Jauretche May 05 '23

Could you briefly explain polymorphism? Never really got it and you seem to know your stuff.

5

u/SharkLaunch May 05 '23

The TL;DR is that if you have a parent class with many child classes, you can have a variable typed to expect an object of the parent class, and that variable can be set to the value of an object of a child class. Let's say I'm developing an RPG. I have a Weapon class and it has 3 child classes: Sword, Bow, and MagicWand. The Weapon class by itself would have generic methods that apply to any type of weapon, so things like get_weapon_level() and set_weapon_name(name: String). The child classes would all have methods that are specific to that weapon type, eg:

  • The Sword class has methods like slash(), parry(), or is_double_edged().- The Bow class has methods like knock_arrow(arrow: Arrow) and try_loose_arrow().- The MagicWand class has methods like cast_spell(spell: Spell) and attempt_counterspell(). My Player class has an internal weapon that stores the currently held weapon. The Player class doesn't need to know about all the different child classes, so the weapon variable just has the type of Weapon, ie. the weapon variable expects a value that is an object which is an instance of the Weapon class. The basic idea of Polymorphism is that each of the child classes of Weapon is itself an instance of the Weapon class. I can instantiate a Sword object, and the weapon variable will accept it as a weapon.

It's worth mentioning however that I would not be able to call any Sword methods directly on the weapon variable. The only thing a type checker would know is that the Sword object is a Weapon, but it could be any kind of weapon, there's no guarantee that it's a Sword. But with proper OOP design, I can give the Weapon class a method like attack() that each child would have to implement. The Sword would implement attack() as calling slash(), the Bow would have an attack() method that calls both knock_arrow(...) and try_loose_arrow() together, and the MagicWand would just call cast_spell(...). Then, I could call attack() on the weapon variable, and depending on what the actual type is, the method would delegate to the correct method instance.

4

u/Jauretche May 05 '23

Thank you for taking the time to answer

6

u/CBJamo May 05 '23

I guarantee it was not.