Can AI Refactor Bad Code?
Reading time: 3 minutes
Keywords: AI, refactoring, legacy code, technical debt, clean code
Refactoring bad code is one of the most satisfying parts of software development. But what happens if you let AI help you do it? Can AI actually improve messy code — or does bad code simply produce bad AI output?
We often say that bad input leads to bad output. That’s true for AI models as well. They’re trained on data, and quality matters. But refactoring is different from greenfield development. The intent already exists in the code — even if it’s poorly expressed. That’s where AI can surprisingly help.
I’m not a hardcore TDD developer. I know the theory, I respect it — but in practice, writing tests isn’t always my favorite task. AI has changed that for me. Generating quick validation scripts or test scaffolding makes experimentation safer and more enjoyable. It’s not perfect testing, but it’s better than none.
Technical debt is where things get tricky. Adam Tornhill, author of Code as a Crime Scene, has pointed out that AI-generated code is often verbose — and verbosity can increase maintenance costs. That resonates with me. But here’s the nuance: clean code is often verbose too. The difference is intent. Human-written clean code usually reflects deliberate design decisions. AI-generated verbosity doesn’t always.
Recommended by LinkedIn
AI is especially valuable when dealing with legacy code. Asking it to explain what a function actually does — in plain language — can save hours. I’ve refactored many systems simply because understanding them was too painful otherwise.
I’ve also seen how poor technology choices turn into long-term technical debt. In one project, Visual Basic was used simply because one developer liked it. Later, no one else knew the language, and deployment depended on fragile runtime versions. AI could easily repeat this mistake if we let it generate code in tools the team doesn’t own or understand.
So the question isn’t whether AI can refactor code. It’s whether we can guide it responsibly.
What’s your experience — has AI helped you clean up code, or just helped you write more of it?
#SoftwareDevelopment #Refactoring #AIDevelopment #TechnicalDebt #CleanCode
It was many years ago I wrote code professionally, so take this for a mere guess and perhaps hyperbole. Many decades ago developers worried about optimized machine code. Compilers improved and they worried about optimized source code (perhaps most common in c/c++). Then compilers got so good they optimized better than any human. The devs focused in improving readability and maintainability. Are we perhaps at the last decade of programming languages at all? Perhaps at some point we don't version the source code, but rather the prompts? Initially these prompts will transition through a programming language, but it's a question of time and economics before it goes straight to binary. A counter argument is that "how will we review it?" but that's already a question of chance in any reasonably big source code. Focus will probably shift to verification and validation.
It can write good well structured code in the hands of a proficient developer. But, refactoring code is tricky, it's really hard making AI agents being careful not causing regression bugs. It doesn't have the fingertip feel to realize that it's changing sensitive things in all cases... Yet
Interesting! It was a long time since i coded but started with Lovable, which is not coding as usch but as close I can get nowadays. 😂 Anyway, I started a small project to test how it was to promt-code and it event quite good and I had a prototype up and running in no time. After some time I ran it to some problems/issues with slow and a bit buggy responses. So I put everything on github and started using ClaudeAI as reviewer. What a difference it made! 🙃 Immediayely it found ~10 bugs/issue and the suggestions i used in Lovable as promts and after some iterations/reviews(4) my application became a lot better. Was like working with a team and using them together. As my sounding board I used chatGPT for inputs on my thought and brainstorm more features. 😄 👀 If the code is clean/good? No clue but it works really good! 😁