Rename is the refactoring you will do most often and talk about least. You change the name of a variable, parameter, function, method or class so that it says what the thing is or does. The behaviour stays the same. Only the words change. In an interview, Rename comes up in two ways. In a code review exercise, renaming cryptic identifiers is usually the cheapest improvement you can point to. In a design discussion, interviewers want to know whether you can rename a public method used by forty services without a broken deploy. This guide covers both: picking a good name, and getting it through a codebase safely.
The problem
A situation where the obvious design starts to hurt.
Picture the parcel locker service at Boxwell, a company running 1,200 self-service lockers in train stations. A parcel that stays past its grace period costs an hourly overstay fee. The fee logic lives in a class called LkrMgr with a method calc(d, n, g) and a field r.
Every name in that code is a puzzle. You can only read it because someone left comments translating d, n, g, h and x into English. Those comments are already drifting: nothing stops the next person from changing g to minutes and leaving the comment alone. Callers elsewhere see mgr.calc(a, b, 48) and have to open the class to find out what it calculates and in what unit. The real problem is that the code needs comments to explain what its names should have said.
Before and after
The same code, before and after the change.
Before
// Locker manager: works out the fee for a parcel left too long
export class LkrMgr {
// r = hourly rate in cents after the grace period
constructor(private r: number) {}
// d = drop-off epoch ms, n = pickup epoch ms, g = grace hours
calc(d: number, n: number, g: number): number {
const h = Math.ceil((n - d) / 3_600_000); // hours stored
const x = h - g; // billable hours
return x > 0 ? x * this.r : 0;
}
}
// Parcel held 50h10m, 48 free hours, 150 cents per hour
console.log(new LkrMgr(150).calc(0, 180_600_000, 48)); // 450import math
# Locker manager: works out the fee for a parcel left too long
class LkrMgr:
# r = hourly rate in cents after the grace period
def __init__(self, r: int) -> None:
self.r = r
# d = drop-off epoch ms, n = pickup epoch ms, g = grace hours
def calc(self, d: int, n: int, g: int) -> int:
h = math.ceil((n - d) / 3_600_000) # hours stored
x = h - g # billable hours
return x * self.r if x > 0 else 0
# Parcel held 50h10m, 48 free hours, 150 cents per hour
print(LkrMgr(150).calc(0, 180_600_000, 48)) # 450<?php
// Locker manager: works out the fee for a parcel left too long
final class LkrMgr
{
// r = hourly rate in cents after the grace period
public function __construct(private int $r)
{
}
// d = drop-off epoch ms, n = pickup epoch ms, g = grace hours
public function calc(int $d, int $n, int $g): int
{
$h = (int) ceil(($n - $d) / 3_600_000); // hours stored
$x = $h - $g; // billable hours
return $x > 0 ? $x * $this->r : 0;
}
}
// Parcel held 50h10m, 48 free hours, 150 cents per hour
echo (new LkrMgr(150))->calc(0, 180_600_000, 48), PHP_EOL; // 450After
export class LockerFeeCalculator {
constructor(private hourlyRateCents: number) {}
overstayFeeCents(droppedOffAtMs: number, pickedUpAtMs: number, graceHours: number): number {
const hoursStored = Math.ceil((pickedUpAtMs - droppedOffAtMs) / 3_600_000);
const billableHours = hoursStored - graceHours;
return billableHours > 0 ? billableHours * this.hourlyRateCents : 0;
}
}
// Parcel held 50h10m, 48 free hours, 150 cents per hour
console.log(new LockerFeeCalculator(150).overstayFeeCents(0, 180_600_000, 48)); // 450import math
class LockerFeeCalculator:
def __init__(self, hourly_rate_cents: int) -> None:
self.hourly_rate_cents = hourly_rate_cents
def overstay_fee_cents(
self, dropped_off_at_ms: int, picked_up_at_ms: int, grace_hours: int
) -> int:
hours_stored = math.ceil((picked_up_at_ms - dropped_off_at_ms) / 3_600_000)
billable_hours = hours_stored - grace_hours
return billable_hours * self.hourly_rate_cents if billable_hours > 0 else 0
# Parcel held 50h10m, 48 free hours, 150 cents per hour
print(LockerFeeCalculator(150).overstay_fee_cents(0, 180_600_000, 48)) # 450<?php
final class LockerFeeCalculator
{
public function __construct(private int $hourlyRateCents)
{
}
public function overstayFeeCents(int $droppedOffAtMs, int $pickedUpAtMs, int $graceHours): int
{
$hoursStored = (int) ceil(($pickedUpAtMs - $droppedOffAtMs) / 3_600_000);
$billableHours = $hoursStored - $graceHours;
return $billableHours > 0 ? $billableHours * $this->hourlyRateCents : 0;
}
}
// Parcel held 50h10m, 48 free hours, 150 cents per hour
echo (new LockerFeeCalculator(150))->overstayFeeCents(0, 180_600_000, 48), PHP_EOL; // 450Why it's worth it
What the change buys you.
A name is the only documentation that travels with every use of a symbol. A comment on calc helps the reader of that one file. The name overstayFeeCents helps every caller, every stack trace and every search result.
Good names carry three things the old ones were missing. They say what the thing is (LockerFeeCalculator, not LkrMgr). They say the unit when the type does not (Cents, Ms, Hours), which is where real billing bugs come from. And they say the role, not the implementation: billableHours tells you why the subtraction exists, while x only tells you that it does.
Once the names are right, the translating comments become redundant and can go. That matters because a comment can silently fall out of date while a name is checked at every call site by the compiler or the test suite.
The arithmetic is untouched, so behaviour is identical: the same inputs still produce 450. Rename is safe precisely because it changes nothing a computer cares about, which is also why you should keep it in its own commit, separate from any logic change.
- Callers understand what a method returns, and in what unit, without opening its source.
- Unit suffixes like
CentsandMsmake unit mix-ups visible at the call site. - Translating comments disappear, so there is nothing left to drift out of sync with the code.
- Code search and stack traces become useful because the names match the domain words the team already uses.
- The change is low risk: no logic moves, so a review only has to check that every reference was updated.
Careful with
- Renaming a parameter is a breaking change wherever callers use named arguments, such as Python keyword arguments and PHP 8 named arguments. Treat those parameters as part of the public API.
- Editor rename tools only find references they can resolve statically. Strings, reflection, DI config, serialized payloads and ORM mappings need a manual text search.
- For published libraries or service APIs, keep the old name as a deprecated alias for at least one release instead of removing it in one step.
- Do not mix a rename with a behaviour change in the same commit. Reviewers cannot spot a logic change hidden inside 200 renamed lines.
Step by step
Small, safe steps, with the tests passing after each.
Make sure tests cover the code you are renaming. For Boxwell, a test asserting that 50h10m with 48 grace hours at 150 cents returns 450 is enough to start.
Rename local variables first (
htohoursStored,xtobillableHours). They have no outside callers, so use your editor's rename-symbol action, run the tests and commit.Rename parameters and private fields (
d,n,g,r). Check for callers that pass arguments by name, such as Python keyword arguments or PHP 8 named arguments, and update them in the same change.For a method with callers outside the file, add the new method
overstayFeeCentswith the body, and make the oldcalca one-line call to it marked deprecated. Run the tests.Do the same for the class: rename it to
LockerFeeCalculatorand keepLkrMgras a deprecated alias (a TSexport constplus type alias, a Python assignment, or PHPclass_alias). Run the tests.Migrate callers to the new names in small batches, running tests after each. In a monorepo this can be one commit; across separately deployed services it may take several releases.
Search for the old names as plain strings, not just symbols: dependency injection config, reflection, serialized class names, routes, templates, log queries and dashboards.
When nothing references the old names, delete the deprecated method and alias, then remove the comments that only translated the old names. Run the full suite and commit.
Smells it removes
Smells this refactoring helps remove.
Common questions
Short answers to the usual doubts.
How do I pick a good name?
Name it after what it means to the caller, not how it is computed. Use the domain words your team and product people already say, include the unit when the type does not carry it, and make the length proportional to scope: a loop index can be i, a public method cannot be calc. If you struggle to name something, that is often a sign it does too many things.
Is it worth renaming in an interview coding exercise?
Yes, briefly. Saying that you would rename calc to overstayFeeCents and why shows you read code for the next maintainer. Do it after the solution works, and mention that you would ship it as a separate commit.
How do I rename something used by other services I do not control?
Use an expand and contract approach. Add the new name alongside the old one, make the old one delegate to the new one, announce the deprecation, track remaining usage through logs or metrics, and remove the old name only when usage reaches zero.
Should I rename a database column the same way?
The same principle applies but with more steps, because data and running code both depend on the name. Add the new column, write to both, backfill, switch reads, then drop the old column. Each step is its own deploy.
Does adding units to names not make them too long?
A few extra characters are cheaper than a fee charged in milliseconds instead of hours. If the suffix appears everywhere, consider a dedicated type such as a Money or Duration value object instead, which moves the unit from the name into the type system.