Refactoring, AI Style

Published on September 9, 2025

You know what the T in GPT stands for? Transformation. And what is refactoring if not transformation? Refactoring is changing the code, without changing the functionality. And I don’t know if you’ve tried AI code refacotring yet, but it’s a special

Now, we’ve been making changes to the code without AI for years. Take Renaming, for example. We can do this by “find and replace”, for example. No brains, just brute force.

Then there’s a smarter version – using the IDE. Most IDEs index all used names of methods and variables. You can rename something, and they will find and rename them for you.

There are a whole lot of refactoring patterns, and you can do them manually, or depending on your IDE and language, the IDE will do that for you.

But then there are bigger changes in code. Here’s where we can call up the genies.

Test Refactoring

Here’s an example. I’ve created a couple of tests using a framework, let’s say UnitTest in Python. And now I want to “translate” them to PyTest. The genie is able to grant these wishes very quickly. Since LLMs are trained on a lot of code, they know a lot of patterns. So when I asked about my ugly API test:

I didn’t know what I’d get. What I got are tests using a wrapper class, called APIClient:

Ah, I smell a refactoring pattern! Extract Class – based on two famous principles in Software – encapsulation and abstraction. We’re hiding implementation details behind a higher level interface. It’s exactly that.

We can take this further, though.

It’s Refactoring, But with Patterns

In our Bigger Better Bookstore, we have an API to create a book. It has all kinds of required and optional parameters. One way is to call the API is like this:

Which is ok, but quite verbose. And with more and more fields, it would be harder to set up, especially with a lot of hierarchical data.

But, hey, I’ve got a genie!

The builder pattern is a way to refactor tests like ours, more readable, and fewer lines.

Much better, don’t you think?

Another way I used AI more than once, is to use the Page Object Model, for web apps. The translation is using the same principles we discussed, encapsulation and abstraction, and results in better tests.

Like magic, right?

Dark Magic Ahead

You see, every time we refactor, we make an assumption. Even if it’s just renaming a variable. We assume that although we changed the code, it will still run as before.

we’re talking about real refactoring. The original definition: no changes to functionality.

For small refactoring changes, we learned to trust the tools, or ourselves. But with big changes like that? Sometimes across multiple files? Maybe even across projects? Can we trust them?

There can be big implications. For starters, this can be “more than refactoring”. I’ve seen code lines added, removed and changed, without me asking.

Second, there could be effects, other than functional. Maybe the code changes introduce extra network calls, or writing to disk, which can impact performance. Maybe it opens the server to attacks. Maybe it changes accessibility features.

So what do we do? We want that magic!

We are willing to pay!

So how do we wield this magic without getting burned? It comes down to a simple process:

Verify with Tests: Your existing test suite is your safety net. Run it immediately. If you don’t have tests, your first request to the genie should be, ‘Write comprehensive tests for this code before we refactor it.’“First and foremost, we need to review the changes. Don’t let the genie slide in some code we don’t understand. And approve it blindly.

Prompt in Small Batches: Ask for one specific change, not a whole architectural redesign.

Review and Understand: Scrutinize every line. If you don’t understand why the AI made a change, ask it to explain its reasoning. Never commit code you don’t understand or can stand behind.

AI is changing how we write and maintain code every day. If you want more practical tips on leveraging AI in your development workflow, follow me on X and LinkedIn or subscribe to my newsletter for more insights. And memes.

The post Refactoring, AI Style first appeared on TestinGil.