Inline Method
Replace a method call with the method’s body.
“A method’s body is just as clear as its name.” – Martin Fowler
Reverse of Extract Method.
graph LR
A[Function Call] -->|Refactoring| B[Code Block]
When to apply (Smells)
- Poltergeist Methods: Variable name is as clear as the function name.
- Indirection: Too much delegation makes code hard to follow.
Motivation
- Sometimes a method is just a wrapper that adds no value.
- “Indirection is good, but too much of it is confusing.”
Mechanics (Steps)
- Check that the method is not polymorphic (not overridden).
- Find all callers of the method.
- Replace each call with the method body.
- Delete the method.
Explanation
Problem
A method’s body is more obvious than its own name.
Solution
Replace calls to the method with the method’s content and delete the method itself.
Real world analogy
If you have a task “Go to Store” that just consists of “Walk out door”, you don’t need a separate checklist item for it. Just say “Walk out door”.
Pros and Cons
| Pros | Cons |
|---|---|
Comparison
- Inverse Refactoring: Extract Method.
Code example
Typescript
Before
After
PHP
Before
After