Hacker News new | past | comments | ask | show | jobs | submit login

It's mostly stuff I picked up from practical experience and talking to other programmers, so I don't have any links offhand. Let me expand a bit though.

The "wrong" way (or what I view as the wrong way) tends to be common in academic settings, and (seemingly) among Java programmers. It is often contemptuous or wary of multiple inheritance, and it follows the Nygaard Classification[1] "A program execution is regarded as a physical model, simulating the behavior of either a real or imaginary part of the world."

This definition makes sense when you realise it was created by the inventors of Simula to describe their OOP simulation language. Unfortuantely, Simula was far more influential than it deserved, because OOP is terrible at simulating things[2]. When you create a class structure based on the actual simulation you (1) will have a bear of a time doing it, (2) will find multiple inheritance will melt your brain ("okay, so Black and White inherit from Color, and Zebra inherits from Black, White, and Animal...") and (3) will find than once your done you haven't actually solved your problem. (Not surprisingly, Simula didn't have multiple inheritence, and language without that feature often seem to gravitate to Nygaard-style OOP.)

So much for what not to do. What about the right way? I don't really have any links, but good principles are: Focus on interfaces and behaviours. Classes should be abstract (in the common sense, not the language keyword), not concrete. Understand how your program works, break it down into functional areas, implement each area as a class. If you find common behaviours, abstract them into base classes which you mixin where you need them. Inheritence trees should be flat, minimal, and almost an afterthought. Also, a language like Python (with duck typing) is a lot easier for most people to "get" OO than Java.

Finally, the way it was explained to me that really "clicked" is this:

"Where I think most introductory courses in OOP go wrong is introducing objects as being nouns rather than a collection of verbs. That leads directly to improper use of inheritance. I was misled for years by the 'is a' idea. I wish someone had told me 20 years ago that it was 'has the behavior of' that was important."[3]

I think that's exactly right. But do keep in mind that the world is full of people who think that's a heretical view and the Nygaard Classification is the One True Way. :)

[1]: http://c2.com/cgi/wiki?NygaardClassification [2]: http://lambda-the-ultimate.org/node/3265#comment-48063 [3]: Omnivore, in a conversation on the ##stars! channel on Freenode




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: