Refactoring
Claude in VS Code: Pair Programming
Refactoring is where Claude in VS Code earns its keep. It can rename, restructure, simplify, and modernise code — but only if you give it the right scope. Too broad and it changes more than you intended. Too narrow and it misses dependencies. This chapter covers how to scope refactors correctly, how to use the diff view to stay in control, and how to handle changes that span multiple files.
The Core Risk: Scope Creep
The most common problem with AI-assisted refactoring is that Claude improves things you didn't ask it to improve. You say "rename this variable" and get back a rewritten function. You say "simplify this method" and Claude also changes the calling code.
This isn't necessarily wrong — Claude may be right that the related code should change. But it means you're reviewing more than you planned, and changes you didn't request can introduce bugs you won't notice until later.
The solution is explicit scope constraints in every refactoring prompt.
Scoping a Refactor
Ctrl+I with selection. Fastest for single-expression changes.Constraint Phrases That Prevent Over-reach
These phrases, added to any refactoring prompt, keep Claude within the scope you intended:
| Phrase | What it prevents |
|---|---|
Don't change anything outside this function |
Claude modifying callers or related functions it thinks "should" also change |
Keep the public interface identical |
Parameter names, return types, or method signatures being altered silently |
Preserve existing behaviour exactly |
"Improvements" that change edge-case handling or error behaviour |
Show me every file you'll touch before making changes |
Surprise edits to files you didn't know were affected |
Only rename — don't restructure the logic |
Claude taking a rename request as an invitation to also clean up the surrounding code |
Make the minimum change to achieve this |
Over-engineering or adding abstractions you didn't ask for |
Using the Diff View
When Claude makes changes via the inline editor (Ctrl+I), VS Code shows the result as a diff — additions in green, removals in red — before anything is applied. This is your main safety mechanism for refactoring.
Read the diff carefully before accepting. Ask yourself:
- Did Claude change anything I didn't ask it to?
- Is every removed line intentionally removed, or did something slip?
- Are the added lines functionally equivalent to what they replace?
- Does the diff touch code outside my selected scope?
Ctrl+Z to undo just the specific line, or make a manual edit. Rejecting a large diff because of one line wastes all the good work.
Common Refactoring Requests
Multi-File Refactors
Some refactors touch more than one file — renaming a class, changing a function signature, moving a module. These need more care because the diff view shows one file at a time and it's easy to miss an affected file.
Ctrl+Shift+F) to search for the old name. Compare Claude's list against the search results — if they differ, Claude missed something or the search found false positives.Ctrl+Shift+F. Any remaining hits that shouldn't be there are missed updates.F2) is often faster and more reliable than asking Claude. It uses the language server to find every reference automatically. Use Claude for renaming when the change also involves restructuring, or when the language server doesn't cover the file type.
Refactoring Patterns Claude Does Well
- Early returns — flattening deeply nested conditionals
- Extract method/class — pulling a coherent block of logic into its own function or class
- Modernise syntax — updating old patterns to current language idioms (e.g. f-strings, optional chaining, pattern matching)
- Remove duplication — identifying repeated logic and centralising it
- Clarify naming — renaming cryptic variables and functions to descriptive names
- Convert callback to async/await — restructuring callback-based code to async equivalents
- Add type annotations — inferring and adding types to untyped Python or TypeScript code
Refactoring patterns Claude does poorly
- Performance optimisation — Claude can suggest patterns but rarely knows your runtime bottleneck. Profile first, then ask Claude to apply a specific optimisation.
- Architecture-level restructuring — moving entire modules, changing dependency direction, introducing new layers. These benefit more from a planning conversation than a direct refactoring request.
- Database schema changes — Claude can write the migration but doesn't know your data volume, index strategy, or production constraints. Treat its output as a starting point.