3 ms·
Drifting from the topic, but I never understood this urgent need for so much inheritance when doing OO, especially when using languages that don't natively have
by skrebbel 13y ago
Drifting from the topic, but I never understood this urgent need for so much inheritance when doing OO, especially when using languages that don't natively have it, like C and JavaScript.
I really like OO programming (i.e. I'm particularly not one of the functional-fundamentalist OO-haters here on HN), but as I've grown more experienced, I've started to use inheritance less and less, and composition more and more. This is trivially supported in C structs, and you don't need any fancy approach for it, plus you can do with way less void pointer casting, which means less runtime errors.
Inheritance is very useful for a small set of relatively complex problems, such as language parsing or UI frameworks or particle simulations. For many other problems, including most applications (as opposed to libraries), however, my experience is that you can do fine without. This holds especially these days, when many of said complex problems are solved in excellent freely available open source projects, in nearly any language.
- 616c 13y agoThat is funny because my limited experience with Python, on-off over the years, it was impressed upon me many, many times composition is superior to inheritance, and the language even has both built-in. And yes, for all the pointy-hairs around, I know there are exceptions to the rule. I am speaking generally.
- Someone 13y agoThat is far from an uncommon idea. The phrase to search for to learn more about this is "Composition over Inheritance". That leads you to e.g. http://en.wikipedia.org/wiki/Composition_over_inheritance http://en.wikipedia.org/wiki/Composition_over_inheritance or, combined with "site: c2.com" to http://c2.com/cgi/wiki?CompositionInsteadOfInheritance http://c2.com/cgi/wiki?CompositionInsteadOfInheritance or http://c2.com/cgi/wiki?UseCompositionAndInterfacesWithoutClassInheritance http://c2.com/cgi/wiki?UseCompositionAndInterfacesWithoutCla.... In some sense, deep inheritance hierarchies were a thing of the 80's, when inheritance was new (at least, in the mainstream), just like the <blink> tag was a thing of the '90s (or was that the 00's?). Now, deep hierarchies are a code smell.
- pjmlp 13y ago80's!?! OO only started to get into the enterprise in the 90's.
- camus2 13y agoJavascript supports prototypal inheritance natively and it works well. The problem is people trying to fit every java oo concept in js, ending up writing unreadable mess.
- discreteevent 13y agoI agree and I think that this is what Go gets right. The thing that's valuable about objects are the interfaces (Alan Kay said it was the messages). Microsoft's COM is similar in this regard.