Technical
How I Use Claude Code for Multi-File Refactoring
Renaming a module across a codebase used to take an hour of find-and-replace anxiety. One missed import in a test file and your CI pipeline fails at 11pm. Now I tell Claude Code what I want changed, and it handles every file, every import, and every test.
The Old Way
Before AI agents, multi-file refactoring meant:
- Find every reference to the old name using grep or your IDE's search
- Replace each one manually (or with regex, and pray nothing breaks)
- Run the test suite to find the references you missed
- Fix the broken tests that reference the old name
- Run tests again and repeat until everything passes
This process was error-prone and tedious. String-based find-and-replace does not understand Python imports, TypeScript modules, or the difference between a variable name and a comment.
The Claude Code Way
Now I describe what I want in plain English:
claude
> 'Rename the module src/utils/helpers.py to src/utils/text_processing.py.
Update all imports across the project. Run tests to verify nothing broke.'Claude Code reads the project structure, finds every import of the old module, renames the file, updates every import statement in every file, runs the test suite, and reports the results. All in about 30 seconds.
Real Refactoring Scenarios
Here are refactors I have done with Claude Code in the last month:
Scenario 1: Extract a router. 'Move the subscriber endpoints from posts.py into a new file src/routers/subscribers.py. Update main.py to mount the new router at /subscribers.'
Scenario 2: Change a field name. 'Rename the tags field to categories across all models, endpoints, tests, and frontend components. Update the API documentation strings too.'
Scenario 3: Reorganize project structure. 'Move all database-related code from utils/ into a new db/ directory with separate files for connection, queries, and models. Update all imports throughout the project.'
Each of these would have taken 30-60 minutes manually. Each took less than a minute with Claude Code.
The Key: Clear Instructions
Multi-file refactoring works well with Claude Code because:
- The scope is clear: 'Rename X to Y everywhere' has no ambiguity
- The verification is automatic: 'Run tests to confirm' gives a clear success criterion
- The pattern is mechanical: Find references, replace them, verify the result
These are exactly the tasks AI agents excel at. They are repetitive, they require attention to detail across many files, and they have a clear success criterion.
When to Be Careful
Watch out for edge cases that agents can miss:
- Dynamic imports:
importlib.import_module('old_name')will not be caught by static analysis - String references: Database queries or config files that reference the old name as a string
- External consumers: If other projects import your module, the agent cannot update them
Always review the diff before committing. Claude Code is fast and thorough, but it is not infallible. A quick git diff review catches the edge cases.
Learn more about Claude Code capabilities at the official documentation.
RELATED READING
The Consulting Shift I Am Making In Year Two
After a year of writing and building, my consulting practice is changing shape. Shorter engagements. Sharper outcomes.
ReadThe Frontend Shift: Shipping Less JavaScript In Year Two
A year ago I reached for Next.js for everything. This year I often reach for nothing.
ReadThe Serverless Lesson I Would Write On A Sticky Note
After a year of shipping serverless projects, one rule explains most of the wins and all of the losses.
Read