Tuesday, November 02, 2004

Re-usable Code

Keith Ray blogged about code re-usability as a set of rules.

I will summarize the rules that I like:
  • Write your class to avoiding depending on other classes (etc).
  • A class should have one responsibility, delegating other responsibilities to other classes.
  • A class should not depend on other concrete classes [except the most basic types like String].
  • To further allow substitution of concrete types, avoid using "new ConcreteClass" within your class -- particularly the constructors.
He goes on to say:

The user of your class may want to subclass-and-override. Make that possible by avoiding static, final, or non-virtual methods, and private methods. Private methods either need to be declared protected, or should be moved to another class and made public, with the original class using an instance of that other class.

And provides a set of rules for making classes easily extensible.

I strongly disagree with this sentiment. Making everything overridable may seem to increase the likelihood that your class will be reused, but it also makes it likely that the implementation of a class will be spread across several classes in your hierarchy. In the long run, this makes maintenance increasingly difficult, particularly if you choose to employ the Template Method Pattern.

I think it makes more sense to prefer containment to inheritance, and to mix in new behaviors with the Inversion of Control pattern.