Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
No, inheritance is not the way to achieve code reuse (littletutorials.com)
22 points by edw519 on July 6, 2008 | hide | past | favorite | 5 comments


In my experience the pitfalls of inheritance are something you have to really see for yourself in practice, so it generally takes even very good developers years to really develop the intuition around when to use inheritance and when to use composition. Inheritance is a blunt tool that's easy to use, while composition is trickier and requires more skill and foresight to use correctly. There are also more different styles of composition that accomplish slightly different things, so you have to know which one will work best for your problem. My personal experience is also that many people just don't understand compositional models as readily either.

One of the big problems, though, is that inheritance is much easier in most programming languages than composition is. Java, for example, has absolutely no support for composition or delegation, whereas inheritance requires a single keyword. C++ has multiple inheritance, which you can kind of re-purpose for the sake of composition, but it's still not very elegant and tends to just exacerbate the misuse of inheritance instead. Ruby has a pretty elegant mixin model, and you can pretty much do whatever you want in Javascript if you're willing to roll the solution yourself (I confess that my Python knowledge is more limited so I have no idea if there's support there), but the most mainstream OO languages require you to really fight them in order avoid using inheritance inappropriately.


I think this is yet another argument against JavaSchools. The article mentions roles, which are a very powerful way to compose class fragments into classes; usually much more cleanly and reusably than inheritance. Unfortunately, Java doesn't have Roles (or Traits; same thing), so you just can't teach it. Instead, you have to teach hackish workarounds that don't even work. The choice of "a real language" to teach computer science means that you can't actually teach computer science. Instead you teach Java.

It's a shame that students have to begin their programming careers by working around bad tools.

Anyway, I think everyone with a degree in computer science should at least be able to write programs in Smalltalk, C, Lisp, and a dynamic language (Perl/Python/Ruby). Let them learn .NET and Java in trade school. They are of dubious educational value.


This makes an important point that too few people understand. "Inheritance of implementation" is a rare trick. It's something appropriate to only a small fraction of problems. I constantly see people trying to use inheritance as an extension mechanism (i.e. "just inherit from this class to use the library"), and it's just a disaster.


Yeah. I think Apple's Cocoa framework is a good example. Instead of inheritance you use delegates to modify class behaviour. Subclassing is 100% possible but not very often required because of the delegate pattern.


Takes him a while to get to it... "The reasons for using inheritance are modularity, separation of concerns, clear representation of concepts, categorization and polymorphism." Exactly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: