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.
Deep nesting and tangled branching make code unreasonable. This prompt flattens control flow with guard clauses and collapses duplicate decisions — with every branch still routing the same way.
Complexity reduction is the refactor most likely to silently change behavior: inverting a condition to flatten nesting, collapsing two branches that look identical, deleting code that merely looks dead. This prompt uses the simplification goal's priorities — guard clauses over nesting, one decision encoded once, dead code removed only when proven unreachable — and pairs them with the goal's own warnings: "looks unused" is not "is unused" (reflection, dependency injection, serialization, external callers), and inverted conditions are a classic behavior-change risk. Validation confirms every input class still reaches the same outcome.
Flatten with guards
Early returns and guard clauses replace nesting — the happy path reads top to bottom.
Collapse repeated decisions
The same condition decided in three places becomes one decision, decided once.
Prove before deleting
Dead code dies only with evidence of unreachability — lookalike-dead code gets flagged instead.
It puts behavior above the goal explicitly. The TRANSFORMATION GUIDANCE warns that inverting conditions to flatten nesting is a classic behavior-change risk and requires verifying every branch still routes the same inputs to the same outcomes. BEHAVIOR PRESERVATION REQUIREMENTS add that if achieving the goal and preserving behavior conflict, behavior wins — the model stops and explains rather than compromising.
Only code proven unreachable is removed. Priority 3 removes dead code but not code that merely looks unused, and the guidance spells out the trap: "Looks unused" is not "is unused" — check for reflection, dependency injection, serialization, dynamic dispatch, and external callers first. Lookalike-dead code gets flagged instead of deleted, so it stays in your review, not silently gone.
It surfaces rather than silently skips. The OUTPUT REQUIREMENTS put any worthwhile-but-skipped transformation under a "Suggested but not applied" list, stating what information would unlock it. At the Balanced risk level, borderline-safe changes get the conservative version applied with the more aggressive option noted, so you decide the risk instead of the model.
"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.
Speed up code that works but drags — find the actual hot path instead of guessing, understand why it's slow, optimize it, and prove with tests that you changed the speed and nothing else.