Page 158 -
P. 158
Many programmers think of coding as a purely constructive task, for which the only reason
to add, remove, or change the source code is to alter the behavior of the software. Refactor-
ing introduces a new concept: adding, removing, or changing the source code for the sole
purpose of making it easier to maintain. There are many different refactorings, or techniques,
through which programmers can alter their code to make it easier to understand.
Refactoring is a new way of thinking about software design. Traditionally, software is
designed first and then built. This is especially true of object-oriented programming,
where the programmers might be handed a complex object model to implement. But most
programmers who have worked on a reasonably complex project have run across
instances when they discover ways that an object could have been designed better. They
could not have predicted most of these improvements because they only became apparent
during the construction of the code. Refactoring provides them with a way to incorporate
those improvements in a structured, repeatable manner.
Because each refactoring is a change to the design, it may impact the design review pro-
cess. If the software design has already been reviewed by project team members, then any
changes that arise from refactoring activities should be communicated to the people who
reviewed it. This does not necessarily mean that design specification must be reinspected
after each refactoring; since refactoring changes the design without altering the function-
ality, it is usually sufficient to distribute just the changes to the design and have the team
members approve those changes. In general, people do not object very often to refactor-
ing, but they appreciate being given the opportunity to discuss it and suggest alternatives.
Each refactoring has a set of steps—similar to the scripts we use to describe the tools in this
book—which makes it much less likely for the programmer to introduce defects. It also
has two design patterns that show what the code looks like before and after the refactor-
ing. There are dozens of refactorings, each with its own particular pattern and steps. The
example below demonstrates four of these refactorings: Extract Method, Replace Magic
Number with Symbolic Constant, Decompose Conditional, and Introduce Explaining Variable. A
comprehensive catalog of refactorings can be found at http://www.refactoring.com/catalog.
Refactoring Example
In this example, a programming team in an investment bank is reviewing a block of code
for a feature that calculates fees and bonuses for brokers who sell a certain kind of invest-
ment account to their corporate clients. The programmer who wrote the code uses refac-
toring to clarify problems that were identified during the code review.
The inspection team performed a code review on this block of Java code, which included
the class Account and a function calculateFee from a different class:
1 class Account {
2 float principal;
3 float rate;
4 int daysActive;
5 int accountType;
6
7 public static final int STANDARD = 0;
8 public static final int BUDGET = 1;
150 CHAPTER SEVEN