Inline Function
Intent
Section titled “Intent”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.
The smell
Section titled “The smell”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.
Before → After
Section titled “Before → After”A ratingFor helper that just forwards to a one-line check adds a layer with no payoff. Inline it.
// Beforefunction deliveryFee(driver: Driver): number { return moreThanFiveTrips(driver) ? 2 : 5;}
function moreThanFiveTrips(driver: Driver): boolean { return driver.trips > 5;}
// Afterfunction deliveryFee(driver: Driver): number { return driver.trips > 5 ? 2 : 5;}# Beforedef delivery_fee(driver): return 2 if more_than_five_trips(driver) else 5
def more_than_five_trips(driver): return driver.trips > 5
# Afterdef delivery_fee(driver): return 2 if driver.trips > 5 else 5// Beforefunc DeliveryFee(driver Driver) int { if moreThanFiveTrips(driver) { return 2 } return 5}
func moreThanFiveTrips(driver Driver) bool { return driver.Trips > 5}
// Afterfunc DeliveryFee(driver Driver) int { if driver.Trips > 5 { return 2 } return 5}// Beforefn delivery_fee(driver: &Driver) -> i32 { if more_than_five_trips(driver) { 2 } else { 5 }}
fn more_than_five_trips(driver: &Driver) -> bool { driver.trips > 5}
// Afterfn delivery_fee(driver: &Driver) -> i32 { if driver.trips > 5 { 2 } else { 5 }}Mechanics
Section titled “Mechanics”- Confirm the function is not polymorphic — you cannot inline a method that subclasses override.
- Find every call site. (If there are many, reconsider; this is easiest when calls are few.)
- Replace each call with a copy of the function’s body, adjusting for the actual arguments.
- Run your tests after each replacement.
- When no call sites remain, delete the original function.
- Run the full suite once more to confirm nothing referenced it.
When to use / trade-offs
Section titled “When to use / trade-offs”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.