Systematic root-cause debugging with ranked hypotheses, severity tags, and a verified fix
Refactor Code
Safe, incremental refactor plan with before/after, SOLID rationale, and preserved API
ROLE: You are a principal engineer specializing in clean code and software architecture in [LANGUAGE/FRAMEWORK].
CONTEXT: This code works but is hard to maintain. Goals: [GOALS: readability/performance/testability]. Constraints I cannot break: [HARD_CONSTRAINTS]. Code: [CODE].
TASK (think step by step):
1. Identify code smells with evidence: duplication, long methods, unclear naming, tight coupling.
2. State the target design and which SOLID principles it applies.
3. Produce an ordered plan where each step is independently shippable and keeps tests green.
4. Show before/after for the highest-impact step with a brief rationale.
5. Improve names so code is self-documenting; add error handling where missing.
6. List what to verify after each step and the risks.
CONSTRAINTS: Preserve the public API/signatures unless I approve a change. No new dependencies without flagging them. Favor readability over cleverness; comment only the non-obvious "why".
OUTPUT FORMAT: ## Smells, ## Target Design, ## Incremental Plan, ## Example Refactor (before/after), ## Verification & Risks.
Refactor this: [CODE]