91
Designing with Reuse in Mind
Documenting a Class and Using Comments
The topic of comments and documentation comes up in every book and article, in every
code review, and in every discussion you have about good design. Unfortunately, com-
ments and good documentation are often not taken seriously, or even worse, they are ig-
nored.
Most developers know that they should thoroughly document their code, but they
don’t usually want to take the time to do it.The bottom line is that a good design is prac-
tically impossible without good documentation practices.At the class level, the scope
might be small enough that a developer can get away with shoddy documentation. How-
ever, when the class gets passed to someone else to extend and/or maintain, or it becomes
part of a larger system (which is what should happen), a lack of proper documentation and
comments can be lethal.
Many people have said all of this before. One of the most crucial aspects of a good de-
sign, whether it’s a design for a class or something else, is to carefully document the
process. Implementations such as Java and .NET provide special comment syntax to facili-
tate the documentation process. Check out Chapter 4,“The Anatomy of a Class” for the
appropriate syntax.
Too Much Documentation
Be aware that overcommenting can be a problem as well. Too much documentation and/or
commenting can become noise and may defeat the purpose of the documentation in the
first place. Unfocused documentation is, unfortunately, often ignored.
Building Objects with the Intent to Cooperate
In Chapter 6,“Designing with Objects,” we discuss many of the issues involved in design-
ing a system.We can safely say that almost no class lives in isolation. In most cases, there is
no reason to build a class if it is not going to interact with other classes.This is simply a
fact in the life of a class.A class will service other classes; it will request the services of
other classes, or both. In later chapters we discuss various ways that classes interact with
each other.
In the cabbie example, the cabbie and the supervisor are not standalone entities; they
interact with each other at various levels (see Figure 5.4).
When designing a class, make sure you are aware of how other objects will interact
with it.
Designing with Reuse in Mind
Objects can be reused in different systems, and code should be written with reuse in
mind. For example, when a Cabbie class is developed and tested, it can be used anywhere
you need a cabbie.To make a class usable in various systems, the class must be designed
with reuse in mind.This is where much of the thought is required in the design process.
Attempting to figure out all the possible scenarios in which a Cabbie object must operate
is not a trivial task.