Change Function Declaration covers any change to a function's name or parameter list: renaming it, adding or removing a parameter, or swapping loose values for the object they came from. The name and parameters are how readers understand a function without opening it, so a bad declaration costs you every time someone calls it.
This guide uses an invented parcel-locker service, LockerLane, which charges storage fees when customers leave parcels in a locker too long. You will rename a cryptic fee(d, w) to storageFeeCents(rental) without a single broken build along the way.
The problem
A situation where the obvious design starts to hurt.
LockerLane's billing module has a function called fee(d, w). The name doesn't say what kind of fee it is or what unit it returns, and the parameters are single letters, so a comment has to explain them. Every caller holds a Rental and pulls out daysStored and weightKg just to pass them along, and those two values always travel together.
The pricing team now wants to add rules based on other rental fields. With the current signature, each new rule means adding another positional argument and editing every call site at once. In an interview, you'd point to this kind of cryptic, high-churn signature as the place to start.
Before and after
The same code, before and after the change.
Before
interface Rental {
lockerId: string;
daysStored: number;
weightKg: number;
}
// d = days stored, w = parcel weight in kg; returns cents
function fee(d: number, w: number): number {
const base = d <= 2 ? 0 : (d - 2) * 150;
const heavy = w > 20 ? 300 : 0;
return base + heavy;
}
function invoiceLine(r: Rental): string {
return `${r.lockerId}: ${fee(r.daysStored, r.weightKg)} cents`;
}
function totalOwed(rentals: Rental[]): number {
return rentals.reduce((sum, r) => sum + fee(r.daysStored, r.weightKg), 0);
}
const rentals: Rental[] = [
{ lockerId: "L-104", daysStored: 5, weightKg: 22 },
{ lockerId: "L-207", daysStored: 1, weightKg: 3 },
];
console.log(invoiceLine(rentals[0])); // L-104: 750 cents
console.log(totalOwed(rentals)); // 750from dataclasses import dataclass
@dataclass
class Rental:
locker_id: str
days_stored: int
weight_kg: float
# d = days stored, w = parcel weight in kg; returns cents
def fee(d, w):
base = 0 if d <= 2 else (d - 2) * 150
heavy = 300 if w > 20 else 0
return base + heavy
def invoice_line(r):
return f"{r.locker_id}: {fee(r.days_stored, r.weight_kg)} cents"
def total_owed(rentals):
return sum(fee(r.days_stored, r.weight_kg) for r in rentals)
rentals = [Rental("L-104", 5, 22), Rental("L-207", 1, 3)]
print(invoice_line(rentals[0])) # L-104: 750 cents
print(total_owed(rentals)) # 750<?php
final class Rental
{
public function __construct(
public readonly string $lockerId,
public readonly int $daysStored,
public readonly float $weightKg,
) {}
}
// $d = days stored, $w = parcel weight in kg; returns cents
function fee(int $d, float $w): int
{
$base = $d <= 2 ? 0 : ($d - 2) * 150;
$heavy = $w > 20 ? 300 : 0;
return $base + $heavy;
}
function invoiceLine(Rental $r): string
{
return "{$r->lockerId}: " . fee($r->daysStored, $r->weightKg) . " cents";
}
function totalOwed(array $rentals): int
{
return array_sum(array_map(fn (Rental $r) => fee($r->daysStored, $r->weightKg), $rentals));
}
$rentals = [new Rental('L-104', 5, 22), new Rental('L-207', 1, 3)];
echo invoiceLine($rentals[0]), PHP_EOL; // L-104: 750 cents
echo totalOwed($rentals), PHP_EOL; // 750After
interface Rental {
lockerId: string;
daysStored: number;
weightKg: number;
}
const FREE_DAYS = 2;
function storageFeeCents(rental: Rental): number {
const base = rental.daysStored <= FREE_DAYS ? 0 : (rental.daysStored - FREE_DAYS) * 150;
const heavy = rental.weightKg > 20 ? 300 : 0;
return base + heavy;
}
function invoiceLine(r: Rental): string {
return `${r.lockerId}: ${storageFeeCents(r)} cents`;
}
function totalOwed(rentals: Rental[]): number {
return rentals.reduce((sum, r) => sum + storageFeeCents(r), 0);
}
const rentals: Rental[] = [
{ lockerId: "L-104", daysStored: 5, weightKg: 22 },
{ lockerId: "L-207", daysStored: 1, weightKg: 3 },
];
console.log(invoiceLine(rentals[0])); // L-104: 750 cents
console.log(totalOwed(rentals)); // 750from dataclasses import dataclass
FREE_DAYS = 2
@dataclass
class Rental:
locker_id: str
days_stored: int
weight_kg: float
def storage_fee_cents(rental):
base = 0 if rental.days_stored <= FREE_DAYS else (rental.days_stored - FREE_DAYS) * 150
heavy = 300 if rental.weight_kg > 20 else 0
return base + heavy
def invoice_line(r):
return f"{r.locker_id}: {storage_fee_cents(r)} cents"
def total_owed(rentals):
return sum(storage_fee_cents(r) for r in rentals)
rentals = [Rental("L-104", 5, 22), Rental("L-207", 1, 3)]
print(invoice_line(rentals[0])) # L-104: 750 cents
print(total_owed(rentals)) # 750<?php
const FREE_DAYS = 2;
final class Rental
{
public function __construct(
public readonly string $lockerId,
public readonly int $daysStored,
public readonly float $weightKg,
) {}
}
function storageFeeCents(Rental $rental): int
{
$base = $rental->daysStored <= FREE_DAYS ? 0 : ($rental->daysStored - FREE_DAYS) * 150;
$heavy = $rental->weightKg > 20 ? 300 : 0;
return $base + $heavy;
}
function invoiceLine(Rental $r): string
{
return "{$r->lockerId}: " . storageFeeCents($r) . " cents";
}
function totalOwed(array $rentals): int
{
return array_sum(array_map(fn (Rental $r) => storageFeeCents($r), $rentals));
}
$rentals = [new Rental('L-104', 5, 22), new Rental('L-207', 1, 3)];
echo invoiceLine($rentals[0]), PHP_EOL; // L-104: 750 cents
echo totalOwed($rentals), PHP_EOL; // 750Why it's worth it
What the change buys you.
A function's declaration is its interface. Callers read storageFeeCents(rental) and know what it computes, what unit comes back, and what it needs, so the comment that decoded d and w has nothing left to explain.
Passing the Rental instead of two of its fields removes a data clump. Every caller already had the object, so the call sites get shorter, and future pricing rules that read other rental fields won't touch any caller.
The key is how you make the change. A small, private function can be renamed in one step with your IDE. When there are many callers, or callers you don't control, use the migration route instead: add the new function next to the old one, move callers across one at a time, then delete the old one. The code compiles and the tests pass after every step, so you can stop halfway, merge, and finish tomorrow. That ability to pause safely is what interviewers want to hear about.
- The name says what the function returns and in what unit, so callers don't need to read the body or a comment.
- Call sites stop unpacking the same two fields from
Rentalevery time. - New pricing rules based on other rental fields only change one function, not every caller.
- Every intermediate step builds and passes tests, so you can ship the change in several small commits.
Careful with
- Taking the whole
Rentalcouples the function to that type. If some callers only have a days count and a weight, such as a price-quote screen, keep primitive parameters or give them a small type of their own. - For published APIs, keep the old function as a deprecated wrapper until every external caller has moved. Deleting it early breaks code you can't see.
- In Python and PHP, a missed caller fails at runtime, not compile time. Search for dynamic calls and string references before you delete the old name.
- Don't mix behaviour changes into this refactoring. Keep the 150-cent rate and the 300-cent surcharge exactly as they were until the signature change is merged.
Step by step
Small, safe steps, with the tests passing after each.
Make sure tests cover the current behaviour, including a rental inside the free period and a heavy parcel over 20 kg. Run them and confirm they pass.
Find every caller of
fee, including other modules, scripts and anything outside your repository. If outside code calls it, plan to keep the old function around for a while as a deprecated wrapper.Add
storageFeeCents(rental)with a one-line body that returnsfee(rental.daysStored, rental.weightKg). Nothing calls it yet. Run the tests.Change
invoiceLineto callstorageFeeCents(r). Run the tests.Change
totalOwedthe same way, then any remaining callers, running the tests after each one.Once
feehas no callers left, inline its body intostorageFeeCents, readingrental.daysStoredandrental.weightKgin place ofdandw. Run the tests.Delete
feeand the comment that explained its parameters. Extract the magic number 2 intoFREE_DAYSif you like. Run the tests one final time.
Smells it removes
Smells this refactoring helps remove.
Common questions
Short answers to the usual doubts.
When can I skip the migration steps and just rename?
When the function is private or only used in your codebase, and your IDE or compiler can update every call site reliably. For a TypeScript function with a handful of callers, an automated rename plus a test run is enough. Use the migration route when there are many callers, dynamic calls, or code you don't control.
Why does the new function call the old one first, not the other way round?
The old function only receives days and weight, so it can't build a full Rental to pass on. The new function has the whole object and can unpack it to call the old one. That direction keeps both versions valid while you move callers, and you inline the body at the end.
How is this different from Introduce Parameter Object?
Introduce Parameter Object creates a new type to group values that travel together. Here the Rental type already existed, so you only changed the declaration to accept it. Both remove data clumps, but this one doesn't add any new type.
How would I explain this in an interview?
Name the problem first: a cryptic name, a comment explaining the parameters, and callers unpacking the same fields. Then describe the migration: add the new function, delegate to the old one, move callers one at a time with tests after each, inline, and delete. Mention that you'd keep a deprecated wrapper for external callers.