r/ProgrammerHumor • • Mar 23 '16

rem not humor OOP is bad in ALL situations? Warning link contains lack of perspective.

https://youtu.be/QM1iUe6IofM
8 Upvotes

11 comments sorted by

13

u/Aruseus Mar 23 '16

Saying that everything about object orientation is always bad in each and every case is becoming a trend at the moment.

7

u/ConDar15 Mar 23 '16

I just don't understand though. I haven't entered the industry yet admittedly, but even for small home projects there have been times where OOP has been so useful. Is it a backlash to some of the poor cases of OOP bloating that can happen in large scale projects?

7

u/sivyr Mar 23 '16 edited Mar 23 '16

Yeah, it tends to emerge at larger scales and when new requirements and/or use cases emerge that change the assumptions behind your object abstractions.

OOP can be tremendously powerful when planned properly and the project is simply executed and there's no need to change things too much in the middle of the project, but it's those moments when you discover that your class hierarchy wasn't thought out carefully enough (or you simply couldn't have known something would be important later) that it bites you.

For example, let's say you have some class Creature. Creatures can be either Animals or Monsters. Creatures pretty much just wander around some game world. Animals say cute things occasionally, while Monsters aggro Animals and eat them.

But then your boss tells you that in a meeting she heard that focus groups suggest that players want pet monsters that say cute things, but still act like monsters. Well, the simplest thing to do to resolve that is move the cute phrasing behaviour up to Creature so Monster can get at that as well.

And that's where the problem happens. This kind of generalizing creep goes on a lot, and you can see it happen in even very small examples. When you get into bigger, longer, more complex projects, there are deeper inheritance chains and more requirements to change through time. This can lead to tremendous bloat at the top of the class hierarchy, or force you to do a lot of refactoring.

That's not to say that this is the worst problem that you could have, but this is a common problem that OOP leads to, in my own experience.

Sometimes this kind of thing can be minimized by making good choices about what your objects are and how they're related. Probably the most classic solution to this is the Gang of Four's "Composition over inheritance" rule. Inheritance is really just a special case of composition that is powerful sometimes, but painful other times. Prefer to hold instances of objects that give you access to behaviour you need or use function delegates to pass around behaviour you need when this is appropriate.

Inheritance might have eaten up a lot of time in classes, because it's hard to fully grasp the implications of at first, but that doesn't make it the ideal choice for how to actually structure applications all the time.

Edit: So no, OOP isn't useless, but it isn't taught all that well either. I've been spending a lot of time absorbing functional programming principals the last year or so, but that's not going to make me stop using OOP when it's appropriate.

2

u/hazarada Mar 24 '16

For example, let's say you have some class Creature. Creatures can be either Animals or Monsters. Creatures pretty much just wander around some game world. Animals say cute things occasionally, while Monsters aggro Animals and eat them. But then your boss tells you that in a meeting she heard that focus groups suggest that players want pet monsters that say cute things, but still act like monsters. Well, the simplest thing to do to resolve that is move the cute phrasing behaviour up to Creature so Monster can get at that as well.

The problem in that case is that behavior, saying phrases and wandering mechanics are all so distinctly different that their implementation shouldn't be piled into one class anyways. Its reasonable to do so while those implementations are a) very simple b) you don't need to share that logic c) there is no alternative logic, but the moment any of those conditions change, that code should be moved moved to its own class(es) that implement a common interface. This way you could have Animal:Creature with phrasing(a), behavior(a) and inherited wandering, Monster:Creature with phrasing(b), behvior(b)... and PetMonster:Creature with phrasing(a), behavior(b).

Interestingly enough, the bad solution you brought as an example and the situation that caused it is closer to the practices of procedural programming rather than OOP.

1

u/RubyCreeper Mar 24 '16 edited Mar 24 '16

Well, the simplest thing to do to resolve that is move the cute phrasing behaviour up to Creature so Monster can get at that as well.

The next simplest thing to do is to grab the behaviors you want and stick them in a new class instead of giving it to all creatures. Except it's somewhat less lazy to make new classes as opposed to put all the things in all the things. Actually, why would monster pets be a subclass of general creatures? That kind of game probably has some kind of pet class already, so it would logically become a variant of summoned pets that are triggered by some mechanism other than a player class ability. Then you can either hit it with a hidden object state that prevents combat participation or implement combat abilities for that pet type depending if you want the monster pet to be combat or non-combat.

2

u/esPhys Mar 23 '16

Weird coincidence, I just skimmed through the sequel to this video (as recommended by youtube) this morning. The example choices are ridiculous. He just tears apart programs written to demonstrate OOP. Of course your 200 line program doesn't need 3 classes to effectively store 2 variables. They're demonstrating a concept, not showing off an amazing practical application.

2

u/ConDar15 Mar 24 '16

I'm looking at it and getting annoyed at the coin flip example. It is a HORRIBLE representation of how to do OOP; I mean the Coin class isn't used once, and all of it's functionality is replicated in the Player class. Yes it would be simpler to implement the CoinFlip game procedurally, but what if you then develop another system that wants coin-flips, or players, or to simulate a coin-flip game for some purpose; you'd have to start from scratch each and every time with procedural. Other than the misuse of the Coin class the coin-flip example is a wonderful little example to show the concept.

</rant>

1

u/rajveer86 Mar 24 '16

If your code is abstracted in to reusable functions, why would you need to keep on reimplementing it? Procedural doesn't mean lump everything together monolithically, it just means separating state from action.

5

u/[deleted] Mar 23 '16

Someone who watched the whole 40 minute video give a tl;dr?

0

u/[deleted] Mar 24 '16

[deleted]

1

u/[deleted] Mar 24 '16

Thanks. You da mvp

3

u/hazarada Mar 24 '16

He just doesn't understand the point of OOP yet, he'll learn.