Technical
Orchestrating Claude Code for Multi-File Refactors
Renaming a function across 40 files used to be a day of pain. Find-and-replace breaks things. Doing it by hand guarantees mistakes. This is the kind of mechanical work Claude Code was built for. Here is how I structure a multi-file refactor so the agent does not lose context.
Start With the Target State
I describe what the code should look like after the refactor, not what the refactor steps are. The agent figures out the steps. My prompt says:
Rename every use of `SubscriberCreate` to `SubscriberInput`
across all Python files in src/. Update imports, docstrings,
and the OpenAPI schema generation. Keep tests passing.That is the target. The agent scans, plans, and edits.
Let the Agent Plan First
For big refactors I ask for a plan before changes: 'List every file that needs to change and what the change is. Do not edit yet.' The agent returns a file-by-file plan. I sanity check, approve, and then say 'execute.'
This two-step approach catches plan errors before they become code errors. It adds two minutes to a refactor that would otherwise take an hour.
The Test Harness Is the Safety Net
A good test suite is what makes agent refactors safe. The agent edits, runs tests, and reports results. If tests break, the agent iterates or reverts.
If you do not have tests for the code being refactored, write them first. I spent an hour writing tests for a file I was about to refactor. The refactor itself took 10 minutes. The tests are permanent insurance.
What I Still Review
- Import changes: subtle ordering issues the agent misses
- Docstring accuracy: the agent updates names but sometimes stale descriptions remain
- Public API impact: does this rename break something a consumer relies on
- Migration needs: if a database field got renamed, is there a migration
These are human checks. The agent handles the volume of edits; I handle the judgment about what the edits mean.
The Git Workflow
I always create a branch, let the agent work, and review the diff before merging. Even with passing tests, I read every hunk. The agent is fast, not infallible.
git checkout -b refactor/subscriber-input-rename
# agent does its work
git diff main
# human review
git pushWhy This Beats Find-and-Replace
Find-and-replace matches strings. The agent understands code. If SubscriberCreate appears in a comment about an unrelated type, it is left alone. If it appears as a substring of SubscriberCreateEvent, the agent knows not to touch it. This contextual awareness is the whole game.
See the Claude Code documentation for the multi-file editing capabilities and safety guarantees.
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