This guide provides a structured introduction to the book:
Refactoring: Improving the Design of Existing Code.
– Martin Fowler. Addison-Wesley Publishing Co., Inc., 1999. ISBN 0-201-48567-2.
Study Group Goals
For an introduction to study groups, please see
A Learning Guide to Design Patterns, by Joshua Kerievsky
For this study group on Refactoring, we want to:
- understand the primary refactorings enough to apply them in our professional development practice,
- be familiar enough with the secondary refactorings to determine when they may be appropriately applied,
- be conversant with the principles of refactoring enough to address their nature (what), value (why), opportunity (when), especially when challenged by those unfamiliar with refactoring (esp. managers).
Proposed Discussions
The front inside cover of Refactoring provides an indexed list of the refactorings. The back inside cover provides a list of the refactorings organized by Bad Smell. The proposed discussion order intends to quickly introduce participants to the primary concepts and techniques.
After the initial introductory sessions, we will systematically examine the most commonly used refactorings as organized by their associated Bad Smells. Some of the refactorings appear in multiple Smells, but revisiting and reviewing them in another context will help to reinforce their usefulness.
The sessions are generally organized to cover the material in a single meeting. However, some sessions may spill over across more than one week if the discussions get lively.
| Principles (53-73) | Session 1 |
| Motivates the discussions. It’s all about product quality and developer productivity. This initial session will be used primarily to discuss the several topics in this chapter and answer questions about them. So, bring questions! | |
| Testing (89-102) | Session 2 |
| Software engineering entails testing, especially regression testing. Refactoring is founded on regression testing to ensure that externally observable behavior is maintained during and after refactoring. This session will be used to discuss testing in general, but will focus on regression testing, the role it plays in refactoring, and regression test frameworks like JUnit. | |
| Example (1-52) | Sessions 3 + 4 |
| During these two sessions we consider how the following refactorings apply to the example in Chapter 1: Extract Method (110), Move Method (142), Replace Temp with Query (120), Self Encapsulate Field (171), Replace Type Code with State/Strategy (227), Replace Conditional with Polymorphism (255). | |
| Duplicated Code (76) | Session 5 |
| Duplicated code is likely the single most prevalent “sin” in software. Extract Method (110), Extract Class (149), Pull Up Method (322), Form Template Method (345), Substitute Algorithm (139) | |
| Long Method (76) | Session 6 |
| Smaller methods enhance reuse, understandability and maintainability. Extract Method (110), Replace Temp with Query (120), Replace Method with Method Object (135), Decompose Conditional (238) | |
| Long Parameter List (78) | Session 7 |
| Object-oriented method signatures are generally simple. Long parameter lists should usually be simplified by using existing objects or introducing new ones. Replace Parameter with Method (292), Introduce Parameter Object (295), Preserve Whole Object (288) | |
| Large Class (78) | Session 8 |
| Classes should generally focus on only a few responsibilities. Classes that know or do too much should be decomposed into simpler classes. Extract Class (149), Extract Subclass (330), Extract Superclass (336), Extract Interface (341), Replace Data Value with Object (175) | |
| Alternative Classes (85) | |
| After a rapid construction phase, some method names may not adequately reveal their intention, or a method may not have an optimal placement within a collaboration. Rename Method (273), Move Method (142), Extract Superclass (336) | |
| Inappropriate Intimacy (85) | Session 9 |
| Familiarity breeds consent. Too much intimacy results in strong coupling and unwarranted dependency. Move Method (142), Move Field (146), Change Bidirectional Association to Unidirectional (200), Replace Inheritance with Delegation (352), Hide Delegate (157) | |
| Switch Statements (82) | Sessions 10 + 11 |
| Polymorphism is an elegant mechanism for separating cases. Replace Conditional with Polymorphism (255), Replace Type Code with Subclasses (223), Replace Type Code with State/Strategy (227), Replace Parameter with Explicit Methods (285), Introduce Null Object (260) | |
| Primitive Obsession (81) | Session 12 + 13 |
| Coherent groups of primitive values and functions that operate on them should be modeled as objects. Replace Data Value with Object (175), Extract Class (149), Introduce Parameter Object (295), Replace Array with Object (186), Replace Type Code with Class (218), Replace Type Code with Subclasses (223), Replace Type Code with State/Strategy (227) | |
Study Questions
Disclaimer: These questions can and should be augmented and / or replaced by questions raised by the participating study group members.
| Principles (53-73) | Session 1 |
|
|
| Testing (89-102) | Session 2 |
|
|
| Example (1-52) | Sessions 3 + 4 |
|
|
| Duplicated Code (76) | Session 5 |
|
|
| Long Method (76) | Session 6 |
|
|
| Long Parameter List (78) | Session 7 |
|
|
| Large Class (78) and Alternative Classes (85) | Session 8 |
|
|
| Inappropriate Intimacy (85) | Session 9 |
|
|
| Switch Statements (82) | Sessions 10 + 11 |
|
|
| Primitive Obsession (81) | Session 12 |
|
|
Supplemental Readings
The following books are recommended as supplemental reading for Refactoring.
Design Patterns: Elements of Reusable Object-Oriented Software.
– E. Gamma, R. Helm, R. Johnson, J. Vlissides. Addison-Wesley Publishing Co., Inc., 1995. ISBN 0-201-63361-2.
Provides an excellent introduction to design patterns and a catalog of solutions to common design problems.
Refactoring to Patterns.
– J. Kerievsky. Industrial Logic, Inc., 2001.
Supplements the works on design patterns and refactoring by connecting these two activities (design and design improvement).
Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns.
– K.J. Lieberherr. PWS Publishing Co., 1996. ISBN 0-534-94602-X.
Adaptive programming specifies the connections between objects as loosely as possible and defers the binding of algorithms to data structures until as late as possible. The Demeter system provides tools that support adaptive programming.
Object-Oriented Design Heuristics.
– A.J. Riel. Addison-Wesley Publishing Co., Inc., 1996. ISBN 0-201-63385-X.
Riel provides a fairly comprehensive set of 61 heuristics for object-oriented design. Unfortunately, the examples are all written in C++. Still, many of the heuristics are worth considering and easily transfered to Java development.
Object-Oriented Design Measurement.
– S.A. Whitmire. John Wiley & Sons, Inc., 1997. ISBN 0-471-13417-1.
While the title of this book indicates a focus on object-oriented designs, it also provides metrics applicable to software designs in general. It provides an excellent introduction to the elements of measurement theory, enough to generate a wide range of measurements tailored to specific needs. If you have a metrics-oriented manager or simply want more objective ways of evaluating your designs, this book is highly recommended.
Elements of Software Science.
– M.H. Halstead. Elsevier North-Holland, Inc., 1977. ISBN 0-444-00205-7.
Halstead pioneered the field of software metrics. This treatise still serves as a useful guide for thinking about and working with fundamental elements software elements such as operands, operators, program length, program volume, vocabulary size, language level, effort, difficulty, and predictive error rates.