L

Refactoring vs Rewriting

Both produce a large diff and the sentence “I cleaned up the export module”. One of them made a promise about behaviour. The other quietly cancelled it.

Two pull requests. Same size, same sentence.

Both changed about four hundred lines. Both say the module was cleaned up. One of them also changed the tests.

Pull request A

Split a 300-line function into six named ones. Renamed three variables. Deleted a dead branch. The test file was not opened.

Every existing test passes exactly as written. Nothing outside the file can tell anything happened.
Pull request B

Rebuilt the export module from scratch on a cleaner design. Eleven tests were updated to match the new shape.

That edit to the tests is the whole story. The old tests described the old behaviour, and the new behaviour is different.
Both diffs are the same size and both say “cleaned up”. Only one of them had to change what the tests expect.

What each one actually is

One is a change with a promise attached. One is a change with the promise removed, which is why it costs what it costs.

Refactoring keeps a promise: nothing outside this file may notice. That promise is what makes it safe, and it is also what makes it checkable — the old tests are the promise written down, so if they still pass untouched, the promise held.

Rewriting cancels the promise, and everything the old code quietly knew goes with it. Years of small decisions live in working code and almost none of them are written anywhere else — the odd currency, the partner who needs tab-separated files, the retry that exists because an API once flaked at month-end.

The question that separates them takes one second and never lies: did the tests have to change? If yes, behaviour changed, and you are rewriting — which may well be right, but it is a different job with a different risk and a different estimate.

Sort six changes, then see what each promises

Tap a change, then tap whether anything outside the file may notice. Then see what each one is actually committing to.

The same swap, different desk

Pick a situation. In every one, a tidy-up was asked for and a replacement was delivered.

In every one of these, a tidy-up was asked for and a replacement was delivered.

The export nobody remembered

It was used four times a year, by one partner, and it was in the old code because somebody once needed it.

Test the distinction

Five questions. Nothing is scored.

Question 1 of 5
Multiple choice

The words this pair actually contains

Five terms, not two. Tap one.

Questions people ask

Open all 10 questions
How do I know if I need refactoring or rewriting for my project?

If your code works but is messy or hard to read, refactoring is usually enough. If the code is broken, unfixable, or must do something new, rewriting may be needed. Always start by asking what needs to change—structure or behaviour.

What are the risks of rewriting code compared to refactoring?
  • New bugs can appear.
  • Old features might be lost.
  • Timelines can slip.
  • Testing must start from scratch.
  • More resources are needed.
Can AI tools help with refactoring and rewriting?

AI tools can suggest refactoring steps, like renaming variables or splitting functions. For rewriting, AI can generate new code, but you must check that the new code matches what you need and does not break anything important.

Why do people confuse refactoring with rewriting?

Both involve changing code and can look similar in a code review. The confusion happens because both can be described as 'cleaning up' or 'fixing' code, but only rewriting changes what the code does.

What is an example of refactoring in a real project?

Renaming variables in a payroll script so they are easier to understand, or splitting a long function into smaller ones, are both refactoring. The script still calculates pay the same way, but is easier to update next month.

How can I check if a change was refactoring or rewriting?
  1. Look at the code before and after.
  2. Run the same input through both versions.
  3. If the output matches, it was refactoring.
  4. If the output changes, it was rewriting.
What should I watch for after a rewrite?
  • Unexpected bugs.
  • Missing features.
  • Performance drops.
  • Compatibility issues.
  • Unhappy users or colleagues.
How do I reduce risk when rewriting code?

Break the rewrite into small, testable parts. Write tests for old and new code. Involve colleagues who know the old system. Communicate clearly about what is changing and why.

Is it possible to refactor and rewrite at the same time?

Yes, but it is risky. Mixing the two makes it hard to spot where new bugs come from. It is better to refactor first, then rewrite if needed, so you can track what changed.

What is a regression and why does it matter in rewriting?
TermWhat it meansWhy it matters
RegressionA feature that stops working after a changeRewriting can introduce regressions, so users lose trust if things break.

If the old tests still pass untouched, you refactored. If you had to change the tests, you rewrote — whatever the commit message says.

Copyright © Pawan Nayar · LLOS.ai · 2026 — Refactoring vs Rewriting: keeping the promise about behaviour, versus cancelling it.Original pedagogy, voice, and design — all rights reserved.