You have a big algorithmic method. Let’s break it.
TL;DR: Long methods are bad. Move them and break them.
Problems Addressed ๐
- Lack of Testability
- Accidental Complexity
- Testing Private Methods
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 ๐ฃ
- Create an object to represent an invocation of the method.
- Move the big method to the new object.
- Convert the temporary variables of the method into private attributes.
- Break the big method in the new object by usingย https://hackernoon.com/improving-the-code-one-line-at-a-time
- 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://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