Skip to content
Clean CodeChapter 14 / 57Intermediate6 min read

Refactoring Safely

How to refactor safely in small steps. Improve code without changing behaviour, use tests as a safety net, make small commits and stay in scope.

On this page

1. What is this practice?

Refactoring and building a new feature are two different jobs. In a refactor, the app stays exactly the same from the outside: same input, same output. Only the arrangement inside changes.

PartMeaning
Small stepsOne rename, one extract, one move, then check
Always workingTests are green after every step
Behaviour in a separate commitOne commit for the refactor, another for the feature
Stay in scopeClean up only what is related to your task, note the rest
Check callersBefore deleting old code, see who uses it

2. Why does it matter?

A big "I will fix everything" refactor is the riskiest kind of work. You changed 40 files, something broke, and now you do not know which change broke it.

Big bang refactorSmall safe steps
3 days of work, nothing runs in betweenThe code works every hour
If it breaks, search through 40 filesIf it breaks, check only the last step
Refactor and new feature in one PR, review is impossibleThe refactor PR only changes the shape, review is easy
The main branch moves ahead, big merge conflictSmall PRs merged quickly
Revert and you lose everythingRevert one small commit

3. When to follow it

LevelHow importantWhy
Learning projectA git commit after every small stepGoing back is easy when something breaks, build this habit early
Production codeTests and small PRs are a mustA refactor must never cause a bug that affects users
Senior levelPlan big refactors and split them upA senior splits a large change into small safe pieces

4. Bad vs good code

Task: clean up the discount function.

ts
export function discount(c: any, t: number) {
  if (c.vip) { if (t > 1000) return t * 0.8; else return t * 0.9; }
  if (c.orders > 10) return t * 0.95;
  return t;
}

Bad approach: changed the name, signature, logic and types all at once, and slipped in a "small" behaviour change too.

ts
export function price(customer: Customer, total: number) {
  if (customer.tier === "vip") {
    return total * VIP_RATE;
  }

  return total;
}

The 5 percent discount for loyal customers (more than 10 orders) quietly disappeared. There was no test, so nobody knows.

Good approach: first a safety net, then small steps.

Step 1: capture the current behaviour in a test.

ts
test.each([
  [{ vip: true, orders: 0 }, 2000, 1600],
  [{ vip: true, orders: 0 }, 500, 450],
  [{ vip: false, orders: 11 }, 100, 95],
  [{ vip: false, orders: 2 }, 100, 100],
])("discount(%o, %i) is %i", (customer, total, expected) => {
  expect(discount(customer, total)).toBe(expected);
});

Steps 2, 3, 4: change one thing at a time and run the tests each time.

ts
const VIP_BIG_ORDER_THRESHOLD = 1000;
const VIP_BIG_ORDER_RATE = 0.8;
const VIP_RATE = 0.9;
const LOYAL_MIN_ORDERS = 10;
const LOYAL_RATE = 0.95;

export function discount(customer: Customer, total: number) {
  if (customer.vip) {
    return total * vipRate(total);
  }

  if (customer.orders > LOYAL_MIN_ORDERS) {
    return total * LOYAL_RATE;
  }

  return total;
}

function vipRate(total: number) {
  if (total > VIP_BIG_ORDER_THRESHOLD) {
    return VIP_BIG_ORDER_RATE;
  }

  return VIP_RATE;
}
What is the difference?BadGood
Safety netNo testsA test of the current behaviour first
StepsEverything at onceTypes, then constants, then braces, then extract
BehaviourThe loyal discount vanishedExactly the same, the tests prove it
RenamingThe function name changed too, all callers brokeThe public name stays, change it in a separate PR if needed

5. How to apply it

The safe refactor loop
Do you have tests? If not, first write a test of the current behaviour
One small change: rename, extract or move
Run the tests and the typecheck
Green? Commit
Red? Undo only the last step
Next small change

When should you refactor? When the code shows these signs: duplication, a long function that does many things, tangled conditions, or names that say nothing. The best time is right before you add a feature to that same code. Refactor first to make the feature easy, then add the feature in a separate commit.

Your editor's built-in refactor tools (Rename Symbol, Extract Function, Move to File) are safer than doing it by hand, because they update every reference.

6. Exceptions and common mistakes

Sometimes a full rewrite really is cheaper, like a small file with no callers or prototype code. But in production, "rewrite from scratch" often loses hidden rules that nobody documented. Capture those rules with tests first.

Change the signature of a public API or of a function used by another team only when migrating it is part of your task.

Mistake: Refactor and behaviour change in one commit

Why? When a bug appears you cannot tell whether the reshaping or the new logic caused it, and the reviewer cannot check both at once.

Better: First a refactor commit with the same behaviour, then a separate commit for the new feature.

Mistake: A big refactor without tests

Why? There is no proof that the behaviour stayed the same, and hidden edge cases break silently.

Better: First write tests for the current behaviour, even if that behaviour looks odd.

Mistake: Cleaning up the whole codebase in the middle of a task

Why? The PR gets huge, the scope drifts and the real work arrives late.

Better: Clean up what is related to your task, and note the rest as a separate task.

Mistake: Deleting an old function without checking callers

Why? Another module or script was using it, and the build or production broke.

Better: Search all references first, then delete.

7. What to learn before and after

Final quick revision

CheckQuestion
Safety netAre there tests before you change anything?
StepsIs each change small enough that a break is noticed right away?
GreenDid the tests and typecheck run after every step?
Separate commitAre the refactor and the feature in separate commits?
ScopeDid you touch only code related to the task?
CallersDid you check references before deleting?