Skip to content

Inline Function

Replace a call to a small function with the function’s body, then delete the function. Use it when the name adds nothing the body did not already make obvious.

This cures needless indirection. Sometimes a helper made sense once but has shrunk to a single trivial line, or its name merely restates its one statement. Following the name to a definition that says no more than the call site is wasted effort for the reader. Inlining removes the hop. It is also a useful clean-up step: collapse a tangle of tiny helpers into one place, then re-extract better boundaries.

A ratingFor helper that just forwards to a one-line check adds a layer with no payoff. Inline it.

// Before
function deliveryFee(driver: Driver): number {
return moreThanFiveTrips(driver) ? 2 : 5;
}
function moreThanFiveTrips(driver: Driver): boolean {
return driver.trips > 5;
}
// After
function deliveryFee(driver: Driver): number {
return driver.trips > 5 ? 2 : 5;
}
  1. Confirm the function is not polymorphic — you cannot inline a method that subclasses override.
  2. Find every call site. (If there are many, reconsider; this is easiest when calls are few.)
  3. Replace each call with a copy of the function’s body, adjusting for the actual arguments.
  4. Run your tests after each replacement.
  5. When no call sites remain, delete the original function.
  6. Run the full suite once more to confirm nothing referenced it.

Inline when the body is at least as clear as the name, when a group of badly-factored helpers should be merged before re-splitting, or when indirection is getting in the reader’s way. Do not inline a function that is called from many places or whose name genuinely captures a non-obvious concept — there the name is documentation worth keeping.

The inverse is Extract Function. The two are partners: inline to collapse a poor structure, then extract to rebuild a better one.

When is Inline Function the right move?
Inline Function is the inverse of which refactoring?
Why might you deliberately inline several helpers before doing anything else?