Reusability

This pillar of object-oriented programming is usually called inheritance, but inheritance is actually one of several techniques for achieving the broader goal of reusability. Reusability means being able to create a new class that uses the features of an existing class without recoding those features. There are actually several ways to achieve reusability in object-oriented programming.

Inheritance means reusing code in a hierarchical structure. For instance, a
Basic­Programmer is a Programmer is a Worker is a Person is an Animal. All Animals have heads, and therefore the Head property of a BasicProgrammer should inherit all the general features of Animal heads plus all the features of Person heads plus all the features of Worker heads plus all the features of Programmer heads. When creating a Head property for a BasicProgrammer object, you should need to write only the head code unique to BasicProgrammers. There’s a lot of debate in the object-oriented community about the value of deep levels of inheritance such as the one described above, but there’s no doubt that inheritance can be a useful way to achieve reusability. And Visual Basic doesn’t have it.

The way to get reusability in Visual Basic is through a process called delegation (also called containment). For example, you put an Animal object inside your Person class. You write code to expose the appropriate Animal methods and properties as Person methods and properties. Then you put a Person object in your Programmer class and expose its methods and properties, and so on.

Many object-oriented design books talk about two kinds of reuse relationships. In the is-a relationship, one class is an enhanced version of another class. A Person, for example, is an Animal with additional features. Normally, these relationships are defined with inheritance. In the has-a relationship, one class has features of another class. Normally, these relationships are defined with delegation. For example, an Animal class might delegate blood circulation to a Heart class and digestion to a Stomach class. Since Visual Basic doesn’t support inheritance, it forces you to define both kinds of relationships with delegation. If you could achieve the same results with delegation, this would be a problem only for object-oriented design purists. Unfortunately, inheritance is automatic while delegation is usually manual (although nothing stops a language designer from making it automatic). When using inheritance to model is-a relationships, you have to write code for the new features only. When using delegation for is-a relationships, you delegate everything, even the methods and properties that don’t change. This mechanical process ought to be automated, and in fact, Visual Basic does automate it, but it works only for controls, not for classes. Furthermore, automatic control delegation is not a language feature. It’s a wizard program that writes the code it thinks you want. The pitfalls and limitations of wizards are a subject I won’t get into here.

The Component Object Model (COM) supports a third reuse technique called aggregation. Aggregation means combining several inner objects so that they appear to be part of an outer object. This is a collective organization rather than a hierarchical organization. If you tried to combine a BasicProgrammer, a Pro­grammer, a Person, and an Animal using this method, you’d end up with a lot of duplication and confusion. Clients would have to decide whether to use
the Person.Head or the Programmer.Head, so instead you would use an
organization that adds features rather than extends them. If you started with
an Animal class, you might then add person features, such as Reasoning and Emotions. Later you add programmer features, such as Coding and Debugging. Finally you add Visual Basic programmer features, such as IterateWithForEach and HandleEvents. Although Visual Basic doesn’t directly support this feature, you can get some of the advantages of it with the new Implements statement.

The bottom line: reuse is unnecessarily painful and inconvenient in Visual Basic—a language that is supposed to make things easy.

Rating for reusability: .4.