Skip to content

Inline Function: Remove Indirection That Adds Nothing

Learn how to safely inline a function whose body is as clear as its name, with step-by-step guidance and examples in TypeScript, Python and PHP.

6 min readRefactoring

Extracting small, well-named functions is usually good advice. Sometimes, though, the extraction goes one step too far. A function ends up wrapping a single expression that already reads as clearly as its name, or it just forwards a call to another function. Inline Function is the reverse of Extract Function. You replace each call with the function's body, then delete the function. It is a small refactoring, but interviewers like it because it shows you know that indirection has a cost and that you can judge when a name earns its place.

The problem

A situation where the obvious design starts to hurt.

Picture the fare calculator for Ridgeline Bikes, a city bike-sharing service. Rides of 30 minutes or less are free. After that each extra minute costs 15 cents, and a single ride never costs more than 18 dollars.

Over several refactoring passes the calculator picked up helpers like isFreeRide, which only calls withinFreeWindow, which only compares a number against FREE_MINUTES. There is also applyCap, which only wraps Math.min. To follow fareInCents you have to jump through three private methods, and none of them tells you anything the expression ride.minutes <= FREE_MINUTES would not. The chain of delegation hides a simple rule behind several names that mean the same thing.

Before and after

The same code, before and after the change.

Before

const FREE_MINUTES = 30;
const PER_MINUTE_CENTS = 15;
const RIDE_CAP_CENTS = 1800;

interface Ride {
  minutes: number;
}

export class FareCalculator {
  fareInCents(ride: Ride): number {
    if (this.isFreeRide(ride)) {
      return 0;
    }
    const fare = this.billableMinutes(ride) * PER_MINUTE_CENTS;
    return this.applyCap(fare);
  }

  private isFreeRide(ride: Ride): boolean {
    return this.withinFreeWindow(ride.minutes);
  }

  private withinFreeWindow(minutes: number): boolean {
    return minutes <= FREE_MINUTES;
  }

  private billableMinutes(ride: Ride): number {
    return ride.minutes - FREE_MINUTES;
  }

  private applyCap(fare: number): number {
    return Math.min(fare, RIDE_CAP_CENTS);
  }
}

const calculator = new FareCalculator();
console.log(calculator.fareInCents({ minutes: 25 }));  // 0
console.log(calculator.fareInCents({ minutes: 52 }));  // 330
console.log(calculator.fareInCents({ minutes: 300 })); // 1800

After

const FREE_MINUTES = 30;
const PER_MINUTE_CENTS = 15;
const RIDE_CAP_CENTS = 1800;

interface Ride {
  minutes: number;
}

export class FareCalculator {
  fareInCents(ride: Ride): number {
    if (ride.minutes <= FREE_MINUTES) {
      return 0;
    }
    const fare = this.billableMinutes(ride) * PER_MINUTE_CENTS;
    return Math.min(fare, RIDE_CAP_CENTS);
  }

  private billableMinutes(ride: Ride): number {
    return ride.minutes - FREE_MINUTES;
  }
}

const calculator = new FareCalculator();
console.log(calculator.fareInCents({ minutes: 25 }));  // 0
console.log(calculator.fareInCents({ minutes: 52 }));  // 330
console.log(calculator.fareInCents({ minutes: 300 })); // 1800

Why it's worth it

What the change buys you.

Every function call asks the reader to jump somewhere else and come back. That is worth it when the name explains something the code cannot, such as intent, a domain rule or a calculation with several steps. It is not worth it when the name only restates the body. isFreeRide added a second hop to withinFreeWindow, and both said exactly what ride.minutes <= FREE_MINUTES says, since the constant already carries the meaning. applyCap was a thin label on Math.min next to a constant called RIDE_CAP_CENTS.

After inlining, the whole pricing rule fits on one screen and reads top to bottom: free under the window, per-minute charge, cap. Notice what stayed. billableMinutes is kept because the subtraction encodes a real rule (the free window is deducted, not just a threshold), and the name makes that explicit.

Inlining is also a useful intermediate step. When a group of functions has been split badly, you can inline them all back into one body and then extract again along better lines.

  • The full pricing rule is visible in one method without jumping between definitions.
  • Fewer names to maintain, so a rename or rule change touches less code.
  • Removes delegation chains where one function only forwards to another.
  • Clears the ground for a better extraction if the original split was in the wrong place.

Careful with

  • Do not inline a method that subclasses override or that implements an interface. The call site depends on dynamic dispatch, and the body you copy is only one of the variants.
  • If the function is public or exported, other modules may call it. Find every caller first, or keep a deprecated wrapper until they migrate.
  • Watch for arguments with side effects or costly evaluation. If a parameter is used twice in the body, assign the argument to a local variable first so it is evaluated only once, as before.
  • Recursive functions and functions with several early returns are harder to inline correctly. Simplify the body first or leave it alone.

Step by step

Small, safe steps, with the tests passing after each.

  1. Check that the function is not overridden in a subclass or implemented through an interface. If it is polymorphic, inlining it would change behaviour, so stop.

  2. Make sure tests cover the calling code, including edge cases like a ride of exactly 30 minutes and a ride long enough to hit the cap.

  3. Start with the innermost delegate. Replace the body of isFreeRide with the body of withinFreeWindow, substituting ride.minutes for the parameter. Run the tests.

  4. Delete withinFreeWindow once nothing references it. Run the tests.

  5. Replace the call to isFreeRide(ride) in fareInCents with ride.minutes <= FREE_MINUTES. Run the tests, then delete isFreeRide.

  6. Replace the call to applyCap(fare) with Math.min(fare, RIDE_CAP_CENTS). Run the tests, then delete applyCap.

  7. Reread the method and decide whether any remaining helper, such as billableMinutes, still earns its name. Keep it if it does.

Smells it removes

Smells this refactoring helps remove.

Common questions

Short answers to the usual doubts.

How do I decide whether a small function should stay or be inlined?

Ask whether the name tells the reader something the body does not. billableMinutes names a domain concept, so it stays. withinFreeWindow(minutes) wrapping minutes <= FREE_MINUTES repeats what the constant already says, so it goes. Length alone is not the test: a one-line function with a meaningful name is fine.

Does inlining hurt testability?

Only if tests called the private helper directly, which is a sign they were testing implementation rather than behaviour. Tests against fareInCents with inputs of 25, 30, 31 and 300 minutes cover everything the helpers did, and they keep passing through the refactoring.

Is there any performance gain from inlining?

Rarely anything you can measure. JIT compilers and modern runtimes already inline small hot functions. Make this change for readability, not speed, and say so in an interview.

How is this different from Remove Middle Man?

Remove Middle Man works at the class level: a class that mostly forwards calls to another object, so clients should talk to the delegate directly. Inline Function works on a single function. The motivation is the same, which is to cut indirection that adds no meaning.

What should I say if an interviewer asks why I would undo an extraction?

Explain that extraction and inlining are two halves of the same judgement. You extract when a name clarifies, and you inline when the name only adds a hop. Mention that you check for polymorphism and external callers before inlining and run tests after each call site changes.

Next guide3 of 7 · Techniques: composing functionsExtract Variable: Give a Complex Expression a NameRead the next one