Skip to content

Change Function Declaration: Fix a Signature Without Breaking Callers

How to rename a function or change its parameters in small, test-backed steps so every caller keeps working throughout the change.

6 min readRefactoring

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)); // 750

After

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)); // 750

Why 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 Rental every 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 Rental couples 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.

  1. 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.

  2. 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.

  3. Add storageFeeCents(rental) with a one-line body that returns fee(rental.daysStored, rental.weightKg). Nothing calls it yet. Run the tests.

  4. Change invoiceLine to call storageFeeCents(r). Run the tests.

  5. Change totalOwed the same way, then any remaining callers, running the tests after each one.

  6. Once fee has no callers left, inline its body into storageFeeCents, reading rental.daysStored and rental.weightKg in place of d and w. Run the tests.

  7. Delete fee and the comment that explained its parameters. Extract the magic number 2 into FREE_DAYS if 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.

Next guide3 of 4 · Techniques: simplifying callsRemove Flag Argument: Replace a Boolean with Explicit FunctionsRead the next one