Is inheritance evil?

Started by
16 comments, last by Ravyne 7 years, 11 months ago
I've becoming more and more convinced that inheritance is a code smell. This article explains my sentiments
http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html
I tend to agree with it because inheritance has caused me plenty of frustrations. I really appreciate what the makers of golang did. They got rid of inheritance and make use of embedding and interfaces instead.

Am I wrong to think that inheritance should be avoided?
My current game project Platform RPG
Advertisement
Of course it isn't. It's s tool that can be used or abused.

Even deep inheritance trees can be appropriate in certain domains. Look at something like Qt for example.

There is, though, a huge amount of very bad OOP out there and a great deal of confusion about the is-a and has-a relationships.

But inheritance itself is not the cause of this. Personally I blame Java :).
The key question facing any problem is: "what are the right tools to solve this?"

You don't kill a fly with an elephant gun. But sometimes. Just sometimes. You find yourself up against an elephant.


PS - it's a goddamn metafor.

I've becoming more and more convinced that inheritance is a code smell. This article explains my sentiments
http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html
I tend to agree with it because inheritance has caused me plenty of frustrations. I really appreciate what the makers of golang did. They got rid of inheritance and make use of embedding and interfaces instead.

Am I wrong to think that inheritance should be avoided?

Inheritence works best for ideas and implementatino of those ideas.

Or interfaces you need to implement. These useages are perfect for inheritence.

_HOWEVER_

If you wish to add a new behavior, then you should review your design and seek for other options.

Many times i've read that compoisition is better than inheritence in these cases. And sometimes they are right.

Some good patterns involve with composition rather than inheritence, such as the adapter. Facade, Proxy and decorator.

For most cases, strategy pattern is a better solution than inherit the class and add the behavior.

It seperate concerns and behaviors into new REUSEABLE components. This is a perfect goal. Therefore inheritence lack in that case.

But, if you use inheritence to express a new idea from another idea, it will suit.

In example: A chair got magnified and got a new coolish look, so new data is presented such as "color of the flag", "the hamster that is sitting on it" and my favoritre "The cat that ate the hamster".

This chair doesnt need the cat as a strategy since it's its own field.

There are many articles that state "XXX IS BAD, USE YYY". "YYY IS TERRIFIC, STILL USE XXX".

Don't listen to those articles. Good programmers know to valuate their tools and state what these tools are good for.

The article is correct, assuming you read it.

Note that it is talking about "extends", which is one type of inheritance. Java's "interface" and "implements" are another type of inheritance.

The article as a whole is correct with it's proper title: "Why extends is evil: Improve your code by replacing concrete base classes with interfaces."

It is not correct with your title about "inheritance".

In C++, C#, Java, and other similar languages, it would perhaps be better to write: "Base classes should be abstract, leaf classes should be concrete." Generally you should make non-leaf classes abstract. In Java grammar the article's author is correct that generally 'implements' should be used over 'extends'. In other languages, the grammar for the rule is different.

If you are familiar with SOLID principles, the O, L and I initials are easily implemented if you use interfaces, also called abstract base classes in some languages. The D in SOLID is practically a necessity for base classes to not be concrete.

The SOLID principles serve as an excellent base for software architecture. They were good advice before the acronym made it cool, and the fact that some people found a memorable acronym makes it even better.

The problems he describes in the article are with O (the Open/Closed principle) as base classes are fragile when it comes to extension in some code bases, L (Liskov Substitution principle) because some code bases write subclasses that cannot be used as substitutions for each other, and D (Dependency Inversion principle) because the examples he gives do not operate on the abstractions but on the concrete implementations.

For Java, the article's title about "extends is evil", that is true enough.

No. Inheritance is not evil.

Inheritance is the base class of evil.

:P

More seriously, my usual advice for these sorts of cases where people keeping calling something bad practice is to _avoid_ it, but don't _completely abstain_ from it. That is, inheritance is usually the wrong tool for the job. _Usually_. You shouldn't reach for inheritance whenever you begin solving a problem without thinking through the consequences and deciding that you do really need it. Will using it unnecessarily greatly harm your code? Probably not. You're just probably cheating yourself out of a better solution by reaching for the easy inheritance approach too soon. :)

Sean Middleditch – Game Systems Engineer – Join my team!

It is good advice to prefer interfaces over inheritance, but there are uses for inheritance too. Inheritance does get over used, probably because it is often taught too early to new programmers.

Personally, I reserve the word "evil" for things that are anti-patterns in all cases. For example, in C++, NULL is evil and can get you into trouble; prefer nullptr in c++11 and later or 0 in C++03 and earlier.

As others have said, one must distinguish between "interface inheritance" and "implementation inheritance". Speaking in generalities, the former is a good thing, and the latter almost exclusively a bad thing.

However, and this is a fairly large caveat, in languages such as Java which lack any concept of mixins, one ends up having to use implementation inheritance to achieve a poor-man's version of mixins... so we don't have the leeway to disallow it entirely.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Class inheritance can break pooling of ascendant objects with immediate access, etc. but can as well be totaly neutral towards run time performance in some cases, which are feature extendings/property inflatings ... for example. It is surely good to aproach it as a "no no" and think twice before implementing it.

Inheritance is not evil. You need it for a variety of designs and a lot of code can not be created as efficiently without it. It's evil when you use it poorly however, hence diamonds of deaths which can cause you a whole slew of logical problems.

This topic is closed to new replies.

Advertisement