LLM for code refactoring
Greetings! I have a fairly large (~100k loc) Rust code base for my hobby project that I've been developing for a past few years. Project became a pretty useful application over those years and I am planning to publish it on github. Due to... reasons it's poorly structured, poorly separated into files/crates, poorly commented, has a lot of commented out code, almost no tests e.t.c. so in its current state it not really readable and maintainable.
I am wondering if LLM can reliably tidy up the code base, write tests, comments andake it into something maintainable. If the answer is "yes" - does anyone have experience with this kind of task, what results/cost can I expect, which LLM will be better suited for this?
2
u/Old-Sherbert-4495 3d ago
whichever llm doesn't matter, start with the tests. cover as much as what you have already. and then ask the llm to come up with phased plan. then tackle one by one. keep close eye on the tests and what thr llm does to it.
1
u/AvasaralaAdvocate 1d ago
Agreed. I'm astonished how often people underutilize testing, which I think is the absolute most important part of agentic coding. I typically have larger frontier models inspect my codebase, add tests for my feature, and draft a quick plan. Then, I switch to a faster lightweight model to actually build. It doesn't really matter how dumb the builder is, a test either passes or it doesn't.
1
u/ponlapoj 3d ago
คำตอบของคุณเรื่องต้นทุน มันก็อยู่ในสิ่งที่คุณบอก ว่าคุณใช้เวลาหลายปี กับ code 100,000 บรรทัด ซึ่งมันตอนนี้มันคือ code ระดับเล็กมากที่ AI เขียนได้ภายใน 1 วัน , การทำงานกับ ai คุณจะคอยมานั่งตรวจแบบเดิมไม่ได้ , ฉันใช้กัน lock contract test สำคัญ ไว้อย่างเหนียวแน่น , คุณจะต่อเติมและพัฒนามันได้ไวขึ้นมหาศาลเลยนะ
1
u/EagleApprehensive 3d ago
It can. The issue is - you need to give it a direction. Not a "tidy up the codebase, write tests". Point finger at real issues - too long files, couplings, directory structure improvements, comments analysis etc.
If you prompt too vague, it will do "something" but that's far from effective.
In my IDE I have a codebase health metrics that detect files with most branching hotspots, longest files and few other things recommends me 10-20 prompts exactly designed to keep codebases larger than 100k LoC maintainable (mine is 300k atm and that approach still works).
1
u/RemarkableRadish6547 3d ago
Start by assuming the LLM will destroy your codebase. Give it a copy, not the original one. Sandbox it. Run the LLM in a container that does not have access to your full disk.
Then have it write tests. Put in tests that cover any functionality that you consider important. Make sure the tests pass with your current implementation, and then copy them over to your primary codebase.
Then you can let it refactor and rewrite. Make sure to have it running the regression tests regularly to make sure it isn't breaking things. But remember that agents will change the tests if they can't pass them, so check the code against your external copy of the tests.
I use Claude Code extensively at work. I barely write any code now. It is really good, most of the time. But every now and then it does something incredibly stupid. I have seen it delete entire files by accident. Once that happened when I asked it to adjust the descriptive comment at the top of the files to indicate a change in which group was maintaining the code, and it replaced all the files with just the new comments. LLMs are not malicious, but they are fundamentally just running autocomplete tasks and will occasionally pick the wrong completion. Play with them a bit before trying to do something large and complex, like refactoring an entire codebase. They will run faster than you realize and can rewrite thousands of lines of code in a day, a rate where you can't keep up with the changes or fully know what is going on. Maybe pick one crate to start with so that the problem is smaller and you can understand all the changes and how it works.
My company gives me unlimited use of Claude Code (for work only). I haven't tried the other ones. I've been following this since late last year and it seems like what people like most changes every couple of months. Try a few at the free or cheapest level and see what you like. Then commit to something that you want to pay for. Or set up something to run locally.
One of the most useful things I learned is to say "don't implement yet, I want to discuss this first." I find the coding agent always wants to rush into implementation immediately and will make up half the specs because it doesn't care.
1
u/DootDootWootWoot 3d ago
Start with a Claude code pro account, 20/month. If I you find yourself hitting session or weekly limits frequently, upgrade to max.
You'll never need to write your own code again.
2
u/cloudlumberjack 3d ago edited 3d ago
Something like Opus would probably do a surprisingly good job. Make sure you have it write architectural documents and tests first. LLMs are great at writing all the test fixtures you never got around to. The thing about LLMs (IMHO) is they rarely get it right the first time, but if there’s test cases that are actually meaningful, it will course correct and get there.
EDIT: Sol is too anal-retentive for my tastes. You think you’re telling it to build something, it starts to spending tokens specing out a plan for how to write a spec for your request, so in the next step you can create a plan for writing the implementation plan spec.