MacMusic  |  PcMusic  |  440 Software  |  440 Forums  |  440TV  |  Zicos
code
Search

Refactoring AI code: The good, the bad, and the weird

Monday December 2, 2024. 10:00 AM , from InfoWorld
Generative AI is no longer a novelty in the software development world: it’s being increasingly used as an assistant (and sometimes a free agent) to write code running in real-world production. But every developer knows that writing new code from scratch is only a small part of their daily work. Much of a developer’s time is spent maintaining an existing codebase and refactoring code written by other hands.

What’s it like doing maintenance work when those hands aren’t human, or were generated by an AI copilot? Can AI tools like GitHub Copilot, Vercel’s v0, or Cursor IDE be used to lighten the load? I spoke to real-world practitioners to find out how this next phase of the AI revolution is playing out.

AI written code: Useful, sometimes weird

All the developers I spoke to expressed varying degrees of enthusiasm over the usefulness of generative AI as part of the development process. They also acknowledged that AI-generated code has its quirks—and those quirks can make it tricky to maintain and refactor.

Dev Nag has been in the trenches with AI coding tools for several years and is the CEO of QueryPal, a software company focused on AI-powered ticket generation. He describes the process of refactoring and maintaining AI-generated code as surprisingly challenging. “The code often lacks consistency in style and naming conventions, which can make a codebase feel disjointed,” he says. “I’ve spent many hours cleaning up and standardizing AI-generated code to fit a project’s conventions.”

Dhaval Gajjar, CEO of IT services and consulting company Pranshtech Solutions, CTO of SaaS development company Textdrip, and an experienced software developer, agrees. “AI-based code typically is syntactically correct but often lacks the clarity or polish that comes from a human developer’s understanding of best practices,” he says. “Developers often need to clean up variable names, simplify logic, or restructure code for better readability.”

To Travis Rehl, CTO at Innovative Solutions, which migrates, modernizes, and builds next-gen systems on the cloud, the oddness of working with AI-written code in order to refactor or maintain it can go deeper. “When the AI has employed unfamiliar patterns or libraries, it can be challenging to refactor without a deep understanding of these choices,” he says. “There’s also the risk of breaking intricate dependencies that the AI might have created. It’s definitely a different experience. You’re often working with code that feels both familiar and alien at the same time. The AI might use approaches that seem unconventional to human developers, leading to ‘Why did it do it this way?’ moments.”

Nag and Gajjar both note that AI-generated code can be more complex than human-written code that achieves the same results. According to Gajjar, “AI tools are known to overengineer solutions so that the code produced is bulkier than it really should be for simple tasks. There are often extraneous steps that developers have to trim off, or a simplified structure must be achieved for efficiency and maintainability.” Nag adds that AI can “throw in error handling and edge cases that aren’t always necessary. It’s like it’s trying to show off everything it knows, even when a simpler solution would suffice.”

There’s one bit of showing off that Innovative Solutions’ Rehl appreciates. “AI also does things like add a ton of commentary around what a function does. That’s a double-edged sword,” he notes: “it’s useful for humans, but it also adds a lot of bloat to a codebase. But think about the next time you use the AI: You want some description as to the purpose of that function that the AI can read again later to understand the business context around it.”

AI eats its own dog food

Despite these quirks, the developers I spoke with thought that AI-generated code had its place within the software development lifecycle. In fact, they said that AI tools could be helpful in the code maintenance and refactoring process. Somewhat ironically, AI tools can even be used to overcome some of the flaws found in AI code to begin with.

For instance, Rehl at Innovative Solutions deploys AI tools for both code analysis and automated refactoring. “AI can quickly analyze large codebases and identify areas that need refactoring, potential bugs, or optimization opportunities,” he said. “For simpler refactoring tasks, like renaming variables or extracting methods, AI tools can perform these operations across the entire codebase with high accuracy.” QueryPal’s Nag noted that he’s “used AI to great effect for changes across a codebase, like updating deprecated API calls.”

Because commercially available AI tools are trained on best practices and patterns they’ve learned from vast codebases, they can also be deployed to suggest improvements that might not be immediately obvious to the human eye. “AI tools are excellent at identifying patterns and suggesting improvements, which can speed up the refactoring process significantly,” said Nag.

