Verified against Claude Code · 2026-08-01
Refactor with Claude Code without smuggling in a behavior change
A refactor prompt that makes preserving external behavior an explicit, checkable constraint, forces the existing test suite to pass unmodified as proof, and requires the diff to be explained hunk by hunk, not just dumped.
The prompt
Ready to copy — highlighted parts are example details you can swap.
You are refactoring, not rewriting. The observable behavior of the code in scope — every input and output pairing, every side effect, every error condition, and every public signature — must be identical before and after this change, unless the preservation constraint below explicitly permits something to change. SCOPE OrderProcessor.calculateTotal in services/order-processor.ts THE ONE GOAL OF THIS REFACTOR Reduce the nested if and else chain into something readable, no other goal. MUST PRESERVE The public method signature and its Decimal return type must not change; internal helper functions may be renamed freely. EXISTING TEST COVERAGE OVER THIS CODE 14 unit tests in tests/order-processor.spec.ts covering the standard discount tiers, but none covering a cart with a single item. PROCESS — FOLLOW IN ORDER 1. Before writing any code, list exactly what you intend to change and why, in terms of structure only, for example extracting one specific duplicated check into its own function. Do not begin editing until this list exists. 2. Make the change described in that list, and nothing beyond it. 3. Run the existing test suite exactly as it stands. If any test needs to change to keep passing, that is direct evidence you changed behavior, not merely structure — stop and explain what happened rather than editing the test to match the new code. 4. For each hunk in the resulting diff, write one line: what changed structurally, and an explicit confirmation of what stayed behaviorally identical across that same hunk. CONSTRAINTS - Do not change a public function signature, an exported name, or a return shape unless the preservation constraint above explicitly permits it. - Do not fix an unrelated bug you notice mid-refactor, even a trivial one. Note it separately at the end instead. - If the stated goal is impossible to achieve without a behavior change somewhere, stop and say exactly where before writing any code — do not make the change silently and disclose it only in a closing summary. - If the existing test coverage described above does not actually exercise the behavior you are about to touch, say so before refactoring, since a passing suite after the change would not actually prove anything in that gap. - If the change list from step one would require touching more files than the scope above names, treat that as a signal the scope was drawn too narrowly rather than silently expanding it — stop and ask whether the scope should be widened deliberately, instead of creeping past it one file at a time. OUTPUT The structural change list, the diff, the per-hunk structural-versus-behavioral notes, and the test run result with pass and fail counts. If a coverage gap was found, add one line naming the specific behavior that a new test would need to exercise to close it.
Customize
Optional — swap in your own details for the highlighted parts above.
Why this works
'Refactor this' is underspecified in a way agentic assistants with real Edit access exploit — without an explicit definition of what must stay the same, a model will change a signature, alter an edge-case return value, or improve error handling while still calling the result a refactor, because nothing told it not to. Naming behavior preservation as a checkable constraint covering inputs, outputs, side effects, error conditions, and signatures gives it a specific target to verify against rather than a vague quality bar it is free to interpret generously in its own favor. Requiring the change list before the edit, and requiring the existing test suite to pass without modification, converts 'trust the diff' into 'verify the diff against a stated plan and a green test run' — if a test needs to change to keep passing, that is the exact signal a refactor silently became a behavior change, and the prompt makes stopping there the explicit instruction rather than leaving it to the model's own judgment about whether the test update is incidental. The coverage-gap check matters independently of the refactor process itself: a green suite after a change is only meaningful evidence if the suite was actually exercising the path that changed, and an agent that reports 'tests pass' without first checking whether those tests ever touched the refactored logic is offering false confidence dressed as verification. Asking for a one-line structural-versus-behavioral note per hunk also produces a paper trail a reviewer can check line by line, instead of a diff with no stated intent behind any individual change. Treating an expanding file count as a signal to stop and ask, rather than a threshold to quietly cross, matters because scope drift during a refactor rarely arrives as one dramatic decision — it arrives as a sequence of individually reasonable one-file extensions, each of which looks like finishing the same job properly, and by the time the total file count has doubled from the original scope, no single step in that sequence would have looked wrong in isolation.
What you get back
Change list: extract the nested discount-eligibility checks into a helper named isEligibleForDiscount, no other change. Test run: 14 passed, 0 failed — no test needed modification, confirming behavior did not change. Per-hunk note: lines 42-58 moved into isEligibleForDiscount with no logic change, only extraction; calculateTotal's signature and Decimal return type are unchanged. Coverage gap flagged: none of the 14 tests exercise a single-item cart — recommend adding one before trusting this refactor fully on that path.
Verified against
Claude Code Sonnet 4.6 · 2026-08-01
Changelog
- 2026-08-01 — Initial publish, verified against Claude Code (Sonnet 4.6).
Building this for real?
This is a free starting point. If you'd rather have custom software built and running for your business, that's Scult's day job.
EXPLORE CUSTOM SOFTWARE
