This chapter looks at the format of the refactorings in this book, the projects referenced in the refactorings, the maturity level of the refactorings, as well as a suggested study sequence for the catalog.

Format of the Refactorings

The format of each refactoring in this book mostly follows the format used by Martin Fowler in Refactoring [ F ], with a few of my own embellishments. Each refactoring generally has most, if not all, of the following parts.

•   Name : The name is important for building up a vocabulary of refactorings. The refactorings in this book refer to numerous refactorings in Refactoring as well as refactorings in this book.

•   Summary : Each refactoring in this book describes a design transformation. I use textual and diagrammatic descriptions to explain each transformation. I call the diagrammatic portion of the summary a sketch because it uses UML to show the essence of a design transformation. The sketches use a variety of UML diagrams, including class, object, collaboration, and sequence diagrams. The sketches rarely show every method or field within a class, as that would distract the reader from the essence of the transformation. Most of the sketches also include gray boxes that contain the names of important participants in a refactoring. For example, the diagram on the next page shows the “after” sketch from Move Embellishment to Decorator (144) .

Summary:

    The participants listed in the gray boxes, “Decorator: ConcreteDecorator” and “Decorator: ConcreteComponent,” both originate from the Participants section from the Decorator pattern in Design Patterns [ DP ]. In some cases, the participants listed in gray boxes originate from the names of classes or methods that I write about in the Mechanics section of a refactoring.

•   Motivation : This section describes why you’d want to use this refactoring. It also tends to include high-level descriptions that give a good overview of the patterns. If you need further details, it will be best to consult a book that describes the particular pattern in depth.

    At the end of the Motivation section, I include a box that lists the benefits and liabilities associated with the refactoring. A plus sign (+) signifies a benefit, while a minus sign (–) signifies a liability. Here’s an example from the refactoring Replace State-Altering Conditionals with State (166) .

Benefits and Liabilities

+  Reduces or removes state-changing conditional logic.

+  Simplifies complex state-changing logic.

+  Provides a good bird’s-eye view of state-changing logic.

–  Complicates a design when state transition logic is already easy to follow.

•   Mechanics : This section lists the step-by-step instructions for implementing the refactoring. Some Mechanics sections include a preamble that describes what ought to be in place before you begin the refactoring. All of the Mechanics sections include numbered steps, so you can easily correlate these steps with the numbered steps in the Example sections.

    You’ll find that the Mechanics sections reference many of the refactorings in Refactoring [ F ]. To fully understand the Mechanics sections in this book, I suggest that you have Refactoring close by your side.

    I also advise that you don’t treat these sections like gospel. At best, they offer a safe way to get from one design to another. If your situation demands an alternate path, don’t hesitate to try it. Also remember that you can often follow the steps in a Mechanics section part way, and if you achieve enough of a design improvement, stop there. Only implement all the steps of a Mechanics section if doing so truly improves your design.

•   Example : This section is where I go into great depth about how I used the refactoring in question to transform a design. Each step within an Example section is numbered and correlates with the numbered steps in the Mechanics section for that refactoring.

    I follow Martin Fowler’s style of using boldface code ( like this ) to highlight changes made to code during the steps of each refactoring. Like Martin, I also use a strikeout font ( like this ) to show when I’m deleting code.

•   Variations : A few refactorings in this book include a section that explains a variation on the refactoring. This section is by no means exhaustive—there are far too many variations on refactorings to describe them all. However, I do document some important ones.

Projects Referenced in This Catalog

The examples in this book either come from real-world projects or are inspired by real-world projects I’ve worked on. I use real-world code rather than toy code because when we refactor real-world code, our refactoring decisions are constrained by forces present in the code. This doesn’t happen as much with toy code. In addition, toy code, which tends to lack these forces or contain only a portion of them, never seems to offer as rich an educational experience as real-world code.

