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.
var to const, callback pyramids to async/await, || guards to ?? — with the trap called out by name: || treats 0 and empty string as missing, ?? does not.
JavaScript modernization has a famous failure mode: the mechanical swap that changes semantics. Replacing a || fallback with ?? changes what happens for 0 and empty string; flattening a callback pyramid reorders error handling; var-to-const surfaces hoisting assumptions. This prompt sets the JavaScript modernization scope — const/let over var, async/await over callback pyramids, optional chaining and nullish coalescing with the ||-vs-?? trap stated inline, destructuring and array methods where they clarify — and loads a real callback-style module whose code carries exactly that trap: a `withAvatar || user` fallback the contract must treat as semantics, not syntax.
Carry the code verbatim
The module rides in a fenced block — the contract transforms what exists, not a paraphrase of it.
Flatten with care
Callback pyramids become async/await with error paths preserved — every callback(err) branch accounted for.
Treat || as semantics
Each || fallback is checked: does this code mean "missing" or "falsy"? Only then does ?? apply.
That trap is called out inline: the modernization scope says "careful: || treats 0 and \"\" as missing, ?? does not," and the loaded module carries a real withAvatar || user fallback the contract must treat as semantics, not syntax. Each || is checked for "missing" versus "falsy" before ?? applies. The Refactor Prompt Builder generates this prompt; the modernization runs in your assistant, and you verify it.
The prompt resolves it one way: "If achieving the goal and preserving behavior conflict, behavior wins: stop and explain the conflict." Borderline-safe transformations get the conservative version applied with the aggressive option noted, and anything skipped for safety lands under a "Suggested but not applied" list with what info would unlock it. It's an instruction the model should follow; you still review the flagged changes and run your own tests.
"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.