Refactoring: Refactorings/Replace Method with Method Object

From Notes
Jump to navigation Jump to search

Turns a method into its own object so local variables become instance variables on that object. Code then becomes much easier to refactor in other ways.

Useful in a long method that uses local variables so you can't use Extract Method.

Mechanics

  1. Create a new class. Name it after the method
  2. Give the new class an attribute for the object that once hosted the original method (source object), and an attribute for temporary variables and parameters in the method
  3. Give the new class a constructor that takes the source object and each parameter
  4. Give the new class a method named "compute"
  5. Copy the body of the original method into compute, using the source object instance variable for any reference to methods on the source object
  6. Test
  7. Replace old method with one that creates new object and calls compute.


Example

Before

class Account
  def gamma(input_val, quantity, year_to_date)
    important_value1 = (input_val * quantity) + delta
    important_value2 = (input_val * year_to_date) + 100
    if (year_to_date - important_value1) > 100
      important_value2 -= 20
    end
    important_value3 = important_value2 * 7
    # ...
    important_value3 - 2 * important_value1
  end
end

After

class Gamma
  attr_reader :account, :input_val, :quantity, :year_to_date, :important_value1, :important_value2, :important_value3

  def initialize(account, input_val_arg, quantity_arg, year_to_date_arg)
    @account = account
    @input_val = input_val_arg
    @quantity = quantity_arg
    @year_to_date = year_to_date_arg
  end

  def compute
    @important_value1 = (input_val * quantity) + @account.delta
    @important_value2 = (input_val * year_to_date) + 100
    if (year_to_date - important_value1) > 100     # now we could easily Extract Method on this
      @important_value2 -= 20
    end
    @important_value3 = important_value2 * 7
    # ...
    @important_value3 - 2 * important_value1
  end
end

class Account
  def gamma(input_val, quantity, year_to_date)
    Gamma.new(self, input_val, quantity, year_to_date).compute
  end
end
Refactorings
Composing Methods

Extract Method • Inline Method • Inline Temp • Replace Temp with Query • Replace Temp with Chain • Introduce Explaining Variable • Split Temporary Variable • Remove Assignments to Parameters • Replace Method with Method Object • Substitute Algorithm • Replace Loop with Collection Closure Method • Extract Surrounding Method • Introduce Class Annotation • Introduce Named Parameter • Remove Named Parameter • Remove Unused Default Parameter • Dynamic Method Definition • Replace Dynamic Receptor with Dynamic Method Definition • Isolate Dynamic Receptor • Move Eval from Runtime to Parse Time

Moving Features Between Objects

Move Method • Move Field • Extract Class • Inline Class • Hide Delegate • Remove Middle Man

Organizing Data

Self Encapsulate Field • Replace Data Value with Object • Change Value to Reference • Change Reference to Value • Replace Array with Object • Replace Hash with Object • Change Unidirectional Association to Bidirectional • Change Bidirectional Association to Unidirectional • Replace Magic Number with Symbolic Content • Encapsulate Collection • Replace Record with Data Class • Replace Type Code with Polymorphism • Replace Type Code with Module Extension • Replace Type Code with State-Strategy • Replace Subclass with Fields • Lazily Initialized Atribute • Eagerly Initialized Attribute

Simplifying Conditional Expressions

Decompose Conditional • Recompose Conditional • Consolidate Conditional Expression • Consolidate Duplicate Conditional Fragments • Remove Control Flag • Replace Nested Conditional with Guard Clauses • Replace Conditional with Polymorphism • Introduce Null Object • Introduce Assertion

Making Method Calls Simpler

Rename Method • Add Parameter • Remove Parameter • Separate Query from Modifier • Parameterize Method • Replace Parameter with Explicit Methods • Preserve Whole Object • Replace Parameter with Method • Introduce Parameter Object • Remove Setting Method • Hide Method • Replace Constructor with Factory Method • Replace Error Code with Exception • Replace Exception with Test • Introduce Gateway • Introduce Expression Builder

Dealing with Generalization

Pull Up Method • Push Down Method • Extract Module • Inline Module • Extract Subclass • Introduce Inheritance • Collapse Hierarchy • Form Template Method • Replace Inheritance with Delegation • Replace Delegation with Hierarchy • Replace Abstract Superclass with Module

Big Refactorings

Tease Apart Inheritance • Convert Procedural Design to Objects • Separate Domain from Presentation • Extract Hierarchy