Ask HN: Inheritance?
Inheritance is just an implicit form of delegation that comes with a bunch of default behaviors that can totally screw you.
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.
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?
5 comments
[ 2.8 ms ] story [ 25.9 ms ] threadIt'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.
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. :)