← Back to playbooks
PLAYBOOK · 47 · CODING

Refactor a legacy file without breaking it.

A diff-first workflow with Cursor that keeps you in control of every change.

Intermediate20 minTested · Apr 20, 2026

Why this works

Legacy refactors fail when the model changes behavior while making code look cleaner.

This flow makes the model describe current behavior, add characterization checks, then patch one concern at a time.

Run it

  1. Ask for behavior notes
    Have Cursor summarize what the file does before editing.
  2. Add characterization tests
    Cover current behavior, even if it is awkward.
  3. Refactor one axis
    Rename, extract, or simplify, but never all three at once.
  4. Review the diff
    Reject any change that is not tied to the stated refactor goal.

The prompt

Refactor this legacy file without changing behavior.
First summarize the current behavior and risk areas.
Then propose characterization tests.
After tests exist, make the smallest diff for one refactor axis only.

Next 5-minute action