Prompt
How do I refactor old code faster with an AI assistant?
Latest observation
Here’s a practical way to refactor old code faster with an AI assistant without turning the process into chaos.
1) Start with a “safety net”
Before changing anything, use the AI to help you create or improve:
- unit tests
- integration tests for critical paths
- snapshots/golden tests for legacy behavior
- a quick characterization test suite if the code is messy
Prompt example:
“Here’s a legacy function. Help me write characterization tests that capture current behavior, including edge cases and weird inputs.”
This is the biggest speed multiplier, because once behavior is locked in, refactoring gets much safer.
2) Refactor in small slices
Don’t ask the AI to “modernize this whole module.” Instead:
- pick one function, class, or file
- ask for one change at a time
- keep diffs small and reviewable
Good prompt:
“Refactor this function to reduce nesting and improve readability, but keep behavior identical. Don’t change the public API.”
3) Ask for alternatives, then choose
AI is useful for generating options quickly:
- simpler structure
- better naming
- extracting helpers
- removing duplication
- isolating side effects
Prompt example:
“Give me 3 refactoring approaches for this code, with pros/cons and risk level.”
Then pick the safest one.
4) Use the AI as a “pair programmer,” not an autopilot
A productive loop is:
- paste a small piece of code
- ask what it does and where the risks are
- ask for a refactor
- ask for a test plan
- review the diff
Prompt example:
“Explain this code in plain English, identify hidden dependencies or edge cases, then propose a step-by-step refactor plan.”
5) Preserve behavior explicitly
Tell the AI what must not change:
- outputs
- exceptions
- performance constraints
- public method signatures
- serialization format
- database queries/schema assumptions
Prompt example:
“Refactor for clarity only. Do not change return values, exceptions, log messages, or SQL behavior.”
6) Use the AI to hunt for cleanup opportunities
Once the code is safe, ask the AI to look for:
- dead code
- duplicate branches
- repeated validation
- long parameter lists
- magic numbers/strings
- poor naming
- hidden state
- mixed responsibilities
Prompt example:
“Scan this file for refactoring opportunities ranked by impact and risk.”
7) Have it generate tests after each change
After every refactor, ask:
- what behavior could break?
- what test should be added?
- what edge case is missing?
This helps you avoid “looks cleaner, secretly broken.”
8) Use it to understand legacy code fast
Old code often has tribal knowledge encoded in it. Ask the AI to:
- summarize the module
- trace data flow
- explain dependencies
- identify side effects
- infer invariants
Prompt example:
“Trace the path of user input through this code and list all side effects and assumptions.”
9) Keep a strict review checklist
Even if AI writes the code, you should verify:
- tests pass
- behavior matches old behavior
- no unintended API changes
- error handling still makes sense
- logging/metrics didn’t break
- formatting/linting are clean
10) Best workflow in practice
A fast workflow looks like this:
- pick a small legacy area
- ask AI to explain it
- write characterization tests with AI help
- request a tiny refactor
- run tests
- compare behavior
- repeat
Prompts that work well
- “Explain this code in plain English.”
- “What are the risks of refactoring this?”
- “Write tests that capture current behavior.”
- “Refactor for readability only.”
- “Extract pure logic from side effects.”
- “Find duplication and suggest consolidation.”
- “Give me a step-by-step migration plan.”
- “Review this diff for behavioral changes.”
Common mistakes to avoid
- asking for too much at once
- not writing tests first
- trusting AI code without understanding it
- refactoring while changing behavior unintentionally
- pasting giant codebases into one prompt
- not specifying constraints
If you want, I can also give you:
- a repeatable refactoring prompt template, or
- a workflow for refactoring a legacy file in under 30 minutes.