My Experience Refactoring a 3,000-Line File with Claude Code
I threw an entire legacy file at it and said "refactor this." The output looked plausible, but all the tests broke.
In the end, I went back to slicing it by function and having it work on them one by one. It doesn’t feel like human work has decreased—it feels like the units have gotten smaller. How is everyone else using it?
9 answers
If you start by writing the tests first, the results are noticeably different. But that's also work lol
I've gone back to the same approach too. In the end, breaking things into smaller units and keeping diffs small makes reviews easier and rollbacks simpler. Rather than AI being a silver bullet, it feels more like handing work to a diligent intern.
Isn't that outdated? These days, models handle around 3,000 lines in a single pass just fine. What did you run it on?
Got a source? Without knowing which model version and what prompt was used, this isn't a review—it's just venting.
I actually think splitting it by function is the proper approach. Human reviewers can't read a 3,000-line diff either. The key is making it a reviewable size, and expecting AI to handle that is a bit...
I disagree. It's not that the units got smaller—this is how it should have been done from the start. The problem was expecting to throw the whole thing in and have it work in one shot.
Well, I suspect that was because the prompt was written poorly. If you give it the full file context and explicitly state constraints like "never change public API signatures; passing tests is the only success condition," it handles even 3,000 lines pretty well. I'm actually the type to throw larger units at it; when you split things too finely, it misses dependencies between functions, and duplicate code piles up later. In particular, if it separately refactors things that should be extracted into shared utilities, the file triples in size. Broken tests are less the model's fault and more because you didn't run a verification loop.
Agreed—if you dump the whole thing in, it only looks polished on the outside while everything inside gets turned upside down.
I took the opposite approach. First, I ask the AI to analyze this file structure and just come up with a refactoring plan, then I review the plan and have it execute step by step. The planning stage gives you a dependency map, so you can see in advance where tests will break, and I've actually made it through full refactorings safely about twice. That said, I wired up a script to run tests at every step so it immediately rolls back on failure. Splitting it into three stages—plan, review, execute—felt much more efficient than having it do everything at once.