Sub topic 1
Why AI-generated code needs review
Prerequisites
This chapter assumes you can program in at least one language, read a Git diff, and run unit tests. It also assumes you have used an AI coding assistant at least a few times. No other tools are needed.
The core idea
An AI coding assistant predicts code that looks like a good answer to your prompt. It does not run the code, and it does not know your system. The result often compiles, follows common style, and reads well. None of that shows that it is correct.
Treat every AI-generated change as an untrusted contribution. Review it with the standard you would apply to a pull request from an unfamiliar author. Because the code looks polished, you need to be at least as strict as you would be with a human colleague.
Why plausible code is risky
Three properties make AI output hard to review by reading alone.
- It looks confident. The code and its explanation are fluent, even when the logic is wrong.
- It lacks your context. The assistant sees only what you gave it. It may not know your data rules, your deployment limits, or your team's conventions.
- It is cheap to produce. You can get hundreds of lines in seconds. Review time does not shrink to match, so unreviewed code piles up.
There is also a human factor. When code arrives fully formed, it is tempting to skim and approve. This is called automation bias, the habit of trusting an automated source more than it deserves.
A worked example
Suppose you ask an assistant for a function that returns the average of a list of numbers. It returns this:
def average(values):
return sum(values) / len(values)It is short, readable, and correct for typical input. Now consider the call average([]). The function computes 0 / 0 and raises ZeroDivisionError. Your requirement never said what should happen for an empty list, so the assistant made no choice. A reader who only checks the happy path will miss it.
This pattern is common. The code answers the question you asked, but not the questions you forgot to ask. Review is where you ask them.
import pytest
def average(values):
if not values:
raise ValueError("values must not be empty")
return sum(values) / len(values)
def test_average_of_numbers():
assert average([2, 4, 6]) == 4
def test_average_of_empty_list_raises():
with pytest.raises(ValueError):
average([])The fix is small. The more important change is the second test, which records the intended behavior so it stays checked in future edits.
What review covers
The rest of this tutorial builds a repeatable process. Each step answers one question about the change.
You can start inspecting a change with standard Git commands. They show what changed and let you stage only the parts you have checked.
git diff --stat
git diff
git add -pgit diff --stat summarizes which files changed. git diff shows every line. git add -p walks through each hunk and asks whether to stage it, so you accept only what you reviewed.
Summary of the stance
- Treat AI output as untrusted until you have checked it.
- Compare it to the requirement, not only to itself.
- Prefer evidence such as tests over impressions.
- Keep the final decision with you. You own the code once you accept it.
Exercise
An assistant gives you a function that works on three sample inputs you tried. Why is that not enough to accept it, and what is one concrete next step?
Show answer
Sample inputs usually cover the happy path, while AI code tends to miss cases the requirement never mentioned, such as empty input. Next, compare the change to the requirement, list edge cases (empty, null, very large values), and write tests for them, as with the empty-list test for average.
Exercise
Which Git command lets you stage an AI's changes one hunk at a time, and why is that useful for review?
Show answer
git add -p. It forces you to look at each hunk and decide whether to stage it, so you accept only the changes you have reviewed.