Monday, January 02, 2006

Anti-Pattern of the day - Inheritance == object oriented development

Object oriented programming is about the objects themselves, not about whether they are part of an inheritance hierarchy. How many times have I encountered an inheritance hierarchy where the "objects" that make it up have no copy constructor or assignment operator and are not disposed of correctly. Common symptoms of this particular anti-pattern are memory leaks and it's bedfellow, confusion of ownership.

I am not alone in my suspicion of inheritance. Sutter says with good reason to "Always prefer composition over inheritance". Stroustrup views base classes as "Places to put commonality".

I wouldn't go so far as to suggest that a substantial inheritance hierarchy in itself is an anti-pattern, as appropriate, finely crafted inheritance can be an elegant solution, but an inheritance hierarchy that violates the Liskov substitution principal is definitely a precursor to the worst of them all, the spaghetti pattern.

No comments: