A refactor plan that ships in small safe steps with Claude

The struggle: You need to clean up a tangled module, but every earlier refactor turned into a two-week branch nobody dared to merge.

What you end up with: A written plan of small steps, each with its own tests and rollback, and a first step you have checked yourself.

Use Claude Code in plan mode to map what depends on the module, then ask for steps small enough to merge one by one. Tests come first, and you read every change before it is committed.

Tools: Claude. Level: Pro. Time: 1 to 2 hours.

Use it if

  • You must change a module that many parts depend on
  • You can run the code and its tests on your machine
  • You want steps that can ship separately and be reverted

Skip it if

  • You cannot give an AI tool access to this code
  • There are no tests at all and nobody can run the app (add tests first)
  • You want it to rewrite the module unattended

What you need first

  • Claude Pro or higher (Claude Code is included from Pro)
  • Claude Code installed, or the Code tab in Claude Desktop, with the repo on your machine and a clean git branch

The method

  1. Start in plan mode on a new branch (Claude: Claude Code)

    Create a branch, open a terminal in the project root and run claude --permission-mode plan, or press Shift+Tab until plan mode shows. In plan mode it reads and proposes, and does not edit.

    You get: Claude Code is running in the repo and cannot change files.

    Tip: Run the existing tests first and note which already fail. A failing test you did not break is not a refactor bug.

  2. Map what depends on the code (Claude: Claude Code)

    Name the module and the goal. Open each file it cites and check the claims. Add anything it missed from your own knowledge, such as scripts and configs outside the code folders.

    Prompt to copy:

    I want to refactor [module or path] to [goal, for example split it into two parts or remove the duplicated logic]. Do not change any files. List what calls it, what it calls, shared state, data it reads or writes, and any code in other folders that depends on its current behaviour. For each, give the file and function. Mark anything you are unsure about as Unsure.

    You get: A dependency map with file references.

    Tip: Ask for dynamic uses too: reflection, string-built names and config keys. Those are what a search for callers misses.

  3. Find the tests that protect you (Claude: Claude Code)

    Ask which behaviour is covered and which is not. Decide with your team which gaps to cover before the first change. Run the tests yourself in the terminal.

    Prompt to copy:

    For [module], list the behaviour the current tests lock in and the behaviour they do not. Propose characterisation tests that would pass today and fail if the refactor changed behaviour, with file names and a one-line description each. Do not write the tests yet.

    You get: A list of tests to add before touching the code.

    Tip: Characterisation tests record what the code does today, even where that looks wrong. Fix bugs in a separate change.

  4. Write the step plan (Claude: Claude Code)

    Ask for numbered steps, each small enough to review in one sitting. Reject any step that changes behaviour and structure at once. Save the approved plan in a file you commit yourself.

    Prompt to copy:

    Write a refactor plan for [module] as numbered steps. Each step must: change one thing, leave all tests passing, be reviewable in under 30 minutes, and list how to revert it. Put the characterisation tests first. State what is out of scope. Mark any step where the risk is unclear and say what to check first.

    You get: A plan of small steps with a rollback for each.

    Tip: If a step needs another step to compile, merge them or reorder. Every step must leave the build green.

  5. Do step one and review it (Claude: Claude Code)

    Leave plan mode for step one only. Read the whole diff yourself, run the tests and commit when you are satisfied. Open a pull request for a colleague to review. Then repeat for the next step in a new session.

    You get: The first small step, reviewed and tested.

    Tip: Do one step per commit. A bad step is then one revert, not a rebuild.

Where this goes wrong

  • Claude Code can edit files and run commands. Plan mode makes no edits, and git shows anything that changes. Review every change before you commit.
  • A confident dependency map can still miss callers. Search the repo and run the tests yourself.
  • Keep .env files, API keys and production credentials out of its reach, and do not paste them into prompts.
  • Do not run generated migrations or scripts against production without a human review.

Sources

More like this

Updated . Written with AI help and checked by an independent review step, as described in our editorial standards.