Refactor Prompt — the Behavior Preservation Contract
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
Code a new hire cannot follow is a cost on every future change. This prompt renames for intent, extracts nameable steps, and replaces cleverness with clarity — semantics untouched.
Readability refactoring is renaming, extracting, and clarifying — and each of those is a behavior risk wearing a friendly face. This prompt carries the readability goal's priorities: names that state intent, small functions that do one nameable thing, magic values replaced with declarations, shorter distances between definition and use. Its warnings target where readability refactors actually fail: a rename must be reference-complete across every call site including reflection and string-based lookups, and an expression you do not fully understand is not yours to "clean up". Validation demands reference-completeness, not vibes.
Rename toward intent
Variables, functions, and types say what they mean — with every reference traced, including the string-based ones.
Extract nameable steps
Long blocks become small functions, each doing one thing its name states.
Verify reference-completeness
A rename missed in one call site is a runtime error wearing a clean diff.
It can't guarantee it — it demands you verify it. The VALIDATION STRATEGY requires renames and extractions to be "reference-complete: verify every call site, override, and string-based reference was updated," and the transformation warning flags reflection, serialization, and string-based lookups grep can't see. The prompt asks your assistant to trace these, but you run it and confirm the diff against your codebase.
Behavior takes priority. BEHAVIOR PRESERVATION REQUIREMENTS state that if achieving the goal and preserving behavior conflict, "behavior wins: stop and explain the conflict instead of compromising it," and the guidance forbids cleaning up expressions you don't fully understand. Anything skipped for safety lands under "Suggested but not applied" with what information would unlock it.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
For code that ships weekly: a refactoring prompt where regression avoidance outranks improvement depth, log lines stay intact, and every step is rollback-friendly.
Generated code duplicates instead of extracting, wraps without deciding, and defends against impossible states. This prompt hunts those exact failure modes — without preserving bugs as behavior.
"Review this code" gets shallow comments. The review contract gets findings with severities, a checklist, and a verdict.
getByRole over CSS chains, auto-wait over sleep, web-first assertions — Playwright tests written the way Playwright wants.
"Fix this error" gets guesses. The investigation contract gets a ten-stage diagnosis: facts separated from assumptions, alternatives weighed, fixes justified.
Build behavior-preserving refactor prompts — goal, risk level, and code context turn into a safe refactoring contract.
The order that actually finds bugs instead of guessing at them — so you end with a verified fix, not a plausible one that quietly returns next week.