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…
If Inheritance is so bad, why does everyone use it?
51–60 of 389 posts
Re: If Inheritance is so bad, why does everyone use it?
#52I 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.
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?
#53Here 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…
Re: If Inheritance is so bad, why does everyone use it?
#54Trouble 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?
#55My 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.
Re: If Inheritance is so bad, why does everyone use it?
#56Here 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…
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?
#57Here 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…
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?
#58Earlier 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.
Re: If Inheritance is so bad, why does everyone use it?
#59Traits 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?
#60Earlier quoted context omitted.
FP rather slows things down though.
What specifically slows it down in your context?
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.