As most software developers know, software code has become more and more difficult to manage, and even to understand, as more features are added, bugs are fixed, and maintenance is done by different developers.
In many development projects, the money spent to maintain and extend code is more important than the funds dedicated to its development. This is because most developers write code that produces the right output for a given input, without paying attention to clean design and presentation.
Refactoring is about improving the internal structure of existing code without altering its external behavior. In practice, refactoring is a set of program transformations applied to a software system, in order to clean up code without introducing bugs.
This book builds on the foundations laid by the pioneers of refactoring, Ward Cunningham and Kent Beck. Fowler presents a detailed discussion of refactoring techniques and provides a catalog of 72 proven refactorings, with step-by-step instructions for applying each.
In chapter 1, the author presents an introductory example of refactoring. He performs a step-by-step improvement of a sample program by applying some of the techniques detailed in further chapters. In chapter 2, the author discusses cases that illustrate when refactoring existing code becomes a necessity. Chapter 3 contains a list of symptoms to identify badly written code, such as duplicated code, long methods, large classes, and data clumps. Chapter 4 demonstrates the use of JUnit, a testing framework built by Erich Gamma and Kent Beck to develop tests for code.
Chapter 5 presents the format for refactoring patterns. As with design patterns, refactorings are carefully defined using a standard format. This format specifies a name; a short summary describing the situation in which this refactoring is useful; the motivation for why the refactoring should be done and circumstances in which it shouldn’t be done; the mechanics (a step-by-step description of how to carry out the refactoring); and finally, an example that illustrates how this refactoring works.
Chapters 6 through 12 comprise an initial catalog of refactorings, categorized by their purpose. These categories include composing methods; moving features between objects; organizing data; simplifying conditional expressions; making method calls simpler; dealing with generalization; and big refactorings. Chapter 6 deals with the problem of long methods. Some refactorings are discussed that are used to package code properly. The core refactorings are Extract Method() and Inline Method(). Chapter 7 details the use of refactorings such as the Move Method(), Move Field(), and Extract Class(), used to redistribute responsibilities between objects. Chapter 8 discusses several refactorings that make working with data easier, and includes a set of illustrative examples. Chapter 9 presents the core refactorings used to simplify conditional expressions: Decompose Conditional, Consolidate Conditional Expression, and Replace Conditional with Polymorph ism. Chapter 10 presents refactorings to make method calls simpler: Rename Method, Add Parameter, Remove Parameter, and Parameterize Method.
Dealing with generalization is achieved by the mean of a set of refactorings. Chapter 11 contains the description of two of them. The Pull Up Field is applied when two subclasses have the same field. This refactoring is achieved by moving the field to the superclass. The Pull Up Method is applied when two methods have identical results on subclasses. This refactoring moves these methods to the superclass. Most of the research and practice of refactoring deals with smaller refactorings. Chapter 12 discusses big refactorings.
The final chapters are written by guest authors. Chapter 13 is by William Opdyke, who presents a brief discussion about the early experience he gained during his refactoring research, and who addresses other refactoring-related issues, such as software reuse, product evolution, and platform selection. This chapter ends with a set of resources and references for refactoring.
Manual refactoring is time consuming, and in most cases not very safe, since it can introduce bugs. Refactoring with an automated tool is thus very helpful. In chapter 14, Don Roberts and John Brant set up a list of technical requirements for a refactoring tool. In chapter 15, Kent Beck concludes the book by giving the reader guidelines for better use of refactoring.
This book is a classic reference for refactoring. The reader will notice, however, that to learn the effective use of refactoring, it is not sufficient to read the whole book, or even to learn the catalog of the seventy-two refactorings; practitioners must gain experience from intensive practice.