Searchable transcript of What Is AI Code Refactoring? Agentic AI & Safe Code Changes — IBM Technology (11:36). Search for a phrase, then click its timestamp to jump straight to that moment in the video.
Captions sourced from the original video on YouTube, published by IBM Technology. The video, its captions and all related intellectual property remain the property of their respective owners; AINotes claims no ownership. Provided for research, accessibility and search — see the Transcript Notice and Copyright Policy.
00:00 A large language model is, well, a probabilistic guessing machine, and these guessing machines are being used to rewrite production code. That's called AI code refactoring. And I suppose the question we have to ask is, is it really safe to let these guessing machines loose on enterprise code? Well, plain old code refactoring means changing a program.
00:27 It means taking a program's internal structure, so maybe it kind of looks a bit like this, and we want to change it, well we can add in something like this but what we haven't changed is its external behavior. To the outside world this thing still looks the same. So... If the external view doesn't change then I think the question is why bother doing code refactoring at all?
00:55 Well let me give you an example why you might want to do it. So let's take for example a variable called temp2 in a piece of code. Well temp2 tells anybody trying to read that code pretty much nothing. Renaming it to something a human can understand, maybe we're going to call customer state for example. That's refactoring and specifically that is refactoring for readability.
01:27 Or perhaps we've got a block of logic and that logic has over time ended up being copied and pasted a bunch of different times, and it turns out that that code block has got a bug. Well now that bug needs to be fixed in multiple places so pulling all of this code into a single place, where we can fix the bug just once. That is an example of remove duplication, or let's consider we've got a really really long function it kind of goes on forever and it's doing like a half dozen different jobs in here if we were to break
02:03 this up into different chunks then we would be refactoring to lower complexity. These are the benefits of code refactoring. The internal stuff really has changed for the better, but the external view around this, it remains the same. Now, if you skip all this cleanup for long enough, it compounds into something called technical debt. Which means that code changes can take longer than they should.
02:42 And even simple fixes might turn out to be not so simple. So code refactoring can help reduce technical debt. Now it turns out that a lot of this code refactor stuff comes down to pattern recognition, the same tired problems over and over again in a code base. And what's really good at pattern recognition? AI, right? AI models. Thank you for joining us.
03:04 Now, we'll get back to those concerns about deploying those AI models, AKA probabilistic guessing machines, to make changes to production code in a bit, but first, let's better define how AI code refactoring actually works. So I think we can divide this up into two categories. And the first category is inline refactories. So this is refactoring. That happens perhaps inside of an IDE or a code editor.
03:38 And here this happens working alongside the developer as they type in this code editor. So maybe it spots a clumsy variable name like you know temp2 and it suggests a better one, or the developer perhaps highlights a block of code in the IDE and then the tool kind of offers to pull it out into its own function something like that. So inline refactoring is for small local fixes and it happens directly in the code editor so that's inline.
04:07 Then really the other type which is a bit more recent is the agentic refactoring. Now what makes refactoring agentic? Well instead of suggesting one edit at a time the agentic tool gets handed a goal. Now this is something like "Upgrade this library to the latest version", that'd be a good goal. But it might also be something quite a bit more vague than that, like just "Clean up this whole module".
04:40 Well, with the goal set, agentic refactoring goes off and it works the problem across the entire code base on its own. So what's happening here that's different to inline is this is autonomous. It's running by itself. It takes advantage of an agent's ability to pattern match and keep a large code context in mind as it goes ahead and makes its changes.
05:03 But it also brings us back to those concerns about the guessing machine sweeping through hundreds of files, largely unsupervised, you know, autonomous. It predicts likely looking code so it might tidy up something that looked unused but actually was still in the code base for a reason, like maybe... I don't know, an obscure function that did something with leap years, for example.
05:27 Well, you only find out that you actually needed it next time the calendar ticks over to February 29th and by then it's too late. So, uh, yeah, this is going to need some serious guard rails. So let's talk about those guard rails, agentic refactoring manages the risk by working as a loop. So I'm going to draw this as a circle. The agent moves through the same steps in order round and round until the job is done, but different agents they might work slightly differently, but you know this is more or less kind of how it
05:55 works. So in the center, as I mentioned, what do we start with? We start with a goal that has been provided by the user. So the human developer has handed the agent some kind of goal to achieve. It might be upgrade the library, might be clean up the module, whatever it is. Okay. Now the process starts with a plan. So the agent works out an ordered list of steps of what it needs to change and in what order.
06:29 And it's going to keep updating that plan. Now to help make the plan, it has to do a read operation, which is to say it opens the files involved and then maps what's in them, like the functions in those files and the classes and kind of how they fit together. Then it goes into a search stage. Now search what exactly? Well, the agent searches the rest of the project and the libraries that the project depends on for related code.
07:00 So if there's a function that gets called from a bunch of other places, then it's the search stage that's gonna find all of those calls. Now we've got, at this point, the context pretty well established. So the agent moves on to the next step, which is report. So it comes back with findings ranked from high priority to low. Each with its own reasoning.
07:25 So a finding might read something like hey this function here is ignoring exceptions that's going to hide failures in production so here's the suggested fix. That would be a pretty useful entry in a report. Now so far our human developer hasn't done much beyond just setting this goal. Remember this is autonomous. But this is where the humans re-enter the loop.
07:50 Basically picking which findings the agent should fix from that report. So typically, at this point, we would have some level of human approval. Now, once we've approved what we should work on, the agent will most likely create some kind of snapshot of where the code base is today. This is the starting state, so the changes can be undone if necessary.
08:16 And then it goes in and it does its thing. So it actually will start changing the code and applying a patch to that specific line of code that needs patching. And it's doing that carrying all of this context from earlier in the loop. And then once that patch is done, we move to the step of verify, which is where the agent writes and runs tests and rebuilds the project.
08:44 And now that something's actually changed, it could show a diff against the starting snapshot and what we actually patched line by line. And a test breaks and we need to fix something, it can roll that back and we go back around this loop again and again. And this loop here is the answer to the question from the start of the video, which is to say, you know, is AI code refactoring safe?
09:10 Well, as you can see in this loop, the agentic probabilistic guesses they don't go straight into the code base. So remember that obscure leap year function? I mean that probably wouldn't have actually ended up getting removed in this loop, because either a human coder here wouldn't approved it in the in the report that it received or a failing test case in the verify stage would likely have caught it.
09:37 So this really is just a case of running tests and running builds. And that means that this whole loop can actually slot in to a CI/CD pipeline, which can run alongside everyday development. The agentic AI is just like another developer. Now, before we leave the loop, there's a couple of other things that I did wanna point out. And the first is that this patch step here, it can actually skip the probabilistic guessing entirely and use deterministic methods.
10:11 Now some tools parse the code into a structured tree for example an abstract syntax tree or a richer version of that which is a lossless semantic tree that also tracks types and formatting and then makes changes as an operation on that tree. So take a rename for example, it would find the symbol in the tree and then update every reference to it and that stuff is deterministic rather than probabilistic.
10:35 And then there's also... Reinforcement learning. So every fix in this verify step that gets accepted or rejected is a training signal and every test case that passes or fails that's also a training signal. So the models get trained to favor the changes that get accepted and pass so the AI code refactoring suggestions are going to improve over time. So where does this leave us?
11:07 Well, AI code refactoring can help pay down technical debt. By making internal changes across a large code base, but it does need to be done in a way that verifies the output, and at least for today, still keeps humans in the loop. Thank you.
Yes, but only with guard rails: human approval, snapshots, deterministic operations where possible, tests, builds, diffs, and rollback must prevent autonomous changes from going directly into production code.
AI code refactoring can help pay down technical debt.