The Pitfalls of AI in Code Generation: Why AI May Be Lowering Your Code Quality

This article is Day 5 of Series 2 of [Commune Product Development Advent Calendar 2024](https://qiita.com/advent-calendar/2024/commune).

## Intro

AI-powered tools like Copilot and ChatGPT are transforming the way developers write software. With the promise of faster code generation and reduced manual effort, they’re becoming a staple in many teams. However, while these tools seem like a blessing on the surface, they come with hidden costs that can degrade code quality, increase technical debt, and slow down the development process in the long term.

This article highlights potential areas where over-reliance on AI tools can create more problems than they solve.

## AI-Generated Code is more Prone to Errors and Anti-Patterns

AI models generating code are trained on large datasets of public repositories, including high-quality and low-quality code. Studies have shown that these models often fail to produce code adhering to best practices. For example, a 2023 [paper](https://arxiv.org/abs/2211.03622) found that programmers with ChatGPT access wrote less secure code than those without access. This discrepancy was attributed to the reliance on AI-generated suggestions, which often lacked robust security practices and introduced vulnerabilities that developers did not identify or correct. While AI tools can accelerate coding tasks, their outputs often omit critical considerations such as input validation, authentication safeguards, and encryption protocols, leaving the code susceptible to exploitation

Such errors are not always immediately apparent. Poorly implemented logic or subtle inefficiencies might only emerge during runtime, leading to delayed bug fixes and heightened frustration for team members.

## Technical Debt: A Long-Term Burden

AI-generated code often prioritizes "just working" solutions over maintainability. This can exacerbate technical debt. [The Report by GitClear](https://www.gitclear.com/coding_on_copilot_data_shows_ais_downward_pressure_on_code_quality) delves into the broader impact of AI tools on code quality. It highlights a rise in "churn code," defined as lines of code frequently rewritten or reverted, since the adoption of tools like Copilot. The trend suggests potential challenges in understanding and reusing AI-generated code, with implications for productivity and technical debt. Their data indicates a growing disparity between the speed of initial coding and the stability of final implementations.

As we all know, high levels of technical debt reduce the agility of development teams by creating a growing backlog of suboptimal or rushed coding decisions that require rework. This debt compounds over time, making the codebase harder to understand, extend, and maintain. As a result, addressing even minor issues often demands disproportionately large efforts, increasing the cost and complexity of future changes. Furthermore, technical debt slows down the development cycle, as developers must spend more time navigating convoluted code, debugging unexpected issues, and adapting to the mounting inefficiencies embedded in the system. Ultimately, this diminishes productivity, delays project timelines, and stifles innovation within the team.

## The Potential Erosion of Developer Skills

A study published in [ACM Transactions on Software Engineering and Methodology](https://www.iese.fraunhofer.de/blog/generative-ai-in-software-engineering-scenarios-and-challenges/) highlights the risks associated with developers bypassing manual processes in favor of automated tools, particularly AI-driven systems. The research emphasizes that while these tools improve efficiency, they might lead developers to skip essential problem-solving steps such as debugging, algorithm optimization, and code design. This can result in a decline in critical thinking and foundational coding skills over time. By relying excessively on automated solutions, developers may lose the ability to navigate complex codebases and effectively troubleshoot issues, creating long-term risks for software maintainability and innovation. These findings underscore the importance of combining AI tools with deliberate skill-building practices to preserve and enhance developer expertise​.

However, this is a relatively new and evolving area of research, and skill degradation is not an inevitable outcome. With thoughtful integration of AI tools into development practices, coupled with a focus on continuous learning and manual problem-solving, developers can harness the benefits of automation without sacrificing their expertise. Future studies are essential to determine best practices for balancing tool adoption with skill retention

## Solution: Augment, Don’t Automate

AI tools are not inherently harmful but should be treated as assistants, not replacements. Teams should establish clear guidelines for AI usage:

* **Code Review Standards**: Require all AI-generated code to meet the same standards as manually written code
* **Testing Requirements**: Mandate rigorous testing for AI-generated code, including edge cases and performance benchmarks
* **Continuous Learning**: Encourage developers to use AI tools as educational aids to improve their coding skills rather than crutches
* **Beat AI with AI**: Use AI tools to cross-verify outputs from other AI systems, ensuring higher accuracy and spotting potential blind spots in generated code.

## Conclusion

While AI tools have undeniable potential, their unchecked use can degrade code quality, increase PR review costs, and amplify technical debt. By approaching AI with a critical, science-based mindset, we can harness its benefits while mitigating its risks.

Let’s aim for a future where AI empowers developers to write better code, rather than fostering complacency or creating more work for the team!