Live data from Hacker News

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

buttondown.email

21–30 of 389 posts

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

#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 Functional Programming in the same way for the same reasons.

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

#22

Lots of design patterns have their uses, but can suffer when applied too overzealously.

Like almost anything in IT, if you are good at using a hammer, you treat everything like a nail.

if you're good at using a hammer, the nail will be hammered in perfectly and without faults.

The only problem is if you're bad at using the hammer, and you only know how to use a hammer.

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

#23
Not even wrong.

Inheritance isn't necessarily bad.

If anything is common, someone will try to seem like a brilliant iconoclast by writing an essay against it. The particular essay in this case was from a Pythonista (not surprising, given Python's weird love/hate with objects):

https://solovyov.net/blog/2020/inheritance/

And not everyone uses inheritance.

Maybe you have another question, like "why would someone use inheritance?"

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

#26

OOP is easy to understand and explain and it makes sense initially, plus a lot of people specialize in languages/frameworks that enforce it, so it becomes easy to get trapped in the OOP world. It also usually comes with DDD (which is a solution to a problem you shouldn't have had in the first place) which is a way to limit the damages of OOP into contextual areas. I also think the blanket statement (OOP is bad) does…

I disagree. Starting with separating class and object have never been helpful when I've tried to teach computer programming, while functions seems to have been more intuitive to those people.

After a while you get to closures, which are pretty much objects without classes, and then factory functions that produce closures and there you have something like a class as well. If I were to design a course I'd probably follow this flow to get to 'OOP' in that sense.

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

#27

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…

> Let's stay I have a base class A. Let's say I have 70 classes that inherit from A.

The alternative case is that you have 70 classes that CONTAIN A. You still only have to change A.

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

#29
IDK, I recently did a big presentation on the topic in my company's Python community and there were tons of people that were genuinely surprised it's not great. And people in my department love throwing random classes that inherit from everything in the universe for no reason at all... for python. Like not even Java or C#. And coming in as the lone new-comer its not going to be easy to convince everyone that has been there for years that the common sense aint great.

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

#30
Kotlin has a few nice patterns here. It allows inheritance but only if you mark your class as open (it's closed by default). This prevents people inheriting from things that weren't designed from that. It also has extension functions, which allows you to add functions and properties to types without having to inherit from them. This is very nice for fixing things that come with Java libraries to be a bit more Kotlin friendly. Spring offers a lot of Kotlin extension functions for its Java internals out of the box. Another nice pattern is interface delegation where you can implement an interface and delegate to another object that implements that interface:

    class Foo(private val myMap: Map=mapOf()): Map by myMap
This creates a Foo class that is a Map that delegates to the myMap property under the hood. So you side step a lot of the issues with inheritance; like exposing the internal state of myMap. But you can still override and extend the behavior. Replacing inheritance with delegation is built into the language.

The net result of this is that inheritance is rarely needed/used in Kotlin. It's there if you really need it but usually there are better ways.

Scala and other languages of course have similar/more powerful constructs. But Kotlin is a nice upgrade over Java's everything is non final by default (or generally defaulting to the wrong thing in almost any situation). Go has a nice pattern based on duck typing where instead of declaring interfaces, you can just treat something that implements all the right functions as if it implements a type. Rust similarly has some nice mechanisms here. All these are post-Java languages that de-emphasize inheritance.

Post reply on HN