Live data from Hacker News

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

buttondown.email

51–60 of 389 posts

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

#51

I work on a very large codebase. We use inheritance and composition. I totally agree that inheritance should be used carefully. However, there are times when composition requires a lot more code and downstream maintenance. Let's stay I have a base class A. Let's say I have 70 classes that inherit from A. These classes must serialize/deserialize from disk. My choice here is that I can add a new data member to A and th…

The answer is that you retain A as a pure interface and maybe you add a convenience base below A that adds the data member, then you inherit from from the convenience base for your 70 leaf classes. You can also use CRTP for the convenience base, if it's appropriate. You can still avoid confusing interface and implementation inheritance.

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

#52

I don't think inheritance is bad at all. It is very often the easiest way by far to model a problem. Sure, it's not perfect, but I think it is wildly overhated by a vocal minority.

I have found that single inheritence can be useful but it is very restrictive.

More than once, I have found an inheritance hierarchy that made sense when first created no longer models the problem well. It becomes hard to change it at that point.

Frequently I find I really want to mix in cross cutting concerns across different hierarchies. This isn't really a surprise; most problems do not naturally decompose to only a single view of things.

I don't mind abstract base classes containing common functionality, so it's useful there, but mix ins would be better to be honest.

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

#53

Here is my take on it. At one point "Object Oriented" became "Blockchain" (or now Gen AI) of those times. You had to be "object oriented" in order to be taken seriously. This applied to everything. Even finished software products were called "built using object oriented". The esoteric concept of inheritance became popular after that. At some point it became so popular that people figured out that it is not really a g…

In the 1990s, it coincided with the proliferation of GUIs and their respective programming interfaces. Most frameworks use hierarchies like Object->View->Control->Button->ImageButton. Then people decided that this is the way for modeling abstract problems that don't have to deal with visual or real-world entities whatsoever.

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

#54
Inheritance is a local maxima. When the hierarchy of classes to consider is small, it often “seems to fit”. It allows the programmer to progress quickly and with low effort as a lot of the code sharing behavior is provided by the inheritance mechanism.

Trouble often comes down the line. We keep adding classes, and soon we find that the hierarchy no longer is “shaped like a tree”. A soccer ball is a spherical object but also a sports equipment and a bouncing object, and there’s no way to organize those into branches.

Our reality isn’t “tree-like”, except when we simplify it extensively.

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

#55

My impression is the opposite, especially from when I used to work as a full-time Java dev. I often asked myself: Is there a place outside of GUI programming where inheritance is used in non-habitual and useful way? I can't think of many. More often than not you have a final class that you are supposed to use and the petrified hierarchy above that is of not much use.

Perhaps having some abstract class with a real and mock implementation inheriting from it, which can then be injected into your code for testing other components is one example.

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

#56
post #21

Here is my take on it. At one point "Object Oriented" became "Blockchain" (or now Gen AI) of those times. You had to be "object oriented" in order to be taken seriously. This applied to everything. Even finished software products were called "built using object oriented". The esoteric concept of inheritance became popular after that. At some point it became so popular that people figured out that it is not really a g…

Pragmatists used inheritance because it gave a quick benefit now, and the longer-term costs were ignored. Some pragmatists became successful because they moved quickly, so the "Programmers from the Church of Purity" used inheritance because successful companies used inheritance. When inheritance was no longer the quickest way to move quickly, the pragmatists moved on. The Church of Purity now bangs on about Functiona…

> Pragmatists used inheritance

Because at some point mainstream OO languages made inheritance easy and composition harder, nothing more.

Composition with Java used to be verbose. Inheritance declaration was simply a single keyword.

If Java had "mixins" from the start, people would have used composition way more.

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

#57
post #21

Here is my take on it. At one point "Object Oriented" became "Blockchain" (or now Gen AI) of those times. You had to be "object oriented" in order to be taken seriously. This applied to everything. Even finished software products were called "built using object oriented". The esoteric concept of inheritance became popular after that. At some point it became so popular that people figured out that it is not really a g…

Pragmatists used inheritance because it gave a quick benefit now, and the longer-term costs were ignored. Some pragmatists became successful because they moved quickly, so the "Programmers from the Church of Purity" used inheritance because successful companies used inheritance. When inheritance was no longer the quickest way to move quickly, the pragmatists moved on. The Church of Purity now bangs on about Functiona…

> The Church of Purity now bangs on about Functional Programming in the same way for the same reasons.

FP in the industry is a niche thing, so the two phenomena are not really comparable.

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

#58
post #21

Earlier quoted context omitted.

Pragmatists used inheritance because it gave a quick benefit now, and the longer-term costs were ignored. Some pragmatists became successful because they moved quickly, so the "Programmers from the Church of Purity" used inheritance because successful companies used inheritance. When inheritance was no longer the quickest way to move quickly, the pragmatists moved on. The Church of Purity now bangs on about Functiona…

FP rather slows things down though.

What specifically slows it down in your context?

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

#59
My impression is that inheritance is in many common programming languages the easiest way to share code. Sharing code in another way would require some more thoughts and sometimes code, so the lazy programmer takes inheritance.

Traits as a general concept would be very useful in many programming languages, but only some (like Scala) have proper support for it. In principle a Java interface with default methods (implemented methods) is also something like a trait, but very limited compared to traits in Scala. I have no statistics, but my impression is that inheritance is much less used in Scala, because the language has an easy to use alternative.

Another example is Go, where structs with their associated methods can be embedded, what is exactly composition supported by the language. Since Go doesn't support inheritance, programmers obviously needs to use this approach, so you would never see inheritance in Go programs.

So, my conclusion is that the usage of inheritance depends on what the language supports and how easy it is to use.

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

#60
post #58

Earlier quoted context omitted.

FP rather slows things down though.

What specifically slows it down in your context?

Doing every simple thing that used to be done in "normal procedural" way in functional paradigm instead when using something like Scala or fp-ts in TypeScript.

Causing engineers to completely change their mental model of how the code runs, which I still have intuitive trouble imagining correctly and I see it with other developers as well. A lot of energy goes into trying to understand how to do a simple action. It is much harder to read the code and have a correct mental model as well.

Post reply on HN