Refactoring Patterns Overview
Refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring results in cleaner code, which is easier to read and maintain.
Table of Contents
Composing Methods
Simplifying methods and removing duplication.
- Extract Method: Turn a code fragment into a method with a name that explains the purpose of the method.
- Inline Method: Put the method’s body into the body of its callers and remove the method.
- Extract Variable: Put the result of an expression, or parts of it, into a temporary variable with a name that explains the purpose.
- Inline Temp: Replace references to a temporary variable with the expression itself.
- Replace Temp with Query: Move the entire expression not into a variable, but into a method.
- Split Temporary Variable: Use a different temporary variable for each assignment if a variable is assigned to more than once.
Moving Features Between Objects
Placing responsibilities in the correct classes.
- Move Method: Move a method to the class that uses it most.
- Move Field: Move a field to the class that uses it most.
- Extract Class: Move the relevant fields and methods to a new class.
- Inline Class: Move a class’s features into another class and remove it.
- Hide Delegate: create a method in the client class that calls the delegate object.
- Remove Middle Man: Force the client to call the delegate directly.
Organizing Data
Handling data structures and state easier.
- Self Encapsulate Field: Access a field via getters and setters.
- Replace Data Value with Object: Replace a data item with an object.
- Change Value to Reference: Turn a value object into a reference object.
- Replace Array with Object: Replace an array with an object that has separate fields for each element.
- Encapsulate Collection: Make a read-only alias for the collection to prevent clients from modifying it.
- Replace Type Code with Class: Replace a type code with a class.
Simplifying Conditional Expressions
Making logic easier to read.
- Decompose Conditional: Decompose the complicated parts of the conditional into separate methods.
- Consolidate Conditional Expression: Combine multiple conditionals that lead to the same result.
- Consolidate Duplicate Conditional Fragments: Move the code that is identical in all branches of the conditional to outside of the conditional.
- Replace Nested Conditional with Guard Clauses: Use guard clauses for all the special cases.
- Replace Conditional with Polymorphism: Move each leg of the conditional to an overriding method in a subclass.
Making Method Calls Simpler
Improving API usability.
- Rename Method: Rename a method to better explain what it does.
- Add Parameter: Add a parameter that allows passing more data.
- Remove Parameter: Remove a parameter that is no longer used.
- Separate Query from Modifier: Separate the method into two methods: one for query and one for modification.
- Parameterize Method: Replace a method with a parameter.
- Replace Parameter with Explicit Methods: Replace a parameter with explicit methods.
- Preserve Whole Object: Pass the whole object instead of parts of it.
- Replace Parameter with Method Call: invoke the object’s method inside the method body.
Dealing with Generalization
Managing inheritance hierarchies.
- Pull Up Field: Move a field to the superclass.
- Pull Up Method: Move a method to the superclass.
- Push Down Field: Move a field to a subclass.
- Push Down Method: Move a method to a subclass.
- Extract Interface: Extract common methods to an interface.
- Collapse Hierarchy: Merge a subclass and superclass.
- Form Template Method: Implement the skeletal algorithm in a method in the superclass.
- Replace Inheritance with Delegation: Place the object in a field and delegate operations to it.
- Replace Delegation with Inheritance: Make the delegating class inherit from the delegate.