I Asked AI to Review My Code. It Found Bugs I Didn't See.
For a long time, I used AI in software development for one primary purpose: Write the code. Give it a requirement. Get some Python. Ask for a function. Get a function. Describe an API. Get an API. It was fast. Sometimes impressively fast. But recently, I've been using AI differently. Instead of asking: "Can you write this for me?" I've started asking: "Can you review what I wrote?" And the…
For years, I relied on AI to assist with writing code, simply providing a requirement and receiving the corresponding Python function. While this was quick and often impressive, I recently began using AI in a different capacity: reviewing my own code. The impact of this shift was surprising. AI wasn't merely speeding up my coding process; it was also helping me examine my code from a fresh perspective.
The drawback of relying solely on AI-generated code is that it can lead to a focus on whether the code works rather than its overall quality. It's crucial to consider how the code behaves under unexpected input, API failures, database unavailability, and other challenging scenarios. This is where code review becomes essential. So, I decided to try a different workflow.
Instead of beginning with AI, I wrote the code first, then asked AI to review it. My prompt was straightforward: Review this code as a senior software engineer, identifying potential bugs, edge cases, security concerns, maintainability issues, and unnecessary complexity. I didn't want AI to immediately generate more code; I wanted it to first analyze the existing code.
The results were fascinating. Some issues were obvious once highlighted, while others were not. For instance, a function might work well with normal input but fail with an empty list, missing values, unexpected data, duplicate records, failed API responses, or very large inputs. These issues might not be apparent during a quick manual test.
AI's review can pinpoint these problems by prompting the question: What happens if this assumption isn't true? However, AI isn't infallible. It may identify genuine issues, but also false alarms or suggest changes that introduce unnecessary complexity without providing meaningful benefits. This is why I don't consider AI a replacement for code review; it simply changes the review process.
The developer still needs to decide whether each flagged issue is a problem, assess its severity, and determine if the proposed solution is appropriate. AI can flag potential problems, but ultimately, the decision rests with the human developer. The most useful AI review isn't "Fix my code"; it's "Review this code and tell me what I should worry about."
This approach keeps me involved in the reasoning process, increasing the number of perspectives examining the code. Relying solely on AI for code review isn't sufficient either. AI might not understand the reasoning behind certain architectural decisions, business requirements, or performance trade-offs. It may flag unusual code that's intentionally designed that way and can make incorrect claims.
Therefore, I view AI code review as an additional layer in the development process, following automated tests, human review, and production monitoring. Each layer addresses different aspects of software quality. AI review focuses on potential issues that may have been overlooked, while tests verify whether the software behaves correctly under defined scenarios.
Human review assesses the implementation's suitability within the real system, and monitoring observes how the software performs in real-world usage. By incorporating AI code review into this multi-layered process, we can catch potential issues that might otherwise go unnoticed. Interestingly, using AI to review my code has also influenced how I write code in the first place.
Knowing that AI will eventually review my code, I become more deliberate about my implementation. I think more about naming assumptions, handling edge cases, error handling, reducing duplicated logic, and minimizing unnecessary complexity. In essence, the reviewer indirectly influences the author, encouraging them to anticipate potential problems before they arise.
This benefit extends beyond AI-generated code. Human-written code also contains bugs, assumptions, and maintenance challenges. A fresh perspective, provided by AI, can be invaluable regardless of who wrote the original code. While AI is an unusually fast second perspective, it doesn't replace the need for human review. However, it's essential not to confuse code review with testing.
While AI can identify what to test, actual testing is necessary to verify the software's behavior under specific scenarios. Combining AI review with comprehensive testing creates a stronger workflow for ensuring software quality.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.