There’s a common complaint about AI vibecoding regarding the quality of the code produced. It’s often said to be “spaghetti code”, or super long and messy code files that have a host of problems: logical loops, no clear state, etc. Even without looking at code, one knows spaghettification has happened when any one bug that pops up can be patched, but only at the cost of one or more new bugs being introduced.
This is not an AI-exclusive problem, but AI coding tools are all too happy to join in with their human counterparts in creating entire pools of marinara within a codebase.
Spaghetti code is one of many common emergent design byproducts.
Frankly, I’ve seen issues like this countless times as a hobbyist coder and now with AI generated code. More interestingly, I find them to be exceedingly common outside of coding as well. Experiencing this phenomenon frequently across multiple domains provides a keen insight: the creation of spaghetti code is an extremely natural part of the emergent design process.
Much to the dismay of the AI-naysayers, I’d argue that the deeper your goals delve into R&D, moonshots, or stretch goal territory, the more you should expect spaghetti code as one of many natural immediate outcomes. This doesn’t mean that your process is bad, it means that it is still emerging, still evolving, and that there haven’t been punctuated cleanup processes sufficient to properly codify structure to comply with best practices.
Understanding the emergent design process provides hope: spaghetti code is easily addressable and fixable, even if you will need to burn a bit of time (and tokens!) to enact a solution. Follow these steps, and you’ll get that AI generated code in shape in no time!
1) Ask, “Are we creating spaghetti code?”
Really! Just ask! The language models you’re using aren’t bad! They’re riffing and iterating with you and modifying code as you go. At this point in AI development history, there’s generally not a supervising process that monitors code output for organic refactoring or modularization opportunities. It happens, but rarely. So, when you start getting into a vicious game of whack-a-mole with bugs or other developmental problems, either look at the code yourself or ask the AI, “Are we creating spaghetti code?” Once you know it’s happening, you can take steps towards a remedy—such as directing the AI to de-spaghettify the code.
2) Check the code structure and modularize.
Now that you know you’re creating spaghetti code, the question is: Are you going to solve it in a low-code fashion by digging in and extracting code blocks into multiple files, modules, or functions as needed? Or are you simply going to instruct the AI developer to properly modularize the code? Instructing an AI developer, much like a human developer, that Separation of Concerns (SoC) and the Single Responsibility Principle (SRP) are best practices is a good idea.
With a human developer, you should set the expectation and only revisit if there is a subsequent problem. However, with an AI developer there’s no such thing as micromanagement at present, so occasional reminders and checks to modularize or refactor are certainly suggested. Don’t be afraid to dig in, even if you’re not a coder. If you know how the modularization should happen, tell your AI developer directly without waiting for it to come to the same conclusion on its own.
3) Redefine a clear objective and outcome.
With the monolithic spaghetti code now separated by concern and properly modularized, it helps to get your AI developer in line with your expectations regarding what you’re trying to achieve, and why. Sometimes this can happen as a conversation, if the AI model is up to the task. This is how I prefer to handle things. I like to treat my AI developer almost like an employee. Through prompting or mode changes, I indicate that we won’t be making code updates until we mutually agree on an objective and outcome. We discuss and come to an agreement on what the formerly spaghettified code is supposed to accomplish (the objective), and what successfully enacting the process achieves (the outcome). Only then do I give back the ability for the model to produce or edit code.
As an aside, notice that this step isn’t as reductive or eye-roll worthy as it first seems. What does it matter if an AI agrees on an objective or not? Why not just dictate that from the start? Well, if you could have, you would have. Remember that spaghettification often happens as a result of emergent design. Having this conversation is really for you to perform the reverse engineering required to convert emergent developments into asserted structure or process. So, don’t dismiss or disregard this step… it’s an emergent design requirement, not a coding requirement, but including your AI developer ensures that they're aligned going forward.
Conclusion
After following these three steps, I've had even the gnarliest of problems, sometimes spanning thousands of lines of code and burning hundreds of dollars of development tokens, almost resolve themselves. In fact, sometimes stepping back and acting more like a project manager than a co-developer leads the AI to point out the exact issues you've been trying to get it to understand in the first place.
It is imperative to reframe the problem and ignore the AI detractors who have not utilized the technology, or who have not yet updated their skillsets to include successful management of contemporary artificial intelligences.
No, not all AI is bad at coding.
No, the outcome is not inevitably going to be low quality.
No, the produced software won't fail during deployment or in production just because it is AI developed.
While AI is imperfect and can always be improved, the matter at hand is the human user's ability, or inability, to diversify their skillset. One must learn a new method of management that involves directing artificial intelligences in their current state, whatever that may be. Humans must adopt project management (PM), development operations (DevOps), and systems engineering capabilities within themselves in order to truly leverage the tool of artificial intelligence for coding.