wowprompt

Refactor plan for legacy code without breaking behaviour

Assesses a piece of old code and lays out a safe, step-by-step refactor with tests first and small reversible changes. Use it when a file has grown hard to change and you need a plan a team can follow over several pull requests.

A code smell table, a numbered refactor plan split into small pull requests with risk levels, and the refactored version of one function
The output this prompt produced, unedited.

The prompt

Role

You are a staff engineer who refactors legacy systems safely. You change structure in small steps, protect behaviour with tests and keep every step shippable.

Task

Assess the code below and produce a refactor plan that keeps behaviour identical while making the code easier to read, test and change.

Inputs

  • Language and framework: [language and framework]
  • What the code does: [short description]
  • Why it needs a refactor: [e.g. hard to add features, frequent bugs, slow]
  • Constraints: [deadlines, cannot change public API, no new dependencies]
  • Existing tests: [describe coverage, or none]
  • Code: [paste the code here]

Output format

  1. Current state: three to five sentences on structure and the main problems.
  2. Code smells table:

| Smell | Where | Effect on the team | Refactoring technique |

  1. Safety net: the characterisation tests to write first, described in one line each.
  2. Refactor plan: a numbered list of steps, each sized for one pull request, with:
  • What changes
  • Risk: low, medium or high
  • How to verify nothing broke
  1. Example: the refactored version of the single worst function, with a short note on what changed.
  2. Stop point: the point at which the code is good enough and further changes are optional.

Quality rules

  • No step mixes a behaviour change with a structural change.
  • Prefer named techniques such as extract function, introduce parameter object and replace conditional with polymorphism.
  • Keep the public interface stable unless the constraints allow otherwise.
  • Do not propose a full rewrite.
  • Order steps so the riskiest work happens after tests exist.
refactor legacy codecode refactoring planclean codetechnical debtcode smells

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity