Refactoring: Refactorings/Extract Method

From Notes
Jump to navigation Jump to search

Turn a section of code inside a long method into its own method (shortens the long method). Resulting code looks more like easy-to-understand pseudocode.

Mechanics

  1. Create a new method, named after 'what it does (not how it does it)
    Note: If code in question is simple, only extract if new method better reveals intention. If you can't come up with a more meaningful name, don't extract the code.
  2. Copy extracted code from source method into new method
  3. Scan extracted code for references to local variables. Make these variables parameters
  4. Scan extracted code for temporary variables declared in source method. If so, declare them in target method instead.
  5. If any local variables inside source method are modified by extracted code, treat extracted code as a query and assign result to said variable
    {{note|If more than 1 modified variable, Split Temporary Variable or Replace Temp with Query
  6. Create function call to target method above extracted code; don't forget necessary local-scope variables for parameters
  7. Delete extracted code in source method, replacing it with function call from previous step.
  8. Check for unused temporary variables that have been declared inside the newly extracted method and remove them.
  9. Test

Example

Before

def print_owing(previous_amount)
  outstanding = previous_amount * 1.2

  # print banner
  puts "*************************"
  puts "***** Customer Owes *****"
  puts "*************************"

  # calculate outstanding
  @orders.each do |order|
    outstanding += order.amount
  end

  # print details
  puts "name: #{@name}"
  puts "amount: #{outstanding}"
end

After

def print_owing(previous_amount)
  print_banner
  outstanding = calculate_outstanding(previous_amount * 1.2)
  print_details outstanding
end

def print_banner
  puts "*************************"
  puts "***** Customer Owes *****"
  puts "*************************"
end

def calculate_outstanding(initial_value)
  @orders.inject(initial_value) { |result, order| result + order.amount }
end

def print_details(outstanding)
  puts "name: #{@name}"
  puts "amount: #{outstanding}"
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