Using AI to Ensure High Code Quality

A LinkedIn post struck me the other day, and it got me thinking about code quality and all the AI buzz in the developer space. The post read something like this: 

School: Write a five-page paper on XYZ research topic

Real life: Explain it in 5 seconds, or I’m out. 

While the post is technically correct, it seems to suggest the five-page research assignment isn’t valuable or relevant to the real world. But in reality, that is exactly how expertise is developed. Learning is long. Simplicity comes only with mastery. 

Perhaps the real truth of that LinkedIn post is captured in an older saying: 

“I would have written a shorter letter, but I did not have the time.”  

High-quality code is no different, though certainly less obvious. It does its job in the most straightforward way possible, and it’s easy to understand. It’s lighter-weight, more resilient, and simpler to maintain and upgrade. 

Low-quality code, on the other hand, is unnecessarily complex, which makes it brittle and difficult/expensive to upgrade over time. It bloats the entire codebase and incurs significant technical debt that can eventually bring the entire project to ruin. 

The entire world is asking “Can AI help with _____________?” The AI hype promises a perfect result with just one click, or that anyone can do anything even with no expertise, the truth is far more nuanced. Like most contexts, AI can help improve code quality by reducing the tedious, manual work in the development cycle and lessen the gap between junior and senior developers.

But it can’t do everything.

3 Ways AI Can Improve Code Quality

1. Fast, error-free code writing

A crop of AI tools has arisen specifically designed to assist developers. Predictive coding works like the auto-complete function in email and text messaging, potentially cutting the time it takes to code and improving code quality by making suggestions that avoid common syntax errors.

AI also gives developers more capability in checking their code before the code review phase. 

  • Even a mainstream AI tool like ChatGPT can be used to proof code snippets. Developer-specific AI tools can suggest improvements to the code and better ways of organizing the code. 

  • AI tools can be trained on specific code standards and style preferences, which helps prevent “cowboy coding” and maintain consistency among developers, teams, and projects. It can also allow developers to take full advantage of open-source code. 

  • AI can help ensure better safety checks and mitigate security risks like command injection attacks or buffer overflow vulnerabilities.

All of this elevates the work of junior coders and reduces the workload for downstream phases of the SDLC like testing. In a world of microservice architectures and faster release cycles, AI can help developers meet the demands of the project without burning out.

2. Better code reviews

So much of the development process is plagued by manual “stare-and-compare” checks and tests, including code reviews. Senior developers are people, too, and there’s only so much time in a day when they — or anyone — can tap into that elusive state of deep focus. 

AI can clear the weeds of black-and-white errors and give reviewers more bandwidth to keep the project on target and on time. 

  • AI can catch basic code errors that would cause the code to compile incorrectly. 

  • AI can assess the code beyond asking “Does it work?” and assess whether the code works well and will be easy to maintain. This is where improving code quality really happens. AI can suggest ways to produce the same outcome with less code and even pinpoint security vulnerabilities and style inconsistencies in the code. 

  • AI can assess code modifications and know whether a change to the code represents a new feature or a defect caused by code changes. 

  • AI can identify bugs, find the root cause immediately, and recommend fixes. Comparatively, manual debugging requires combing through massive amounts of code to find the root cause. 

Generally, AI protects developers, reviewers, and testers from becoming a single point of failure in the development process. Catching bugs before they reach production should be table stakes by now. AI can empower senior developers to apply their creative problem-solving skills to the project, which tends to be their favorite part of the job, and adds way more value to the project in the mid- and long-term.

3. Modernizing legacy code

AI can be retroactively applied to existing and legacy code — even poorly-documented, untested code, (aka, refactoring timebombs). When dealing with legacy code bases, the developer’s goal is to avoid breaking anything, rather than solving the problem. 

  • AI can digest a legacy codebase and improve the code without breaking existing functionality. This can lay the groundwork for making previously impossible modernizations to monolithic applications. 

  • AI can help produce documentation for poorly or un-documented code. This can set developers up for success in maintaining and adding new code. 

Good documentation enables other developers (and even non-technical stakeholders) to use the code correctly, meaning more developers can contribute to a project and continuity can be efficiently maintained if a new developer takes ownership of the project. 

3 Ways AI Can’t Improve Code Quality

1. Come up with creative solutions to COMPLEX problems

Jerry Seinfeld gave a great interview to the Harvard Business Review several years ago. When discussing burnout as a reason Seinfeld ended, the interviewer asks if the writing process could have been more efficient. He goes so far as to ask Seinfeld if “McKinsey or someone” could have helped find a better model. 

