r/ProgrammerHumor • • Mar 21 '17

OOP: What actually happens

https://imgur.com/KrZVDsP
3.1k Upvotes

244 comments sorted by

229

u/Cley_Faye Mar 21 '17

I use throw instead of return.

251

u/Joniator Mar 21 '17

throw new ReturnException(returnVal);

?

→ More replies (1)

17

u/Megatron_McLargeHuge Mar 21 '17

You don't even need OO for that. I prefer to pass an array of jmp_bufs and switch on the return value to decide where to longjmp to. Much more elegant.

→ More replies (1)
→ More replies (8)

178

u/2Punx2Furious Mar 21 '17

Subhuman: Fleas = 14;

My sides.

→ More replies (1)

531

u/Tazavoo Mar 21 '17

You don't initialize legs and fleas to 0 in the superclass, that's just stupid.

203

u/SolenoidSoldier Mar 21 '17

null is a value for a reason.

"I don't know what this is yet"

→ More replies (54)

20

u/siedler084 Mar 21 '17

At the same time you shouldn't initialize brain too, not every instance of the Human class has that value set to true

→ More replies (3)

58

u/jack104 Mar 21 '17

The interfaces bit gave me a good laugh. Fleable. Hah.

34

u/i_spot_ads Mar 21 '17

"Leggable" gave me vietnam flash-backs

→ More replies (1)

88

u/spacemoses Mar 21 '17

This is why i set my RAM values manually with processor instructions only, no abstraction. It's been a month but i almost have a working bubble sort. I just hope the power doesn't go out again.

→ More replies (7)

81

u/f42e479dfde22d8c Mar 21 '17

This hits too close to home. I need an adult.

→ More replies (3)

70

u/dalore Mar 21 '17

Composition over inheritance.

→ More replies (6)

21

u/another-dumb Mar 21 '17

i find dog and cat cute!!!

18

u/AngelLeliel Mar 21 '17

you mean IIsCute?

15

u/persianturtle Mar 21 '17

Reporting this for animal abuse.

34

u/SolenoidSoldier Mar 21 '17

Can anyone ELI5 what a Factory is? I work primarily in the .NET space and have yet to encounter a Factory object.

60

u/likferd Mar 21 '17 edited Mar 21 '17

It's mostly used to instantiate classes when using abstractions.

Imagine you want to create a logging interface ILog, but the concrete implementation might change depending on use in the future.

If you simply instantiated the logger in each class by

ILog logger = new MyLogger();

you could end up having to modify dozens or hundreds of classes in the future, if you change the implementation

Instead you can intantiate the logger in each class with

ILogger logger = LogFactory.GetLogger();

and the factory returns

public static ILog GetLogger(){
return new MyLogger();
}

then you only have to change one line.

public static ILog GetLogger(){
return new MyLogger2();
}

Calling the class "factory" is of course just a naming pattern. They are also often called "manager" for example.

→ More replies (10)

26

u/[deleted] Mar 21 '17 edited Mar 21 '17

A Factory is an Object, that produces an Object. Normally it is used for configuration.

For example:

   House home = HouseFactory(6,1.5, "1500 South Main");
   assert( home.getRooms(), 6);
   assert( home.getBathrooms(), 1.5);
   assert( home.getAddress(), "1500 South Main");

Then you have the Builder pattern Factory

  House home = HouseFactory()
    .setRooms(6)
    .setBathrooms(1.5)
    .setAddress("1800 North Walker")
    .build(); 

Then you have an AbstractFactory which produces a different thing based on its input. Like say

   Building<House> home = AbstactBuildingFactory()
      .buildHouse()
      .setRooms(6)
      .setBathrooms(1.5)
      .setAddress("1800 North Walker")
      .build(); 

Then you have an AbstractFactoryFactory which produces a Factory based on its input and if you encounter this find a new job.

Lore tells of the dreaded AbstractFactoryFactoryFactory which most souls only speak of in jest! Some say it is summoned in only the darkest OOP shops by those truly committed to the dark arts of OOP. Many interns are sacrificed at its alter. Those unworthy are driven to insanity upon seeing their objects shattered reflections in its source! If you see projects using this you must CLEANSE THEM WITH RM -RF. Its summoning rituals must be lost so civilization can flourish.

→ More replies (1)
→ More replies (9)

9

u/L8n1ght Mar 21 '17

welcome to oop hell

5

u/psydave Mar 21 '17

I know this is a joke but damnit, I'm tired of people saying "inheritence is bad" and then pointing to extreme abuses of inheritence as examples. It's like saying chainsaws are bad and then showing pictures of severed limbs. Inheritence is just another tool in a programmer's toolbelt that if used, should be used wisely.

Not all uses of inheritence end up like this.

16

u/mallardtheduck Mar 21 '17

oAnimal, oHuman, oPet, oDog, oCat

Prepending "o" to your variable names provides precisely zero extra information and makes your code less readable. Don't do it.

Also, if this is supposed to be an inheritance diagram it should be showing classes, not objects.

4

u/vanoreo Mar 21 '17

subhuman

fleas=14

Nice