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)

  1. Check that the method is not polymorphic (not overridden).
  2. Find all callers of the method.
  3. Replace each call with the method body.
  4. 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
  • Less indirection
  • Duplication (if used many times)
  • Comparison

    • Inverse Refactoring: Extract Method.

    Code example

    Typescript

    Before

    After

    PHP

    Before

    After