Is inheritance evil?

Started by
16 comments, last by Ravyne 7 years, 11 months ago

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.

...


yeah, I guess when I said inheritance I was referring to extends way of doing things.
My current game project Platform RPG
Advertisement

Generally speaking, everything is evil if you don't understand it. Elements of OOP, recently hyped DoD and many other techniques - they all exists for some reason. They haven't been created to replace each other but rather to solve certain problems. Recently you may read on GD forums how programmers try to implement DoD without any valid use-case, at all cost, just because it sounds modern and makes impression of solving all world's problems. It's simply not true. DoD exists to solve certain set of problems while it may actually complicate other matters. Same goes to the inheritance. Programming is not black and white. You may successfully join techniques of using inheritance and composition etc. It's up to you what kind of architecture you built. Build it crafted to your needs not to current fashion :)

Answering you question I would say, inheritance isn't evil. As was mentioned above, everything may be used or abused. I agree with that 100%.

Implementation inheritance, as provided by mainstream object-oriented languages, is fundamentally evil. I'm a little surprised that statement is even controversial.

This isn't a new observation. The change in paradigm here came in the mid 90's, when people started noticing the limitations and flaws of implementation inheritance, and transitioning to interface inheritance. Robert Martin's original article on the topic is a worthwhile read.

If you look around enterprise software, there aren't a whole hell of a lot of meaningful software projects still clinging to implementation inheritance as the default. Unfortunately, game development software is full of such warts, I think in part because many of the open source game engines were conceived of in the early 2000's, and haven't really seen a rewrite since.

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

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 can't believe you would say something like that, it's disgusting. The word is "metaphor".

:D
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Death knows a thing or two about metaphors...

"I think the whole cat in the box idea is one of them metaphors," said Albert.
AH. A LIE.
- Discworld (The Last Hero), Terry Pratchett

Hello to all my stalkers.

cons

large inheritance can create higher coupling between classes.

root of hierarchy becomes deprecated = cascading affect on subclasses

could reveal encapsulated items

other cascading changes = change one class have to change more

large inheritance hard to understand whats going on behind the scenes with bottom classes

so in other words smaller inheritance is better.

you shouldnt always avoid inheritance. especially if you want to save time doing something simple.

like creating ten different animals who each have a different traits , do you really want to have to code what an animal is for each animal or just extend the animal class for each animal.

id say you should avoid large complex inheritance within an API if you are working on a long term project where the team will be changing.

if you are working with the same team or by yourself for the entire project and everybody's brain can handle the large hierarchy then its not a serious problem, but still possibly a bad design.

When you need to bridge two different object hierarchies it becomes an angel.

e.g. Suppose you were working on a project that mates Simulink to DirectShow.

You can do it with composition but that will be almost twice as work as you emulate a DirectShow filter on the Simulink side to serve as a sink for the composed filter - as opposed to directly making a DirectShow sink filter that dumps the data into the Simulink data flow (for which you would already have a base-class written to interface with Mathworks's C API.)

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

A good rule of thumb is to solve any problem with the least-powerful tool it can be (reasonably) solved with. Try not to think of that in a negative light -- by using the least-powerful tool, what we really mean is the one with the least unnecessary dangers attached.

There are a few general power-progressions you should try to observe:

  • Prefer composition over inheritance -- that is, use inheritance only when it supports precisely the relationship semantics you want, not for reasons of convenience.
  • Prefer interfaces (interface/implements, pure-virtual classes) over inheriting concrete classes (extends, "plain" inheritance).
  • Prefer non-member, non-friend functions over member functions, prefer member functions over non-member friend functions, prefer friend functions over friend classes, in C++.
  • Know the differences between private, protected, and public inheritance in C++, and use the appropriate one.
  • Keep things in the smallest reasonable scope.

Those are just a few examples. Being a good engineer doesn't mean being the one who smuggly wields tools of great power, confident you'll not fuck up; Its great when one can do that when they have no other reasonable choice--and you'll still fuck up--but a trait of a good engineers is that they seek out the solutions which are exposed to the minimum set of potential hazards while meeting requirements of (in mungable order) safety, performance, maintainability, usability, and ease of engineering.

Language features are not inherently evil (not even goto), but they are sometimes misapplied and the more commonly misapplied they are, or the worse the repercussions are, the worse their reputation becomes. Sometimes this is exacerbated by the way that languages are taught, as is the case with how inheritance has come to have such a poor reputation. Sometimes its exacerbated by the mistranslation of programming skills from one language to another; in general, a Java programmer (or C# programmer to a somewhat lesser degree) will *way* abuse inheritance if tasked to write C++ (and they'll probably leak memory like a sieve too :) ).

TL;DR; Know thy tools, and program.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement