Or: Allowing AI to take care of your code
In this blogpost, I’ll walk you through the state of research when it comes to applying AI in software refactoring. We’ll take a look into what scientists are focusing on and where we can find enough space to add more contributions for the ”beautiful” operation called refactoring. All data discussed is extracted from the article “Artificial Intelligence Methods in Software Refactoring: A Systematic Literature Review“, which I’ve co-authored and presented at SEAA 2024.
Who is this for?
- Developers: You’re in the trenches, writing and refactoring code every day. This post will show you where AI sits and where it can/could make your life easier
- Researchers: I’ll be laying out some key trends from a research perspective on how AI progressed in the software refactoring domain, what gaps are there and what could be improved
Figure 1: Percentage of articles addressing specific lifecycle change
AI in Software Refactoring: What’s the Big Deal?
Refactoring has always been about improving the structure of existing code without changing its functionality. AI takes this to the next level by automating parts of the refactoring process that typically require a lot of manual work.
There are multiple stages when it comes to refactoring, which are called refactoring lifecycle changes. In order to understand this, imagine refactoring as a multi-level quest, with each level being a different stage of the process. These stages help guide the journey you have to take to improve your code. It starts with detection, where you identify the parts of your code that need some fixing. Then comes prioritization, where you figure out which areas should be fixed first to make the most impact. Recommendation follows, offering the best strategies for cleaning up your code. Once changes are made, testing ensures everything still works smoothly. Afterward, documentation keeps track of the changes for future reference. Finally, prediction lets you anticipate how the refactoring will improve maintainability and performance in the long run. These stages together form the backbone of the refactoring process.
The scientific study mentioned beforehand analysed a pool of 156 studies that talk about AI used in refactoring. While it is not a humongous number, it represents a verified set of data that we can use in order to draw conclusions. In figure 1, you can observe the distribution of such articles when it comes to lifecycle changes.
As AI has seen a great evolution in the recent years, it can now step in during various phases of the refactoring lifecycle. Here’s what the state of research tells us and how AI affects each stage:
- Detection (37.18%): AI is heavily used to identify code that needs refactoring, like a pair of superhuman eyes scanning for inefficiencies.
- Recommendation (32.69%): AI suggests what type of refactoring should be applied—offering a kind of “auto-complete” for your code.
- Prediction (10.9%): AI methods are increasingly being used to predict where refactoring will most improve software quality.
-
Documentation (9.62%): AI helps keep the logs in order, automatically updating the documentation as you make changes, so you don’t have to remember every little tweak.
-
Prioritization (8.33%): AI figures out which parts of the codebase need the most attention, helping you focus on what will have the biggest impact first.
-
Testing (1.28%): AI ensures that refactoring hasn’t broken anything, but this stage currently sees the least AI involvement, leaving plenty of room for growth.
What AI Methods Are We Talking About?
Let’s dive into the toolbox. When it comes to AI-powered refactoring, there are a few standout methods that are making serious moves. First up, Random Forests—think of it as an army of decision trees, all working together to spot code smells and suggest fixes with high accuracy. Then there’s Genetic Algorithms, where code evolves through mutation to find the best refactoring option, like natural selection for your software.
Support Vector Machines (SVMs) are great at sorting code into clean vs. needs-fixing categories, while Convolutional Neural Networks (CNNs), usually famous for image recognition, are now recognizing patterns in code. Decision Trees? They’re like a flowchart on steroids, guiding refactoring decisions based on your code’s current state.
We also have Natural Language Processing (NLP) techniques like BERT and TextRank, making sense of your code comments and documentation. Graph Neural Networks (GNNs) map out relationships between code components, identifying complex patterns that traditional methods might miss.
What’s Working (And What’s Not)
AI is doing a solid job in refactoring, particularly in detection (37.18%) and recommendation (32.69%). These are the stages where AI really shines, spotting code that needs fixing and suggesting improvements like an auto-complete for your refactoring process. Prediction (10.9%) is also making progress, helping forecast the impact of changes on code quality.
However, prioritization (8.33%) and testing (1.28%) are still playing catch-up. AI hasn’t made as much of an impact in figuring out which code to tackle first or ensuring everything works after refactoring. Interestingly, documentation (9.62%)—often an afterthought—sees more AI involvement than testing and prioritization, though there’s still a lot of room to grow in automatically keeping track of changes.
So, while AI is already making refactoring smarter and faster, we’re still waiting for it to fully tackle these other stages.
How Can You Use This Today?
If you’re a developer, the good news is that AI tools are already out there, ready to jump in and help clean up your code. These tools can scan your codebase, recommend refactoring steps, and even predict where changes will make the biggest difference in terms of maintainability and performance.
For researchers, there’s a lot of untapped potential. Prioritization and testing are still lagging behind in AI support, which means there’s a big opportunity to develop better models that can fill these gaps. Focusing on these areas could drive the next wave of innovation in AI-driven refactoring.
My own view
I use AI in my day-to-day work, whether to write code or to make it better. I use tools like GitHub Copilot in order to get code recommendations and ChatGPT to, among others, break more complex code into smaller parts (which is a good refactoring practice). These tools provide speed and allow you to concentrate on delivering.
Also, there is a plethora of refactoring activities you can implement, for more expertise on this I recommend reading the book “Refactoring” by Martin Fowler.
There is a BIG, IMPORTANT point when using AI and you need to be very careful: every code generated by AI has to be taken with a pinch of salt and properly validated. The model doesn’t work well with abstractions and cannot think like a human does, hence it can introduce critical bugs and invent stuff that doesn’t exist. If you don’t have the experience to decide if a piece of code is good or bad, then better write it yourself. And if you do, properly check it before using it. Also, if the code generated is gibberish or, at the first impression, seems to make no sense, don’t bother to understand it. Either re-generate it or write it yourself, you’ll save much more time and nerves.
Key Takeaways
- AI is stepping up in the refactoring game, with a focus on detection, recommendation, and prediction.
- The big players? Random Forests, Genetic Algorithms, and NLP tools like BERT are leading the charge.
- There’s still plenty of room for improvement, especially in areas like prioritization and testing, which could use some extra AI love.
- Developers can start using AI tools today, while researchers have a chance to push the next wave of innovation forward.