After asking “Who’s McKinsey?” and “Are they funny?” Seinfeld says:

“I don’t need them. If you’re efficient, you’re doing it the wrong way. The right way is the hard way.” 

This holds true for the creative and strategic aspects of software development. AI is about efficiency, not creativity; the creative process can be inefficient by nature — at least, not in the way we normally think about efficiency and productivity.

  • AI can’t tell you what users need, what goal you should pursue, or why you should pursue it. AI can’t come up with unique ways to achieve a goal or translate the goals of a project into features and code.

  • While AI can enforce processes and practices, it can’t decide or establish those processes and practices for you. For example, AI can’t formulate acceptance criteria for functions or tasks, nor can it tell you what types of tests to perform on your code.

  • AI can’t imagine innovative user interfaces or unique, delightful experiences. AI is trained to produce an output that the user is statistically most likely to want (aka, the status quo). While AI can help maintain standardization and consistency, coming up with something new isn’t AI’s strong suit.

AI works best as an accelerant applied to particular junctures of a process, development, or otherwise. Thankfully, it’s still up to humans to answer the highest-level questions of “What should we do?” and “Why should we do it?” and “How can we make it beautiful?”

2. Understand context

AI can only “know” what it’s been trained to know. While AI can recognize patterns and infer guidelines in the code it’s trained on, it can’t make suggestions or observations beyond that. 

  • AI can’t know why the developer wrote the code the way they did. AI can help enforce best practices and standardization, but there are always exceptions and edge cases. 

  • While generative AI tools can empower developers to use code they don’t fully understand, thus keeping the project moving, AI can’t help the developer troubleshoot that code if something goes wrong. 

  • AI can’t tell whether the functionality of the code is relevant to the end user. 

One of the biggest promises of AI is that non-developers will be able to create digital experiences with minimal coding knowledge. I don’t think that will ever be the case. While AI can significantly lower the technical barrier of development, it takes an experienced developer to fully consider and execute a project.

3. Produce perfect code

The million-dollar question of AI is whether AI-generated code will ever reach such a high state of quality that can be pushed to production untouched. 

The answer today is: definitely not. And I don’t ever see that being the case. Unchecked AI-generated code can introduce serious vulnerabilities and increase the total effort of the project. 

  • AI-generated code is rarely accurate. AI is trained on human-written code, which contains errors, and when AI doesn’t have an answer, it makes one up. AI works best if it generates small snippets of code at a time, and even then, it needs proper guidance and context. If AI were being applied to cooking, it could help prep ingredients, but it would still need supervision, and it certainly couldn’t be the head chef. 

If cutting coding time in half with AI results in double the debugging time, have you really accomplished anything? 

  • AI isn’t transparent about how it generates its outputs. When generating code, AI doesn’t consider how that code will fit into the existing code. 

  • AI-generated code straddles the line of legality. On top of using code that may be copyright-protected, AI code generators use code from open-source libraries without abiding by the licensing conditions required by the library. 

As of now, AI tools like Copilot and ChatGPT are merely decent at producing basic, generic code snippets. Even these pieces of code should be met with scrutiny.

Solving Low Code Quality at the Source

I think our human instinct is to seek ease. That’s why we ask “Can AI do the entire job of a developer?” instead of “How can AI prevent the root causes of my pain points?”

So why does low-quality code happen? 

  • A lack of experience. Junior developers are more likely to write code containing simple errors, undue complexity, and redundancies. AI can help with this. 🙂

  • A lack of best practices and policies. Without guidelines, rules, and standards, developers and reviewers resort to their personal preferences and biases. Resolving this disconnect causes undue delays and conflict, and it incurs technical debt. AI can’t help with this. 🙁

  • Manual processes. “Stare and compare” reviews, testing, and debugging take up a ton of time and leave too much room for error. AI can help with this. 🙂

  • A lack of alignment. When stakeholders aren’t on the same page about what, why, and how they’re fulfilling the project, teams get mired in reworking and backtracking so badly that they are just ready to get the thing done. AI can’t help with this. 🙁

  • Aggressive timelines. Too-tight timelines force teams into a Sophie’s choice between speed and quality. AI can help with this. 🙂

Developers are creative problem-solvers by nature. AI shouldn’t aim to take over their job, but to create more space for them to spend more of their time doing the parts of their job that they love. People simply do better work when they enjoy what they’re doing, and that’s the key to unlocking the true potential of any project.

Previous
Previous

Clean Architecture: The Secret to Cutting Costs and Scaling Smart

Next
Next

WLCM Featured on the front page of Inc.com