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.

Simplifying Conditional Expressions

Making logic easier to read.

Making Method Calls Simpler

Improving API usability.

Dealing with Generalization

Managing inheritance hierarchies.