This blog post serves as a thought experiment, delving into potential solutions for a pattern I have noticed on projects throughout my career. As a consultant, I work with many companies, each with unique ways of organizing and handling software development. However, throughout my career at Keyhole and elsewhere, I’ve noticed something that seems to be consistent across all dev teams: the existence of legacy code.
Legacy code can be frustrating and time-consuming to work with, so I used AI to create a solution to mitigate the hassle. While other solutions may already exist (and some may be more efficient), I found the process of creating this tool expanded my understanding; it really helped me grow as an engineer.
So, I’m using this blog post to share my process with you! Let’s dive into how AI can assist in improving application design (specifically legacy code) through automation.
Legacy Code: A Problem That’s Never Going Away
Time and time again, I’ve noticed that most organizations have improperly designed applications hiding in the dark corners of their dev ops infrastructure – commonly referred to as legacy code. Deliverables reigned supreme during the development team’s formative years, and the time needed to build well-organized, well-named, and well-structured software was not available when measured against business demands. A rigid budget and inexperience are commonly responsible for this mismatch.
Early in our careers, almost all of us have written code that falls into this category. And almost all of us have also been on the flipside as the ones responsible for maintaining the downstream issues of someone else’s shortcuts. Acceptance is half the battle, and this “circle of life” perspective helped me feel more of an appreciation for the sometimes frustrating source code I find on most projects. Once I accepted that I’d probably always have to work around some sort of legacy, unorganized code, I decided to engineer a tool using AI that rapidly improves the design of these applications through automation.
AI to the Rescue
While I have used AI in several ways to improve the software I develop, there is one example in particular that I’ll cover in this blog post. A legacy application in VB.NET made its way into my work items at a past job. It contained a master class that spanned thousands of lines with dozens of methods. It was an uphill climb determining which pieces were responsible for the functionality I needed to update – an all too familiar design flaw.
How could I use LLMs like Chat GPT to help me simplify the application before diving in? With cleanly written, “decoupled” code, we can easily place our short and tidy methods/functions into prompts to be quickly refactored, debugged, or just judged by the AI (I think we’ve all been there).
Unfortunately, this was not the case in my situation. The prompt length limitation was a hurdle for processing as much text as I had. Even if I took the time to punch each block of code in separately, there were still a couple of issues. First, sharing that much intellectual property with AI is not best practice. Second, I’ve found that AI struggles to maintain context across that many prompts.
To solve this problem, I thought about how a developer learns a new code base. They can’t read every line of code – that would be a waste of valuable sprints. Rather, they know how to get a ten-thousand-foot view of the high-level components to deduce a mental map of the application. We generally use this map to find one area to fix a pesky bug or to build a shiny new feature.
I began to wonder how I could extract this map in a way the AI could understand, feed it into a prompt, then ingest the response and scaffold the suggested changes.
The Solution
First I had to ask myself, what information would the AI need to determine how to break apart this master class? For this specific example, I came up with the following…
- A list of all properties in the class.
- A list of all methods in the class.
- For each method, a sublist of all invoked methods in the class.
- Also for each method, a list of all properties used.
With this information, the AI would have sufficient knowledge to break apart the monolithic code into an organized collection of smaller classes, using the names of the methods/properties and how they are related to determine a pattern. It would then know how best to name the new classes based on what they contained.
My approach was to treat the LLM like an API and use JSON as a medium for communication. Rather than placing it in the body of a request, I would print it to the user who would then paste it into a prompt. All this is prefaced by a message instructing the AI on what it was to do with the data passed in and how to format its solution. My application then imports the response and scaffolds new files.
An Example with Code
Let’s dive into some code. I’ve created a mock application for a salon that has a large class with similar design flaws. For brevity’s sake, it only contains seven methods. Here is the SalonServiceMasterClass
.
After passing this code through the Code Cleaner, the following output prints to the user.
Next, let’s take a look at the prompt we are going to use before the JSON.
“I’m sending you a list of methods, constructors, fields, and their usage. Please analyze the data, break down the application into separate files with distinct modules, and list the required fields for each file. I aim to modularize a large function. Return the results in JSON format, including the names of the new modules.”
Here is the response from ChatGPT.
Then, take this response and import it into the Code Cleaner Application, and the application will scaffold the files recommended by the AI! Problem solved.
In Conclusion
While the approach discussed here may not be the ultimate solution for every scenario, it illustrates the potential of AI for automation in software development, specifically with legacy code. Thanks to this method and AI, dealing with legacy code is much less aggravating. Let’s continue to innovate and improve the ways we interact with technology. Thank you for reading!
Check out the Keyhole Dev Blog for more blogs.