“Tools like GitHub Copilot propose code simplification, correction of inefficiency, or even the restructuring of logic once identified from some patterns,” added Pranshtech’s Gajjar. “It can help with auto-repetitive tasks, clean boilerplate code, or even hinting towards those parts that demand refactoring.”

Rehl described a practical situation in which he was able to use AI tools to refactor AI-assisted code. “The AI had created a sophisticated React component structure, but it didn’t align perfectly with the data model I had set up in the back end,” he explained.

Refactoring this required a careful balance of preserving the AI’s efficient component design while adjusting it to fit our specific data flow. What I found particularly helpful was using the AI (in this case, Cursor IDE) to assist in the refactoring process itself. I could describe the changes I needed in natural language, and the AI would suggest code modifications. This created an interesting loop where AI-generated code was being refactored with the help of AI, with human oversight guiding the process.

We still need humans in the loop

None of the developers I spoke to suggested that AI was ready to be set loose on a codebase—at least not yet. Jason Wingate, a developer and the CEO of brand development company Emerald Ocean Ltd., said that AI tools allowed him to accelerate refactoring, but human oversight was still key. “Always review and refine AI-generated code changes,” he said.

Wingate described a fundamentally iterative process of coding with AI assistance. “The most basic way would be to ask for refactoring suggestions and give it a chunk of the code. Include basic information about the language, coding standards, and conventions. Depending on what you’re looking for exactly, you can drill down with more questions of what exactly you’re looking to achieve.” Based on your goals—performance? readability?—you could either implement the suggestions yourself or get the AI to do it, he said. “Then review it, and (most likely) do it again.”

Wingate also cautioned developers to watch out for hallucinations and run tests to ensure the tool followed prompts correctly. “You may put ‘using Sarah’s coding standards’—which means absolutely nothing—and it still may say ‘Sure! I’ll use Sarah’s coding standards!” he said.

QueryPal’s Nag also noted the need to carefully review AI-generated code. “In my experience, the key to successfully using AI in both initial development and refactoring is to treat it as a very knowledgeable but sometimes unreliable junior team member. You wouldn’t let a new hire push code directly to production without review, and the same goes for AI-generated code. I always ensure that experienced developers on my team review and adjust the AI’s output.”

Rehl doesn’t see this sort of human supervision as temporary, either. “I believe you truly need a human-in-the-loop experience for a refactoring process, he says. “The business context of why the system is designed the way it is may get lost on an AI model and as a result, the human will need to steer it. I’ve come to believe that QA engineers may be the ‘refactoring engineers’ of the future, validating requirements, comparing outputs, and feeding that content back to the AI.”

The future isn’t quite yet

Every developer and IT leader I spoke to emphasized that we are still in the early days of generative AI. For most shops, the amount of AI-assisted (or fully AI-written) code in their codebases will be relatively small. But it will inevitably grow as AI-assisted refactoring snowballs.

Rehl referenced the strangler vine pattern to explain how he sees the process playing out:

When you like old tech and you want to make new tech, you could either build a net new system alongside of the old one and just cut over, or you can build the components of the new around the old. You start swapping the component tree and its strangler vines. They come in and strangle the tree. I think that’s going to happen over time because of AI. As the AI is exposed to existing systems as a copilot, it’s going to start automatically documenting what’s surrounding the tree. And then, a year later, it will have enough commentary to understand the business context it was trying to achieve, and it can just take over.

But for most shops, that day is not here yet. “Overall, while AI coding tools have certainly increased our productivity in many areas, they’ve also introduced new challenges in code consistency and maintenance,” said QueryPal’s Nag. “They’re not the magic bullet that some had hoped for, but rather a powerful tool that, when used judiciously, can significantly enhance a developer’s capabilities. The key is finding the right balance and always maintaining that human touch in your codebase.”
https://www.infoworld.com/article/3610521/refactoring-ai-code-the-good-the-bad-and-the-weird.html

Related News

News copyright owned by their original publishers | Copyright © 2004 - 2024 Zicos / 440Network
Current Date
Dec, Wed 4 - 19:53 CET