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
- Current state: three to five sentences on structure and the main problems.
- Code smells table:
| Smell | Where | Effect on the team | Refactoring technique |
- Safety net: the characterisation tests to write first, described in one line each.
- 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
- Example: the refactored version of the single worst function, with a short note on what changed.
- 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.






