Category · Refactoring
Refactoring Prompts
7 prompts to clean up code without breaking it. Extract, rename, split, deduplicate — with tests that confirm nothing regressed.
The golden rule: Refactor one thing at a time. Refactor → test → commit → next refactor. Never mix refactoring with feature work in the same commit.
Extract a function from a code block
Use when a function is doing too much or a block of code is repeated. Extracting is the safest refactor — behavior can't change.
Two pieces: the new function and the updated original. If AI changes any logic, push back: "You changed the behavior. This is a pure extraction — no logic changes."
Rename a symbol across the codebase
Use when a name no longer matches what the code does. Renames are risky because strings, comments, and tests often reference the old name.
A file list with occurrence counts, then the changes. If AI misses files, ask: "Search the whole repo — you may have missed test files or fixtures."
Convert callbacks to async/await
Use when you're modernizing code or fixing callback hell. This is a behavior-preserving change if done correctly — the key is preserving error handling.
A clean async version with the same semantics. Watch for accidental parallelization — that's the most common refactoring bug.
Split a large file into modules
Use when a file has grown past ~400 lines or has multiple unrelated concerns. Splitting is delicate — the imports tree must stay consistent.
A proposal before execution. This is the one prompt where you must review before saying yes. If AI just executes, push back: "Show me the plan first."
Remove duplication across files
Use when you notice the same logic in multiple places. The safest way to deduplicate is to prove equivalence first, then consolidate.
A comparison first, then a merge only if truly safe. Watch for AI merging "similar but different" code — that's how subtle bugs get introduced.
Flatten nested conditionals
Use when deep nesting makes code hard to follow. Early returns + guard clauses are the standard fix — and behavior must be preserved exactly.
The flattened function plus a trace confirming each test case still produces the expected output. If the trace is wrong, the refactor is wrong.
Modernize old syntax without changing behavior
Use when migrating from older JS/Python patterns to modern equivalents. This is safest when you commit before, then review the diff carefully.
A diff plus a change log. Review carefully — "modernization" prompts often slip in logic changes. Ask: "Did any line change behavior, not just syntax?"
🎯 Rules for safe refactoring with AI
- Commit before every refactor. You need a clean diff. If it goes wrong, `git reset` and try again.
- One refactor per commit. Extract OR rename OR split — never two at once.
- Tests first when possible. If a function has no tests, ask AI to write them before refactoring.
- Review the diff line by line. AI refactors often introduce subtle logic changes without noticing.
- Behavior preservation is the contract. If behavior changed, the refactor failed — no matter how much cleaner it looks.
❓ Frequently asked questions
What makes a good refactoring prompt?
A good refactoring prompt states the goal (what to change), the constraint (what must not change), and the verification (how to confirm nothing broke). Without constraints, AI will happily rewrite your entire file.
How do I know AI won't break my code during refactoring?
Two things: ask AI to preserve behavior explicitly, and require tests to pass. If you don't have tests, ask AI to write them first against the current code, then refactor.
Should I refactor in one big prompt or many small ones?
Small prompts, one change at a time. Refactor → test → commit → next refactor. Every large refactor should be a series of tiny safe steps.
What is the difference between refactoring and rewriting?
Refactoring changes structure without changing behavior. Rewriting starts from scratch and can introduce new bugs. Prefer refactoring unless the code is fundamentally wrong.
Can AI refactor code it doesn't understand?
Not well. Give AI the surrounding context — what the code does, who calls it, what edge cases exist. The more it understands, the better the refactor.