Refactoring 010 – Extract Method Object

You have a big algorithmic method. Let’s break it.

TL;DR: Long methods are bad. Move them and break them.

Problems Addressed ๐Ÿ˜”

Related Code Smells ๐Ÿ’จ

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ii-o96s3wl4

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-v-evj3zs9

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-viii-8mn3352

https://hackernoon.com/code-smell-03-functions-are-too-long-heres-how-to-fix-that

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxiii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xlii

Context ๐Ÿ’ฌ

Sometimes, a method is so complex that Extract Method isn’t enough.


When you break a long algorithm, you end up with too many local variables.


You pass them as parameters between every new sub-method.


This “parameter pollution” signals the algorithm wants its own identity.


When you extract the method into a Method Object, you create a new class.


Local variables become private attributes of that class.


This creates a sandbox for decomposing the algorithm into tiny, cohesive steps.


The original host class stays clean and uncluttered.


You transform a rigid procedure into a testable, reusable component.


It can eventually evolve into a fullย Strategy pattern.

Steps ๐Ÿ‘ฃ

  1. Create an object to represent an invocation of the method.
  2. Move the big method to the new object.
  3. Convert the temporary variables of the method into private attributes.
  4. Break the big method in the new object by usingย https://hackernoon.com/improving-the-code-one-line-at-a-time
  5. Remove parameters from method invocation. Convert them to private attributes.

Sample Code ๐Ÿ’ป

Before ๐Ÿšจ

class BlockchainAccount {
  // ...
  public double balance() {
    String address;    
    // Very long untestable method
  }
}

After ๐Ÿ‘‰

class BlockchainAccount {
  // ...
  public double balance() {
    return new BalanceCalculator(this).netValue();
  }
}

// 1. Create an object to represent an invocation of the method
// 2. Move the big method to the new object
// 3. Convert the temporary variables 
//   of the method into private attributes
// 4. Break the big method in the new object
//   by using The Extract Method
// 5. Remove parameters from method invocation 
// by also converting them to private attributes 

class BalanceCalculator {
  private String address;
  private BlockchainAccount account;

  public BalanceCalculator(BlockchainAccount account) {
    this.account = account;
  }

  public double netValue() {
    this.findStartingBlock();
    //...
    this computeTransactions();
  }
}

Type ๐Ÿ“

[X] Semi-Automatic

Some IDEs have tools to extract a function into a method object.

Safety ๐Ÿ›ก๏ธ

This is a syntactic and structural refactoring.


You can make these changes safely with IDE tools.

Why is the Code Better? โœจ

You extract the logic into a new, testable component.

You can unit-test it or swap it for a different strategy.

How Does it Improve the Bijection? ๐Ÿ—บ๏ธ

A long method hides several real-world concepts inside one opaque procedure.


When you extract it into a dedicated object, each step gets its own name.


Code should map to the real world, as described in theย Bijection.


Every concept in the domain needs a counterpart in the code.


That’s the core idea of theย MAPPER.


The algorithm’s partial state becomes attributes, and its steps become methods.

The object’s name describes what it computes.

Tags ๐Ÿท๏ธ

  • Complexity

Level ๐Ÿ”‹

[X] Intermediate

Related Refactorings ๐Ÿ”„

https://hackernoon.com/improving-the-code-one-line-at-a-time?embedable=true

https://hackernoon.com/refactoring-037-testing-private-methods?embedable=true

Refactor with AI ๐Ÿค–

Suggested Prompt: 1. Create an object to represent an invocation of the method.2. Move the big method to the new object.3. Convert the temporary variables of the method into private attributes.4. Break the big method in the new object by using Extract Method.5. Remove parameters from method invocation by also converting them to private attributes.

Without Proper Instructions ๐Ÿ“ต

With Specific Instructions ๐Ÿ‘ฉโ€๐Ÿซ

Conclusion ๐Ÿ

The Method-Object suits cases where you use several extract methods.


These methods pass partial state among them as algorithm steps.


The method object stores these partial computations in its internal state.


A clear sign is when computations don’t relate cohesively to the host method.


You can also apply this technique toย anonymous functions.


The result is an atomic, testable method object.

See also ๐Ÿ“š

https://en.wikipedia.org/wiki/Strategy_pattern?embedable=true

https://learning.oreilly.com/library/view/smalltalk-best-practice/9780132852098/ch03.xhtml?embedable=true

https://refactoring.guru/es/replace-method-with-method-object?embedable=true

https://wiki.c2.com/?MethodObject=&embedable=true

Credits ๐Ÿ™

Image byย Manuel de la Fuenteย fromย Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.