ChatGPT Prompts for Refactoring: Clean Code Without Breaking Things
ChatGPT prompts for code refactoring. Systematic refactoring strategies for legacy code, SOLID principles, and safe incremental improvements.
The Prompt
Act as a software architect who specializes in legacy code modernization and has refactored codebases with 1M+ lines of code.
Refactor the following code:
[PASTE CODE]
Language/framework: {language and framework}
Refactoring goals: {readability / performance / testability / SOLID principles / DRY / remove tech debt}
Constraints: {must maintain same public API / must not break existing tests / must be backward compatible}
Context: {how this code is used — is it called frequently? Is it in a hot path?}
Refactoring output:
1. Code smell diagnosis (what's wrong and why — before you show the fix):
- Issue 1: [smell], [why it's a problem], [severity: low/med/high]
- Issue 2...
2. Refactored code (complete, runnable — comments explaining each significant change)
3. Change summary table:
| Change made | Reason | Backward compatible? |
4. Tests to run to validate the refactoring (which existing tests cover this, and any new tests needed)
5. Incremental refactoring path (if the full refactor is risky — step-by-step approach to get there safely)
Constraints:
- Refactored code must pass the same tests as the original (unless tests themselves are being improved)
- Comments must explain WHY changes were made, not WHAT they do
- Each change must be independently deployable — no changes that require other changes to work
- Performance-critical paths must be benchmarked before and after if performance is a goal
Variables to fill in
-
{code}The code to refactor — paste the full block -
{refactoring goals}What you want to improve — readability, testability, performance, etc. -
{constraints}What cannot change — public API, test compatibility, backward compatibility -
{context}How this code is used — helps prioritize which improvements matter most
How to use this prompt
- Define your refactoring goals before running — 'readability' and 'performance' require different approaches
- Use the incremental path when the codebase has no test coverage — refactor safely in stages
- Commit each step in the incremental path separately — gives you rollback points
- Run the full test suite after each step in the incremental path before proceeding
Refactoring without tests is rewriting
Refactoring means improving code structure without changing behavior. If there are no tests to verify behavior hasn’t changed, you’re not refactoring — you’re rewriting, which carries far more risk. The prompt’s step 4 forces you to identify which tests validate the refactoring before you make changes.
The code smell diagnosis is more valuable than the fix
Understanding why a piece of code is problematic is more valuable than the refactored version. If you can’t articulate the problem, you’ll introduce the same smell in the next feature. The diagnosis section — naming the specific smell, its impact, and its severity — builds the pattern recognition that makes you a better developer.
Incremental paths reduce risk to near zero
A full refactor that ships in one PR is high risk: it’s hard to review, hard to test, and hard to roll back. The incremental path breaks the refactor into independently deployable changes — each one small enough to review in 10 minutes and revert in 30 seconds if something goes wrong in production.
Related free tools
- Free AI Prompt Generator — build structured prompts instantly
- AI Token Counter — estimate API costs before long runs
- AI ROI Calculator — measure AI’s impact on developer productivity