Hacker News new | ask | show | jobs
by benihana 4360 days ago
>While it's a well-written article, it really seems like beating a dead horse

I graduated college about 10 years ago, and I was never taught anything near composition over inheritance. I was told about inheritance, but had to learn from other coworkers and experience that composition is much favored over inheritance. Now I mentor a number of junior developers and they need to be told composition over inheritance often. This isn't a problem that has fixed itself, and it's most certainly not beating a dead horse when it needs to be repeated for so many young programmers.

This isn't just a problem with formal CS courses - code schools like Hacker School and Flatiron School have this issue as well. It seems more natural and intuitive to inherit than compose. So this horse needs to continue being being as it's very much alive.

3 comments

"I was told about inheritance, but had to learn from other coworkers and experience that composition is much favored over inheritance."

You weren't taught that, because it isn't true. Inheritance is appropriate sometimes, composition at other times.

In particular, when a two objects are modeled by an ISA relationship, you should use inheritance. When they're modeled by HASA, you should use composition.

A Dog is not an Animal that includes Barkable and PeeOnHydrantable, and a SpaceShuttle is not a descendant of Airplane.

You're missing the most important cases - there are cases where it could go both ways in terms of ISA vs. HASA, in which case (the article says) you should use HASA.

Just saying "use the tool appropriate to the job" dismisses the entire problem where in the real world, it isn't clear what that is.

I'm not responding to the article, I'm responding to the parent, who is saying that encapsulation is "much favored" over inheritance. It is not. At least, not amongst more experienced programmers.

Even in marginal cases, you can reasonably decide that a data model should use inheritance. There's no presumptive bias against it.

> A Dog is not an Animal that includes Barkable and PeeOnHydrantable, and a SpaceShuttle is not a descendant of Airplane

I think there is probably a lot of room for debate on this.

(Edited to make it less pithy and therefore probably much less irksome)

I don't see how this isn't circular. The principle is about the choice of how to model things.
>It seems more natural and intuitive to inherit than compose

Especially when you finally figure out inheritance and OO you fall in to that "when all you have is a hammer".

Composition is not an obvious way to approach this problem when you've been taught to solve problems with inheritance - and it fits "so nicely".

Effective Java is the textbook of junior professional programming. All your new hires shoyld read it.