Real-world code can take a little more effort to understand than toy code. I’ve tried to make it easier to understand the real-world code in this book by removing most of the extraneous details. Yet it’s impossible to remove all rough edges in real-world code, and doing so could even diminish what can be learned from real-world refactorings.

The code used in the Example sections comes from diverse projects. Some of these projects are referenced only once, while others are referenced several times in different refactorings. The projects you’ll find referenced in multiple refactorings are described briefly in the following subsections.

XML Builders

Many information systems I’ve worked on have produced XML, and nearly all of these systems have used the very basics of XML: open and closed tags, with values and attributes. When you work with the basics of XML, it often makes little sense to create XML using a sophisticated third-party library, even if it’s free. You can easily write your own code to create XML exactly as you need it. I’ve found that my own XML code is simpler than that created by third-party tools, which must provide complex ways to create or manipulate XML.

Over the years, I’ve written code to create XML in numerous ways. In this book, you’ll find references to an XMLBuilder , a DOMBuilder , a TagBuilder , and a TagNode . The TagBuilder is my favorite XML builder; it evolved out of my work on other XML builders.

The following refactorings contain example code involved in building XML:

•   Replace Implicit Tree with Composite (178)

•   Introduce Polymorphic Creation with Factory Method (88)

•   Encapsulate Composite with Builder (96)

•   Move Accumulation to Collecting Parameter (313)

•   Unify Interfaces with Adapter (247)

HTML Parser

The HTML Parser ( http://sourceforge.net/projects/htmlparser ) is an open source library that allows programs to easily parse HTML. It is the most popular HTML parser on SourceForge ( http://sourceforge.net ) and is used by many people throughout the world. This project was initiated by Somik Raha. Somik and others on the project made sure to write many tests as they developed the parser. When I joined the project, I found areas in the code that required design improvements. So I began refactoring, often while pair-programming with Somik. This work yielded many interesting refactorings, including numerous refactorings to patterns.

The following refactorings contain examples from the parser:

•   Move Embellishment to Decorator (144)

•   Move Creation Knowledge to Factory (68)

•   Extract Composite (214)

•   Move Accumulation to Visitor (320)

Loan Risk Calculator

I spent the first eight years of my career programming for a bank on Wall Street, creating loan calculators to assess credit, market, and global risk. At the time, I even wore suits! Some of my earliest object-oriented systems were written using Turbo Pascal and C++. While I cannot show code from those systems in this book, I can show code inspired by them. For sensitive calculations, I modified the code to use calculations you might find in any financial textbook.

The following refactorings contain examples related to my work on the loan risk calculator:

•   Chain Constructors ( 340 )

•   Replace Constructors with Creation Methods (57)

•   Replace Conditional Logic with Strategy ( 129 )

A Starting Point

In a section called “How Mature Are These Refactorings?” Martin Fowler writes:

As you use the refactorings [in this book] bear in mind that they are a starting point. You will doubtless find gaps in them. I’m publishing them now because although they are not perfect, I do believe they are useful. I believe they will give you a starting point that will improve your ability to refactor efficiently. That is what they do for me. [ F , 107]

The same can be said of the refactorings in this book. They too are a starting point. With today’s evolving tools and variety of object-oriented languages, there are many ways to perform refactorings.

It’s best to treat my refactorings as a recipe that you adapt to your environment. This may mean skipping some steps in the mechanics of a refactoring, or it may mean going about the refactoring in a different way. What matters in the end is not the steps you follow but whether or not you improve the design of your code. If this book gives you useful ideas for improving your code, I’ll be happy.

This catalog does not describe all of the possible pattern-directed refactorings I could have included. After writing 27 refactorings, it seemed like it was time to ship a book. However, my hope is that other authors will help extend this catalog to document additional pattern-directed refactorings that will be useful to programmers.

A Study Sequence

To thoroughly learn the refactorings in the catalog, you’ll want to study every refactoring’s example code. Because several refactorings refer to the same project, you’ll find it easier to understand the refactorings if you follow them using the study sequence shown in Table 5.1 .

Table 5.1

Image