Refactoring Quick Reference
This is a list of the various refactorings presented in the book Refactoring: Improving the Design of Existing Code (Fowler 1999). Once you've read the book, read through this list periodically
to keep your memory fresh. I've included a brief sentence to clarify those
with potentially ambiguous names.
I should make one for
Design Patterns and
OOSC-2's principles too.
Composing Methods
- Extract Method (110) turns a code fragment into a function.
- Inline Method (117) is the opposite of Extract Method (110)
- Inline Temp (119) gets rid of a temporary variable by moving the
expression to where the temp is used.
- Replace Temp with Query (120) removes a temporary variable and
instead uses a function call where the temp was used.
- Introduce Explaining Variable (124) replaces comments and complex
expressions with a temp variable that is well named. Opposite of
Inline Temp (119).
- Split Temporary Variable (128) splits a temp that is used for two
different things into two different variables.
- Remove Assignments to Parameters (131) removes assignments to
function parameters within the function.
- Replace Method with Method Object (135) moves a complex function
into its own class.
- Substitute Algorithm (139)
Moving Features Between Objects
- Move Method (142) moves a method from one class to a better
class.
- Move Field (146) moves a field (member object) from one class to a
better class.
- Extract Class (149) pulls a set of methods and fields from one class
into a new class. Similar to Extract Subclass (330).
- Inline Class (154) opposite of Extract Class (149).
- Hide Delegate (157). A class that provides access to an object of
another class instead provides the methods of that class by
delegation.
- Remove Middle Man (160) opposite of Hide Delegate (157).
- Introduce Foreign Method (162) adds a method to an untouchable class
by passing an instance of the untouchable class into the method.
- Introduce Local Extension (164) adds methods to an untouchable class
by deriving from it or by wrapping it.
Organizing Data
- Self Encapsulate Field (171) creates getters and setters for private
member objects, and uses them within the class instead of directly using
the member objects. See 206.
- Replace Data Value with Object (175) turns a simple member object
into a full-fledged class.
- Change Value to Reference (179) saves memory by sharing one copy of
an object amongst many other objects.
- Change Reference to Value (183) is the opposite of the previous.
Copies of an object are kept amongst many other objects.
- Replace Array with Object (186) converts an array which has various
fields in each entry into an object.
- Duplicate Observed Data (189) introduces a Document/View architecture
into an interactive application.
- Change Unidirectional Association to Bidirectional (197)
introduces a back pointer.
- Change Bidirectional Association to Unidirectional (200) is
the opposite of the previous.
- Replace Magic Number with Symbolic Constant (204)
- Encapsulate Field (206) adds getters and setters.
- Encapsulate Collection (208) hides a collection within a
class and provides add/remove/get methods to manipulate the collection.
- Replace Record with Data Class (217) makes a dumb data
class to represent a record structure.
- Replace Type Code with Class (218) replaces an
enumeration type with a class that has a set of global instances
of itself, one for each possible value.
- Replace Type Code with Subclasses (223) introduces a
polymorphic hierarchy to replace an enumeration. Leads to (255).
- Replace Type Code with State/Strategy (227) like 223,
but allows change at runtime.
- Replace Subclass with Fields (232) opposite of (223),
this is used when the subclasses no longer serve any real purpose.
Simplifying Conditional Expressions
- Decompose Conditional (238) is similar to Extract Method
(110). Extracts the condition, the then part, and the else part into
functions.
- Consolidate Conditional Expression (240) combines a series
of ifs into one.
- Consolidate Duplicate Conditional Fragments (243) factors
out code that is common to a then part and an else part.
- Remove Control Flag (245) replaces flags that trigger exits
with return, continue, and break.
- Replace Nested Conditional with Guard Clauses (250)
replaces nested ifs with returns.
- Replace Conditional with Polymorphism (255) replaces case
statements with polymorphism.
- Introduce Null Object (260) replaces checks for null values
with an object.
- Introduce Assertion (267) uses assertions to describe a
function's preconditions.
Making Method Calls Simpler
- Rename Method (273)
- Add Parameter (275) adds a parameter to a function.
- Remove Parameter (277) removes a parameter from a
function.
- Separate Query from Modifier (279) makes sure that getters
don't have side-effects.
- Parametrize Method (283) reduces a set of similar
functions to a single function with a parameter to differentiate
amongst the functions.
- Replace Parameter with Explicit Methods (285) opposite
of (283)
- Preserve Whole Object (288) passes an entire object to a
method instead of just selected fields.
- Replace Parameter with Method (292) reduces a parameter
list by using a value that is already available within the class.
- Introduce Parameter Object (295) reduces a parameter list
by grouping parameters into a single object.
- Remove Setting Method (300) makes an attribute read-only.
- Hide Method (303) makes a method private.
- Replace Constructor with Factory Method (304) supports
polymorphism and provides a home for the single case statement.
- Encapulate Downcast (308) hides a downcast within a method
that returns an object of the proper type.
- Replace Error Code with Exception (310) allows error-handling
to be separated from normal paths.
- Replace Exception with Test (315) provides a method the
caller can use to avoid an exception before it can happen.
Dealing with Generalization
- Pull Up Field (320) factors a common field into a
superclass.
- Pull Up Method (322) factors a common method into a
superclass.
- Pull Up Constructor Body (325) is a Java-specific refactoring
that uses delegation to implement constructor inheritance.
- Push Down Method (328) opposite of Pull Up Method (322) moves
a unique method down into a subclass.
- Push Down Field (329) opposite of Pull Up Field (320) moves
a unique field down into a subclass.
- Extract Subclass (330) creates a subclass and moves features
into it. Similar to Extract Class (149).
- Extract Superclass (336) factors common code into a
superclass. See Extract Class (149).
- Extract Interface (341) promotes decoupling and partitioning
of a class's responsiblities by extracting an interface class.
- Collapse Hierarchy (344) opposite of Extract Subclass (330)
and Extract Superclass (336) similar to Inline Class (154) combines a
subclass and a superclass into one.
- Form Template Method (345) factors out common behavior into
a superclass. See the Gang of Four's Template Method pattern.
- Replace Inheritance with Delegation (352)
- Replace Delegation with Inheritance (355)
Big Refactorings
- Tease Apart Inheritance (362) tries to tame a messy
inheritance hierarchy.
- Convert Procedural Design to Objects (368)
- Separate Domain from Presentation (370) moves domain logic
out of the UI classes. See Duplicate Observed Data (189).
- Extract Hierarchy (375) introduces polymorphism to replace
complex conditional code.
Links
<- Back to my Refactoring book review.
Copyright ©2005, Ted Felix.
Disclaimer