Refactoring

Definition
Refactoring is restructuring existing code so it behaves exactly the same but is easier to understand and safer to change, without adding a feature.

Why it matters

Code grows messy as it grows. Quick fixes pile up, and near-duplicate logic spreads across files. This is especially common when an AI coding agent writes most of the code, because it will happily add a new copy of a pattern rather than reuse the old one. Each new feature then takes longer and carries more risk, because a change has to be repeated in several places and one of them gets missed. Refactoring pays that debt down before it slows everything else.

How to apply it

  • Prove behaviour first. A set of tests, or at least a repeatable manual check, should pass before and after the change.
  • Work in small steps and run the check after each one, so a mistake is easy to find.
  • Do not mix a refactor with a feature. Keep it as its own change so a reviewer can see that nothing should have moved.
  • Refactor where the next piece of work is going to happen, not everywhere at once.
  • Stop when the code is clear enough. Endless polishing is also waste.

What it is

Refactoring is tidying code without changing what it does. A user should notice no difference. Typical moves are pulling copied logic into one shared place, renaming something confusingly named, or splitting a very long file into smaller pieces. The key rule is that behaviour stays identical. A change that alters behaviour is a rewrite, a bug fix or a new feature, and should be labelled as one.

Common mistakes

  • Refactoring code that has no tests, so nobody can tell whether behaviour changed.
  • Starting a large clean-up with no clear end and leaving half the code in the old style.
  • Letting an AI agent restructure freely and accepting the result without a check, which is how a regression slips in.
Worked example

Suppose a two-person team runs a pricing calculator where the same discount rule is copied into three files. A change to one copy leaves the other two wrong, and each new feature takes longer than the last. The team asks Claude Code to find the three copies and move the rule into one shared function. Before the change, they run the existing tests and record the totals for twenty sample quotes. After the change, the same twenty quotes return identical totals and the tests still pass. The refactor is committed on its own, separate from the new discount tier, so a reviewer can see that nothing should have changed. Adding the next discount now means editing one place instead of three.

Tools in the example

Some links are affiliate links: we may earn a commission at no cost to you. It never decides a ranking. How we work with partners

  1. Article

    Technical debt

    What refactoring pays down.

  2. Article

    Code review

    Where a refactor is usually checked.

  3. Article

    Regression

    The risk a careful refactor avoids.

  4. Article

    Test coverage

    How much of the code a safety net actually checks.

Where it shows up

  • Writing copy that feels like conversation instead of marketing. How to sound like yourself.
    12 chapters