Jump to content
  • Advertisement
Sign in to follow this  
HappyCoder

Is inheritance evil?

This topic is 960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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. 

Share this post


Link to post
Share on other sites

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.

Edited by King Mir

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!