Skip to content
Academy ofAI Learning

~/academy $ claude learn refactoring-safely

Refactoring safely

Use Claude Code to restructure code in small, tested steps while keeping what the code does exactly the same. 7 min

Step 1 of 6

$ Read

Same behaviour, better shape

A refactor changes how code is organised without changing what it does. Splitting a 600 line file, renaming confusing functions, removing duplication: users should notice nothing.

That makes tests your safety net. Before starting, ask Claude to run the tests and confirm they pass. If the code has no tests, ask for tests that capture what it does today, odd behaviour included, and commit them first.

Say the goal out loud in your prompt: "keep the same behaviour". Claude reads requests literally, so the constraint matters.

Lesson notes: Refactoring safely

Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You can run a refactor with Claude Code where every step is tested, reviewed and easy to undo.

  1. Step 1 of 6 · Read

    Same behaviour, better shape

    A refactor changes how code is organised without changing what it does. Splitting a 600 line file, renaming confusing functions, removing duplication: users should notice nothing.

    That makes tests your safety net. Before starting, ask Claude to run the tests and confirm they pass. If the code has no tests, ask for tests that capture what it does today, odd behaviour included, and commit them first.

    Say the goal out loud in your prompt: "keep the same behaviour". Claude reads requests literally, so the constraint matters.

  2. Step 2 of 6 · Read

    Small steps, each one green

    One change at a time: extract a function, run the tests, commit. Rename, run the tests, commit. If a step breaks something, you know exactly which one, and undoing it costs nothing.

    For anything touching many files, start in plan mode and agree the order of steps first. For a big restructure, claude --worktree refactor-billing keeps it on its own branch in a separate checkout while you carry on elsewhere.

  3. Step 3 of 6 · Improve a prompt

    Brief a safe refactor

    Rewrite this prompt so the refactor happens in small, tested, reversible steps.

    Starting prompt:

    clean up utils.js

    Hint: Say what to change, that behaviour must stay the same, to work in small steps with tests after each, to commit each step, and what not to touch.

    Show a strong version
    Refactor src/utils.js without changing its behaviour. First run npm test and confirm everything passes. Then, one step at a time: split the date helpers into src/utils/dates.js, the money helpers into src/utils/money.js, and remove the duplicated formatPrice. Run npm test after each step and commit each step separately. Do not change any function's inputs or outputs, and if you spot a bug, list it for me rather than fixing it.
  4. Step 4 of 6 · Sort

    Refactor or behaviour change?

    Sort each change. Does it belong in the refactor, or is it a behaviour change that needs its own commit and test?

    • Rename getData to getCustomerOrders everywhere it is used
    • Split a 600 line file into three modules
    • Fix the rounding bug you noticed along the way
    • Change the error message users see on a failed payment
    • Replace a hand-written loop with map, producing the same result
    • Delete a function nothing calls
    Show the answers
    • Rename getData to getCustomerOrders everywhere it is used: Refactor. Same behaviour, clearer name. Tests should pass untouched.
    • Split a 600 line file into three modules: Refactor. Structure changes, behaviour does not.
    • Fix the rounding bug you noticed along the way: Behaviour change. A worthwhile fix, but it changes output. Give it its own test and commit so it is easy to review.
    • Change the error message users see on a failed payment: Behaviour change. Users will notice. That is a behaviour change.
    • Replace a hand-written loop with map, producing the same result: Refactor. Same output, different code. A classic refactor step.
    • Delete a function nothing calls: Refactor. Dead code removal does not change behaviour, as long as a search confirms nothing uses it.
  5. Step 5 of 6 · Read

    Review what changed

    Read the diff with /diff after each step. Ask Claude directly: "list any place where behaviour could differ from before". It is good at finding its own edge cases when asked.

    /simplify reviews changed code for reuse, simplification and efficiency, then applies the fixes. /code-review looks for correctness bugs with a fresh pair of eyes. Use them as a second opinion, then decide yourself.

    If a step goes wrong, /rewind takes the code back to before that prompt, and your step-by-step commits cover anything else.

  6. Step 6 of 6 · Quiz

    Check your understanding

    Halfway through a refactor, Claude notices a real bug and fixes it in the same change. What should you do?

    • Ask it to move the fix into its own commit with its own test, separate from the refactor.
    • Accept it. A fix is a fix.
    • Roll back the whole refactor and start again.
    Show the answer

    Ask it to move the fix into its own commit with its own test, separate from the refactor. Right. Mixed changes are hard to review and hard to undo. Separate commits keep each one honest.