Reusability problems with inheritance

The paper Traits: Composable Units of Behaviour contains a great description why inheritance is not the best tool for code reuse.

Basically code reuse and inheritance are not orthogonal, so I never know if my class hierarchy should be designed to maximize code reuse, or if I should limit my code reuse not to interfere with the class hierarchy. There’s no simple rule, and it isn’t trivial to make the right decision. As the paper summarizes:

[...]multiple inheritance uses the notion of a class in two competing roles: the generator of instances and the unit of code reuse

Schärli, Ducasse, Nierstrasz and Black describe the pitfalls of inheritance (single, multiple and through mixins) as a code reuse tool in their paper (check chapter 2) with such clarity I recommend any OOP programmer to read the paper.

Unfortunately they do not consider delegation in the mix, so there is some room for improvement. Their only reference is at the end of chapter 7:

Delegation (also known as “object-based inheritance”) is another form of composition that side-steps many of the problems related to class-based inheritance [24]

Have in mind this paper is not about reusability problems with inheritance, but about traits, their proposed solution to these problems. If you’re interested in traits you’ll find a good discussion in Reddit’s thread OO Programming: why “traits” rule and mixins and multiple inheritance suck.


About this entry