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 })); // 1800from dataclasses import dataclass
FREE_MINUTES = 30
PER_MINUTE_CENTS = 15
RIDE_CAP_CENTS = 1800
@dataclass(frozen=True)
class Ride:
minutes: int
class FareCalculator:
def fare_in_cents(self, ride: Ride) -> int:
if self._is_free_ride(ride):
return 0
fare = self._billable_minutes(ride) * PER_MINUTE_CENTS
return self._apply_cap(fare)
def _is_free_ride(self, ride: Ride) -> bool:
return self._within_free_window(ride.minutes)
def _within_free_window(self, minutes: int) -> bool:
return minutes <= FREE_MINUTES
def _billable_minutes(self, ride: Ride) -> int:
return ride.minutes - FREE_MINUTES
def _apply_cap(self, fare: int) -> int:
return min(fare, RIDE_CAP_CENTS)
if __name__ == "__main__":
calculator = FareCalculator()
print(calculator.fare_in_cents(Ride(25))) # 0
print(calculator.fare_in_cents(Ride(52))) # 330
print(calculator.fare_in_cents(Ride(300))) # 1800<?php
declare(strict_types=1);
final class Ride
{
public function __construct(public readonly int $minutes)
{
}
}
final class FareCalculator
{
private const FREE_MINUTES = 30;
private const PER_MINUTE_CENTS = 15;
private const RIDE_CAP_CENTS = 1800;
public function fareInCents(Ride $ride): int
{
if ($this->isFreeRide($ride)) {
return 0;
}
$fare = $this->billableMinutes($ride) * self::PER_MINUTE_CENTS;
return $this->applyCap($fare);
}
private function isFreeRide(Ride $ride): bool
{
return $this->withinFreeWindow($ride->minutes);
}
private function withinFreeWindow(int $minutes): bool
{
return $minutes <= self::FREE_MINUTES;
}
private function billableMinutes(Ride $ride): int
{
return $ride->minutes - self::FREE_MINUTES;
}
private function applyCap(int $fare): int
{
return min($fare, self::RIDE_CAP_CENTS);
}
}
$calculator = new FareCalculator();
echo $calculator->fareInCents(new Ride(25)), PHP_EOL; // 0
echo $calculator->fareInCents(new Ride(52)), PHP_EOL; // 330
echo $calculator->fareInCents(new Ride(300)), PHP_EOL; // 1800After
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 })); // 1800from dataclasses import dataclass
FREE_MINUTES = 30
PER_MINUTE_CENTS = 15
RIDE_CAP_CENTS = 1800
@dataclass(frozen=True)
class Ride:
minutes: int
class FareCalculator:
def fare_in_cents(self, ride: Ride) -> int:
if ride.minutes <= FREE_MINUTES:
return 0
fare = self._billable_minutes(ride) * PER_MINUTE_CENTS
return min(fare, RIDE_CAP_CENTS)
def _billable_minutes(self, ride: Ride) -> int:
return ride.minutes - FREE_MINUTES
if __name__ == "__main__":
calculator = FareCalculator()
print(calculator.fare_in_cents(Ride(25))) # 0
print(calculator.fare_in_cents(Ride(52))) # 330
print(calculator.fare_in_cents(Ride(300))) # 1800<?php
declare(strict_types=1);
final class Ride
{
public function __construct(public readonly int $minutes)
{
}
}
final class FareCalculator
{
private const FREE_MINUTES = 30;
private const PER_MINUTE_CENTS = 15;
private const RIDE_CAP_CENTS = 1800;
public function fareInCents(Ride $ride): int
{
if ($ride->minutes <= self::FREE_MINUTES) {
return 0;
}
$fare = $this->billableMinutes($ride) * self::PER_MINUTE_CENTS;
return min($fare, self::RIDE_CAP_CENTS);
}
private function billableMinutes(Ride $ride): int
{
return $ride->minutes - self::FREE_MINUTES;
}
}
$calculator = new FareCalculator();
echo $calculator->fareInCents(new Ride(25)), PHP_EOL; // 0
echo $calculator->fareInCents(new Ride(52)), PHP_EOL; // 330
echo $calculator->fareInCents(new Ride(300)), PHP_EOL; // 1800Why 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.
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.
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.
Start with the innermost delegate. Replace the body of
isFreeRidewith the body ofwithinFreeWindow, substitutingride.minutesfor the parameter. Run the tests.Delete
withinFreeWindowonce nothing references it. Run the tests.Replace the call to
isFreeRide(ride)infareInCentswithride.minutes <= FREE_MINUTES. Run the tests, then deleteisFreeRide.Replace the call to
applyCap(fare)withMath.min(fare, RIDE_CAP_CENTS). Run the tests, then deleteapplyCap.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.