You have 1 article left to read this month before you need to register a free LeadDev.com account.
Your inbox, upgraded.
Receive weekly engineering insights to level up your leadership approach.
Estimated reading time: 6 minutes
Beyond coding assistants, the painful task of migrating code is emerging as the killer application of generative AI for software developers.
Coding assistants like Github Copilot and Zed are the most recognizable application of generative AI for software engineers, but they aren’t without their quirks. They can suggest code snippets, identify all the relevant files and resources for any function, and generally speed up your workflow. Rely on them too much though and you may start to pile up tech debt, atrophy your programming skills, and suck all the fun out of the job.
But one area where generative AI is showing promise is with the boring, tricky, and time consuming task of code migrations. Earlier this year, computer scientists at Google estimated that they halved the time it took to migrate more than 500 million lines of code by using a large language model to identify and migrate a significant portion of Java 8 applications to Java 17.
The problem with code migration
Converting functioning legacy code into a more modern and maintainable format presents a lot of challenges for engineers. While the allure of a resource-efficient and well-documented codebase is understandable, legacy code is working code, and replacing it comes with the risk of breaking everything.
The biggest issue with any code migration is that there is often a lack of documentation on what various parts of the codebase do. Or worse, a lack of documentation on what parts of the codebase are even meant to do.
As Rob Mee, CEO of AI-powered migrations specialist Mechanical Orchard explains, just migrating everything runs the risk of taking a codebase that a few people understand, and creating a new one that nobody understands. This makes figuring out what is important business logic and what’s redundant code a crucial challenge to solve before migrating a single line of code.
Another issue is that external libraries, APIs, and other dependencies may not exist in the target environment. Old code was (ideally) written with the best available architecture and dependencies, but they may now be outdated or insecure so you can’t just replace them without a full overhaul. Similarly, small differences can cause migrated code to function as expected most of the time, but then throw errors when certain conditions are met.
To migrate a COBOL codebase running on a mainframe to a modern application running on cloud infrastructure, Mechanical Orchard captures the full stream of data in and out of the mainframe for a number of weeks. They then use generative AI to create code that takes the same inputs and returns the same outputs.
While this kind of system may not be suitable for every organization, it shows one of the ways that LLMs can tackle the task of code migration. Even with the best planning, code changes breaking things in ways you don’t anticipate can also be a problem, as Mithilesh Ramaswamy, a senior engineer at Microsoft and AI innovator puts it.
More like this
How GenAI can help with code migration
Coding assistants like Github Copilot and Zed are certainly capable of automatically converting code from one language to another, refactoring it, and adding documentation, all while preserving the structure of the application. Migrating a small codebase can just be a matter of asking the assistant to simplify complex functions and remove redundant logic.
Of course, most code migration projects are more complex than that. As Ramaswamy explains, off the shelf coding assistants are able to get a high level overview of large portions of a codebase by using processes akin to retrieval augmented generation (RAG). By essentially being able to identify every time a variable, class, and function is used and follow every call to its final endpoints at the same time, these assistants can figure out what each line of code is doing and thus infer missing documentation.
By processing all the files responsible for specific features at once and tracking how they are implemented across the whole codebase, these tools can assess how the application works and identify ways to improve it. Similarly, where code is reused or repurposed within an application, the AI model can identify patterns across the entire codebase and suggest ways to streamline it at a scale and speed that would be impossible for humans to replicate.
“AI can migrate millions of lines of code far faster than human teams,” says Ramaswamy. Once the engineers have identified how the code migration should happen, the tools can handle most of the grunt work.
Where GenAI still falls short
While AI has huge potential for handling code migrations, it still falls short in a few key ways.
Even the most powerful models are unable to fully grasp complex logic and data flows in large codebases. Google’s Gemini LLMs with a 2 million token context window, the largest currently available, can process around 60,000 lines of code at once.
Given that the team at Google had to refactor more than 500 million lines of code, this means that the codebase had to be broken into a minimum of 10,000 or so chunks. Mechanical Orchard similarly tackles each legacy codebase piece by piece. If you’re using AI models for code migration, exceeding the context window can introduce errors in the new codebase.
Generative AI tools are also pretty blunt instruments. Because they are trained on large amounts of public code, they are most likely to solve problems in the most obvious way. As a result, they can struggle with niche rules, business processes, and regulatory or customer requirements – especially when they are undocumented. Mechanical Orchard attempts to circumvent this by recreating the application data flow, but that requires a level of expertise that not every organization has access to.
As with all applications of GenAI, hallucinations – or just making things up – remains an issue. There is a non-zero chance that every time you rely on generative AI to generate code, it will make a mistake. Mee explains that sometimes the AI will just call a function or use a method that doesn’t exist.
For Nir Cohen, an engineering manager at Checkmarx, these last two points are the biggest issue as they can introduce security issues into your codebase. “AI still generates vulnerabilities,” he says.
How to use GenAI to migrate code
How AI can best be deployed during code migration is still evolving as companies like Mechanical Orchard pioneer new techniques and tools like Github Copilot evolve to this use case.
Even the models involved are changing quickly. Mechanical Orchard is deliberately model agnostic, as whether OpenAI, Anthropic, or Google offers the best LLM for their needs changes all the time.
Most large scale code migrations will require engineers to develop their own code migration pipeline that employs multiple AI models to assess the codebase, suggest ways to refactor it, and make the necessary changes, rather than use a one-click solution. Making 10,000 passes through your 500 million line codebase with a coding assistant just isn’t feasible, though apps like Zed can absolutely work for smaller applications.
Regardless of what tools you use, Ramaswamy recommends migrating large codebases in small chunks with a lot of testing. While it might take longer to migrate the whole codebase, you are far less likely to have everything break or to accidentally introduce major bugs and security vulnerabilities. This is also the approach Mee recommends.
Given how fast generative AI models are developing, Ramaswamy also suggests considering a “fast-follower strategy” – basically, the wait and see approach. If your legacy code is still operating, developments over the next few months and years may make code migration even simpler.
No matter how these tools develop, it’s unlikely that an AI agent will be able to handle an end-to-end code migration for you, unfortunately. “We still need human expertise and oversight in critical areas such as business logic, validation, security assessment, and compliance,” Cohen says.
For Ramaswamy, it’s the same. “AI is a tool, it’s not meant to be a replacement for the artist.”