Refactor Prompt: Improve Without Breaking Behaviour
Simplifies working code without changing behaviour, planned as reversible steps you can review one at a time.
You are a staff engineer doing a careful refactor. Behaviour must not change. CODE ```[LANG] [CODE] ``` CONTEXT - What this code does: [PURPOSE] - Test coverage: [NONE / PARTIAL / GOOD] - Constraints: [PUBLIC API, PERFORMANCE, STYLE] - What bothers me: [SMELL] STEP 1 — BEHAVIOUR INVENTORY List every observable behaviour, including error paths and side effects. This is the contract that must survive. STEP 2 — SMELLS What is actually wrong, ranked by risk. Name the pattern, not the feeling. STEP 3 — PLAN Ordered steps, each independently reversible, each small enough to review in one sitting. For each: what changes, what could break, how to verify. STEP 4 — CODE Show the refactored version. Mark every changed region with a comment. If a step is risky, show it separately. STEP 5 — TESTS FIRST Tests that must exist before the refactor starts. If coverage is NONE, say so and propose the minimum safety net. STEP 6 — WHAT NOT TO TOUCH Parts of this code that look bad but should be left alone, and why. RULES - No behaviour change, no new features, no renamed public API. - Do not introduce dependencies. - If the refactor is not worth the risk, say so and stop.
A refactor that changes behaviour is a rewrite with a nicer name.
02 — Variables
Fill in the brackets
The highlighted fields are the places where your own details go. You do not have to fill all of them; anything left empty is completed by the model's own assumption.
[LANG]
[CODE]
[PURPOSE]
[NONE / PARTIAL / GOOD]
[SMELL]
[PUBLIC API, PERFORMANCE, STYLE]
Field to fill in the prompt
03 — How to use
How to use it, step by step
- 01
If there are no tests, write them first — a refactor without a safety net is a rewrite.
- 02
Take the plan one step at a time; a single large diff cannot be reviewed properly.
- 03
Ask what not to touch; some ugly code is load-bearing.
04 — Variations
Same prompt, different result
One prompt does not solve every case. These variations ask for the same job in a different tone, length or format — pick the one that fits.
Extract module
Same method, focused on splitting this file into modules: propose boundaries, public interfaces and the migration order.
Performance refactor
Same method, but behaviour includes performance characteristics: measure before, set a target, prove the gain after.
05 — Tips
Notes that take the result up a level
- The behaviour inventory is the refactor. Everything after it is mechanical.
- Renaming public APIs is not refactoring; it is a breaking change with a migration cost.
06 — FAQ
Frequently asked about Software and Code
How big should a refactor be?
One reviewable diff at a time. If it touches more than ~200 lines, split it into steps that each ship independently.
Can I refactor and add a feature at once?
No. If something breaks you cannot tell which change caused it.
Next
You may also find these useful
Code Writing Prompt: Clean, Testable Function
Defines a contract instead of a wish: language, version, input, output, error cases and tests.
Intermediate ChatGPTClaudeDebug Prompt: From "Why Doesn't It Work" to a Diagnosis
Converts "why doesn't it work" into a systematic diagnosis: reproduce, hypothesise, isolate, verify.
Intermediate ChatGPTClaudeSQL Query Prompt (Schema Included)
SQL written without the schema rarely runs; this prompt requires schema, relationships and expected output.
Intermediate ChatGPTClaudeRegex Prompt: Testable Pattern, Not a Guess
Builds regex from test cases rather than guesses: strings that must match, strings that must not, and the explanation.
IntermediateThis page was prepared by the CopyPromt editorial team, first published 24 October 2026. Prompts are written to work across models; brand names are mentioned for identification only.