

Ask HN: Inheritance? - BigZaphod

Inheritance is just an implicit form of delegation that comes with a bunch of default behaviors that can totally screw you.<p>Of course I use it constantly and spend a ton of time "designing" everything "properly" to have a sweet class hierarchy with all kinds of unnecessary power and flexibility to solve the problem at hand.<p>Does anyone else find themselves sucked into that trap? It seems like when using straight C, I can still think and program in an OO style, but I don't so readily waste as much effort trying to come up with a "perfect" tree of objects. Is this simply an issue of discipline or is there a drug-like effect when using inheritance?
======
makecheck
It seems natural to overdo a new tool until you've found its best uses.

But I think the only drug is perfectionism, and it applies to anything
reasonably complicated. The cure is brutal honesty. How much will you _really_
be able to reuse in a sophisticated design? (Not much.) How likely is it to
change? ( _Very_ likely.) Will an unanticipated new feature completely destroy
your formerly-perfect object design? (Often, yes.)

One of the original thoughts behind inheritance was to manage complexity. But
since code is read much more than it is written, I think of my eyes and not my
fingers. Sure, I may have saved some time writing the code, and even had fun
coming up with its unique design. But the next 40 or 50 times I have to go
back and understand the code, and read 4 or 5 other files just to figure out
what a function _does_ , I see the error of my ways.

That doesn't mean I avoid inheritance, but I strictly limit it to things where
it has immediate and obvious value. I may have one parent class, and the class
isn't very big. It's also not something I'm likely to share in a public
header; I'll inherit from it myself, and not ask others to do the same, which
avoids a whole category of hurt. :)

------
gaius
The effect is this: it looks like real work but doesn't require you to hit
compile and run the app and find that it doesn't work. It's the same with
refactoring. People love doing it because you seem busy but don't have to take
the risk of revealing (if only to yourself) that your code doesn't do what you
think it ought.

It's one reason for the success of Java in the marketplace, there's so much
scaffolding to be done that it keeps loads of people busy-looking.

------
ScottWhigham
Yeah, I hear ya. I think everyone has that to a certain degree. Once I heard
of YAGNI (<http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It>), I was
able to let go and just code more efficiently.

------
PaulMorgan
<http://en.wikipedia.org/wiki/Is-a> versus
<http://en.wikipedia.org/wiki/Has-a>. Usually the "perfect" tree isn't really
all that perfect.

------
brl
You should almost never use implementation inheritance. In fact, until you
have mastered designing with composition, the 'extends' keyword should be
banned from your vocabulary.

