Live data from Hacker News

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

buttondown.email

331–340 of 389 posts

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

#331
post #319

Earlier quoted context omitted.

There is no benefit to the "tree of life" single inheritance [1]. What you want to reach for to achieve compositional behavior or polymorphism are type classes, traits, and interfaces. They attach methods to data without the silly "Cat is an Animal, Dog is an Animal" cladistic design buffoonery. There's nothing wrong with OO, except for inheritance-based OO. [1] Don't get me started on multiple inheritance. Instead o…

Yes. I hadn't heard the term tree of life inheritance, but that is the heart of the problem. I think this could be somewhat mitigated by banning access to non-abstract parent methods and all grandparent methods (as well as all relations that aren't directly reachable by going up the tree). But at that point you might as well just use composition anyway.

GreatGrandparent->setCancerChancePercent(10);

Grandparent->mutateCancerChancePercent("+|-", 3);

Parent

Child

By banning access to GreatGrandparent's getCancerChancePercent(); method, I won't know what the starting chance was, which will make it harder to determine nature vs. nurture. Isn't that what ancestry and genome mapping is doing? Going back up the tree?

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

#332

Nobody calls it this, but cascading styles in CSS is just like inheritance and I think should be avoided for all the same reasons. I feel it's a big part of why CSS at scale becomes unmaintainable. There isn't even a built-in way to compose two classes together if you want to avoid cascading/inheritance. It looks like composition over inheritance has caught on as the better default in other languages, but in the CSS…

The cascade was born from a time when the hope was to apply your own styles independently of the site's style, without overriding theirs completely. I think the standard languished and fell out of favor, but that was the hope.

I'm having trouble finding the original video, but Jacob Thorton (@fat) did an insightful talk on the whole affair called "Cascading Shit Show"

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

#333
post #218

Earlier quoted context omitted.

Inheritance was a premature optimization for computers with small memory. It did its job. However, once memory got big, people forgot to throw it out. Composition uses a lot more indirection. That's bad on modern CPUs. Pointer chasing throws out performance, so composition is not always preferred, either.

Composition doesn't imply pointer chasing. In C++ there is extremely little difference between the code granted for inheritance or composition (unless you use virtual inheritance), so I have no idea what overhead you are talking about.

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.

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

#334

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. And if you want to avoid huge if-e…

I see that a lot - the alternative to inheritance, when inheritance does make sense, is code duplication, which is much worse than inheritance, or first-order functions, which many languages don't actually support or don't support efficiently.

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

#335

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. And if you want to avoid huge if-e…

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. Agree 100%. It starts from the earliest programming course where we just teach it all wrong; way to abstract (no pun intended). One point to add to yours: well executed OOP allows for "structural" flow of con…

I used to be an OO hater until I started playing with Smalltalk. In particular, I worked through the Pharo MOOC (https://mooc.pharo.org/), which teaches you exactly this: designing the hierarchy IS designing the control flow of the program.

That said, Smalltalky hierarchies are a nightmare in most other languages because another key part of the Smalltalk env is the tooling -- staying inside a running system and being able to edit code from within the debugger is absolutely great and keeps you in a flow state much better than any other workflow I've been exposed to (including Lisp + Paredit + SLIME). The result is that editing class hierarchies in blub-y OO languages is usually a massive pain in the ass, while doing it in Pharo or a similar Smalltalk env is fun and painless. This is why you can't practically write Smalltalkly in Python/C#/Java/etc. even if on paper they have all or almost all of the same features.

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

#336

Earlier quoted context omitted.

Composition doesn't imply pointer chasing. In C++ there is extremely little difference between the code granted for inheritance or composition (unless you use virtual inheritance), so I have no idea what overhead you are talking about.

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.

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

#337

Earlier quoted context omitted.

Polymorphism is doable in plain old C with lookup tables and function pointers. If that is the only benefit, what is the point of creating a language where everything is an object?

> what is the point of creating a language where everything is an object? I think that's the ultimate culprit in everyone hating inheritance. If it weren't for Java, I think we'd all have a healthier view of OO in general. I learned OO with C++ (pre C++-11), and now I work at a Java shop, but I'm luck that I get to write R&D code in whatever I need to, and I spend most of my time in Python. In C++ and Python, you get…

I learned OO with Smalltalk.

Everything is an object so there's no if-it's-an-object do this but if-it's-not-an-object do that.

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

#338

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. And if you want to avoid huge if-e…

Polymorphism is doable in plain old C with lookup tables and function pointers. If that is the only benefit, what is the point of creating a language where everything is an object?

So we might guess that the language designers thought there were other benefits.

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

#339
post #311

Earlier quoted context omitted.

> There are many types of large scale systems where I would only code it using the JVM, everything else is a nightmare. I’ve seen more than one lifetime’s worth of nightmare code written in Java. I agree with the GP commenter - I don’t think the problem is Java (the language) so much as the culture surrounding it. The Java programmers I’ve worked with (all smart people) seem addicted to solving all problems my writin…

> I’ve never seen this abstraction vomit disease be quite this bad in software written in other languages. You can find a bit of it in C#, C++, go and Python. But not as bad as Java That’s just your personal experience with these languages. I would even go as far to say that there is some survivorship bias — certain projects might have survived in java that collapsed under its own weight in some other platform, makin…

Sure. Unless you have data, we’re only talking about our personal experience.

But there’s plenty of large, apparently healthy, projects written in other languages. The Linux kernel (C). Chrome (C++). Unreal engine (C++). Postgres. And so on.

My claim (and criticism of the culture around java) is that I’ve seen a lot of large Java projects at medium to large companies which didn’t need to be large projects at all. And wouldn’t be if they were built in a different style. The claim that building verbosely is good is almost never justified with data, and always seems deeply suspect to me. My instincts often say “I could rewrite this monolith with 2 smart developers in 3 weeks”. After 30+ years writing software, I’ve learned to trust my instincts.

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

#340

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. And if you want to avoid huge if-e…

> you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing

What does this have to do with inheritance? This is just a generic function.

> And if you want to avoid huge if-else statements, you should put the code for the special cases in the classes, not in each function that operates on them

This doesn't sound any different from how people usually talk about inheritance.

I am not convinced it gives you anything more than composition does. Composition is very easy to setup and easy to change. And there are certainly functional ways to do runtime polymorphism.

Post reply on HN