Live data from Hacker News

If Inheritance is so bad, why does everyone use it?

buttondown.email

381–389 of 389 posts

Re: If Inheritance is so bad, why does everyone use it?

#381

Earlier quoted context omitted.

Composition implies a level of complexity. There may be a near infinite number of permutations of things that can be combined. If in reality there are only a fixed set of variations, inheritance may model your system in a tighter way. The benefit can be simplicity.

> Composition implies a level of complexity How?

Inheritance is more limited in what you can do and implies a finite set of behavior states. With composition you have a system that can, in theory, have an untestable amount of permutations of behavior strung together.

Re: If Inheritance is so bad, why does everyone use it?

#382

Earlier quoted context omitted.

> Composition implies a level of complexity How?

Inheritance is more limited in what you can do and implies a finite set of behavior states. With composition you have a system that can, in theory, have an untestable amount of permutations of behavior strung together.

Not really? You can use interfaces with composition that keep you from plugging anything into anything. What you're describing sounds more like functional programming while ignoring the names of functions.

Re: If Inheritance is so bad, why does everyone use it?

#383

Earlier quoted context omitted.

Inheritance is more limited in what you can do and implies a finite set of behavior states. With composition you have a system that can, in theory, have an untestable amount of permutations of behavior strung together.

Not really? You can use interfaces with composition that keep you from plugging anything into anything. What you're describing sounds more like functional programming while ignoring the names of functions.

For a simple example, I might have class A that contains an instance of interface/base type B and another field of interface/type C. If B and C each have 4 implementations that means class A can be composed 4 * 4 = 16 ways. But in practice there may only be matching types of B and C that line up such that there's really only going to be 4 distinct permutations of A. I could model this more easily with inheritance.

Re: If Inheritance is so bad, why does everyone use it?

#384

Earlier quoted context omitted.

Not really? You can use interfaces with composition that keep you from plugging anything into anything. What you're describing sounds more like functional programming while ignoring the names of functions.

For a simple example, I might have class A that contains an instance of interface/base type B and another field of interface/type C. If B and C each have 4 implementations that means class A can be composed 4 * 4 = 16 ways. But in practice there may only be matching types of B and C that line up such that there's really only going to be 4 distinct permutations of A. I could model this more easily with inheritance.

This has not been a practical problem in my experience. Because composition makes it easier to tell what something does, people are not putting the incorrect implementations of things together. They're essentially just functions with contracts. Very easy to read.

With inheritance, you can't know what something does until you know what everything it inherits does. The little method you override could change the entire execution flow of the code. It's extra steps with no benefits.

Re: If Inheritance is so bad, why does everyone use it?

#385
post #216

Earlier quoted context omitted.

Anyone not using components for game objects is insane. How do you add a fire breathing horse without that

Yes, now take it further. Anyone not using components for all domain modeling is insane. Why does everyone assume this wonderful practice only applies to games?

Any recommendations for where we can learn about this approach, or recommended search terms? I'm struggling to find anything through Google that isn't about the Unity component system in specific. Is it this? [1]

https://en.wikipedia.org/wiki/Entity_component_system

Re: If Inheritance is so bad, why does everyone use it?

#386
post #129

Earlier quoted context omitted.

Well, it does work well for the engine part of game engines, and graphics related code in particular as the GP says - it's just gameplay code where OO falls a bit flat.

Unless it'a role playing game.

I don't follow - role playing games are often most suited to ECS, because you want the kind of emergent gameplay that independent gameplay systems acting on component composition gives you - and the kind of flexibility it allows in composing weapons, spells, buffs/debuffs, and so on, vs a strictly hierarchical OOP approach.

Re: If Inheritance is so bad, why does everyone use it?

#387
post #336

Earlier quoted context omitted.

At least in the case of C#, composition tends to be done via interfaces, which has more indirection than if they were to use an abstract class. This has been somewhat mitigated in newer versions of runtime. Not sure what it's like in JVM world, I know they had a better dervirt for a while.

Yeah, but that is a decade later, and also ignores the JIT optimizations like devirtualization, or just like C++ templates, using generics for composition.

> JIT optimizations like devirtualization

JIT devirt can be nitpicky, PGO has improved the situation greatly but until around 6.0 or 7.0 devirt was easy to 'break' in a lot of common scenarios.

Heck, tying into the generic composition bit, ironically static generics still have issues right around devirts. Go figure.

Re: If Inheritance is so bad, why does everyone use it?

#388
post #369

Earlier quoted context omitted.

I can think of one. For Java at least. Imagine that I have a class that has 15 methods -- 14 are a perfect fit as is, but 1 of them needs to be completely overwritten. If I were to do inheritance, I would do `extends` on the class, and then `@Override` the offending method. Problem solved. If I were to do composition, I could use the Delegation Pattern from the Gang of Four, but that would require me to write 14 do-n…

It really depends on what the code variation is, and it can often be a code smell, but I like just adding a flag to the method for simpler cases of this. No complexity or overhead, easy to reason and debug, and you can retain the current method signature with a default-case call to the new method, so no other refactoring required. It can be inelegant, clunky, and terrible practice if you're doing two totally differen…

That's fair. Though, my point stands. Once that number starts to grow, that's when inheritance starts to shine. Again, only in Java as is.

And yes, excellent wording with the idea of "Should not even know about the concept". I think that is another good place where inheritance shines.

Re: If Inheritance is so bad, why does everyone use it?

#389
post #385
post #216

Earlier quoted context omitted.

Yes, now take it further. Anyone not using components for all domain modeling is insane. Why does everyone assume this wonderful practice only applies to games?

Any recommendations for where we can learn about this approach, or recommended search terms? I'm struggling to find anything through Google that isn't about the Unity component system in specific. Is it this? [1] https://en.wikipedia.org/wiki/Entity_component_system

Yeah, that is it. I found this book to be pretty good https://gameprogrammingpatterns.com/ and heard good things about https://www.gameenginebook.com/
Post reply on HN