Skip to content AI News: Nano Banana 2.1 is out: better edits and text, half the API price per image →
PH-0908 · Code prompt

Safe Refactor Pass

A Claude Code prompt that refactors behind passing tests, writing characterisation tests first and stopping the moment one of them fails.

What you get

Refactors behind passing tests, writing characterisation tests first and stopping the moment one of them fails.

What you need
  • 2 blanks to fill in: file or module the reason
Best inClaude CodeAlso works in any coding agent
220 words
Prompt · September 19, 2026
I want you to act as an engineer carrying out a refactor in which behaviour must not change. I will name the file, module or pattern to clean up and say why it bothers me.

Before touching anything, check what protects this code: find the tests that cover it, run them, and report the result. If the coverage is thin, write characterisation tests that capture the current behaviour first, including the odd cases, and confirm they pass before you change a line. Tell me if you believe the current behaviour is wrong, but do not fix it in this pass.

Then refactor in small steps. After each step, run the tests, and stop if any of them fail. Keep the public interface, the names other files import, the error messages and the log output identical unless I say otherwise. Do not add a dependency, a new abstraction layer or a configuration option.

When you are done, give me: the steps you took in order; the test run after each one; a short before and after for the two or three places that changed most; anything you deliberately left alone and why; and the behaviour you are least confident is unchanged, with the check I could run to be sure.

My first request is: "Refactor [file or module] because [the reason]."
Coding Claude Code

How to use it

  1. Fill the blanks under «Make it yours» — the prompt updates as you type.
  2. Copy the prompt and paste it into Claude Code.
  3. Not quite right? Reply with what to change — the model keeps the rest.

More Claude Code prompts

PH-0894Coding

Repo Feature Planner

Explores your repository and returns a commit sized plan, the risks and a definition of done before it writes code.

You fill in describe the feature you want…

Claude Code
PH-0708Coding

Bug Reproduction Test

Writes a failing test first, explains the real cause, then makes the smallest fix and runs the whole suite.

You fill in describe the bug, how to…

Claude Code
Weekly

New prompts in your inbox

One email a week: the best new prompts and one short guide. Unsubscribe any time. Privacy policy