Making the Changes
With your test harness in place, you can attempt an initial set of changes based on a number of strategies. Many of these strategies are well documented; for example, in Refactoring: Improving the Design of Existing Code by Martin Fowler et al. The following list describes some of the common patterns:
- Extract method. Helps by reducing the cyclomatic complexity of a method; results in a larger set of methods that are individually easier to understand.
- Extract class. When a class does too much (something that often results in extraneous efferent coupling), you break the divergent behaviors into different classes.
- Strategy implementation. Replaces logic copied into multiple classes (such as a common algorithm) with a common class that implements the algorithm, which can be injected into the appropriate classes.
- Type generalization. Rather than have a series of classes that do similar things, have one general class that implements the common behavior.
The point here isn't to list every strategy known. In fact, you can employ dozens of similar refactoring strategies. Rather, the point is to use well-known patterns that can help you to reduce cyclomatic complexity and decrease coupling, making your code easier to maintain[md]and therefore less likely to be a source of defects.