Sub topic 1
Why AI-generated code needs review
An AI coding assistant can produce a working-looking function in seconds. That speed makes it easy to skip the step that matters most: deciding whether the code is correct, safe, and appropriate for your project. This chapter explains why review is needed, what makes AI output different from code you wrote yourself, and what standard to hold it to. Later chapters cover the review process in detail.
Prerequisites
This chapter assumes you have:
- Basic programming experience in at least one language. The examples use Python.
- Familiarity with Git and with reading a diff.
- Experience writing or running unit tests.
- Some use of an AI coding assistant, such as a chat tool or an editor plugin.
The core problem: plausible is not correct
A language model generates code by predicting likely text, one piece at a time, based on your prompt and the patterns it learned in training. It does not run the code, and it has no built-in check that the result satisfies your requirement. What it optimizes for is output that looks like good code.
For most everyday tasks, the likely answer is also a correct one. That is why assistants are useful. The risk is that the cases where it is wrong look the same as the cases where it is right. The code is well formatted, the names are sensible, and often there are even comments explaining it. Nothing in the surface of the output signals doubt.
Here is a small example. An assistant is asked for a function that checks whether a string is a valid TCP port number:
def is_valid_port(value: str) -> bool:
return value.isdigit() and 0 < int(value) < 65536This looks right, and it passes the obvious checks: "8080" returns True, "0" returns False, and "70000" returns False. The flaw only appears with input that is unusual but legal for the method. str.isdigit() returns True for some Unicode characters, such as the superscript two, that int() cannot convert:
def is_valid_port(value: str) -> bool:
return value.isdigit() and 0 < int(value) < 65536
def fixed_is_valid_port(value: str) -> bool:
return value.isascii() and value.isdigit() and 0 < int(value) < 65536
print(is_valid_port("8080"))
print(fixed_is_valid_port("\u00b2"))
try:
is_valid_port("\u00b2")
except ValueError as error:
print("original raised:", error)The original function raises ValueError instead of returning False. A function that promises a boolean now crashes on untrusted input. Nobody would flag this on a quick read. You find it only if you ask what inputs the function must handle and then test them.
Why the review burden shifts to you
When you write code yourself, you build it up from an understanding of the problem. By the time you finish, you know why each line is there. With generated code, you receive the result without that process. You have to reconstruct the reasoning, and you might not notice what was never considered.
Three facts make this a real shift, not a minor inconvenience.
- Generation is cheap, verification is not. An assistant can produce a hundred lines in the time it takes you to read ten. If you accept output at the rate it appears, unreviewed code accumulates faster than anyone can check it.
- The assistant has no stake in the outcome. It will not be paged at 3 a.m. when the code fails. It cannot learn from the incident in your team's postmortem. You and your team carry that responsibility.
- The assistant sees only part of your context. It usually does not know your deployment environment, your threat model, your compliance rules, your performance budget, or the conventions your team agreed on last quarter. Code can be correct in general and wrong for your system.
The practical consequence is simple. If you commit the code, you own it. Review is how you make that ownership informed instead of blind.
Treat AI output as an untrusted contribution
A useful mental model is to treat generated code like a pull request from an unfamiliar author. The author may be skilled and fast. You still do not merge their work without reading it, because you have no track record with them and you cannot see how they reached their conclusions.
The comparison with human code is not "AI is worse." In some areas AI code is more consistent than a tired human's. The point is that the signals you normally use to calibrate trust are missing:
- A colleague's history tells you what kinds of mistakes they make. The assistant's mistakes vary by prompt, context, and model version.
- A colleague can explain their reasoning when you ask. The assistant can produce an explanation, but that explanation is also generated text and may not match what the code does.
- A colleague who is unsure will often say so. The assistant tends to present a guess and a verified fact in the same confident tone.
For these reasons, the standard for AI-generated code should be the same as for human code, and stricter wherever you cannot verify the source of an idea. If a human wrote a change and you would ask for tests, you should ask for tests here. If you would question an unfamiliar dependency, question it here too.
What can go wrong if you skip review
The costs of unreviewed code fall into a few groups. Each is expanded in later chapters, so this section only names them.
| Risk | What it looks like | Why it is hard to notice |
|---|---|---|
| Incorrect behavior | Wrong results on edge cases, such as empty input or unusual characters | The common path works, so quick manual testing passes |
| Invented references | Calls to functions, options, or packages that do not exist | The names look realistic and the code reads naturally |
| Security flaws | Injection, hardcoded secrets, missing input validation | The code runs correctly with normal input |
| Outdated patterns | Deprecated APIs or old library idioms | The code works until you upgrade or the library removes the feature |
| Poor fit | Different style, duplicate helpers, or unneeded dependencies | Each piece looks fine alone, but the codebase gets harder to maintain |
Two of these deserve extra attention because they carry consequences beyond your own project. A hallucinated package name is more than an annoyance: attackers have been known to register names that assistants tend to suggest, so installing an unchecked suggestion can run someone else's code on your machine. A security flaw, by contrast, may stay silent for months until someone sends hostile input.
Review does not mean reading everything with equal suspicion
Reviewing well is different from reviewing slowly. If you scrutinize every line with the same intensity, you will get tired, and tired reviewers approve things. A better approach is to spend effort in proportion to risk.
- Code that handles untrusted input, authentication, money, or data deletion deserves a close read.
- Code that is easy to test deserves tests, and a passing test run is stronger evidence than your impression of the code.
- Boilerplate that a linter or type checker can verify should be left to those tools.
The review process in this tutorial follows that order. You first confirm that the change addresses what you asked for. You then read it for known failure patterns, and you gather evidence with tests and static analysis. You then make a decision. Each step is cheap relative to the cost of shipping a defect.
Evidence over impressions
One habit separates careful reviewers from the rest. They do not rely on how the code feels. A feeling of "this looks right" is exactly the signal that AI output is good at producing. Replace it with evidence you can point to:
- A test that fails before a fix and passes after.
- A type checker run with no new errors.
- A command that shows the package exists and is the one you intended.
- A diff you read in full and can explain in your own words.
If you cannot explain a piece of generated code, you are not ready to accept it. That is not a failure on your part. It is the correct signal that something needs to be simplified, rewritten, or rejected.
Key takeaways
- Generated code is optimized to look right, so appearance is not evidence of correctness.
- Generating code is fast and verifying it is slow, so the verification step is where your effort belongs.
- The assistant does not carry responsibility for the result. You and your team do.
- Hold AI output to the same standard as a pull request from someone you do not know, and ask for evidence.
- Spend review effort in proportion to risk, and let tools handle what they can check cheaply.
Exercise
An assistant gives you a function that passes the three examples you tried. A teammate says "it passed, so it's fine to merge." Give two reasons that is not enough, and name one kind of evidence you would ask for next.
Show answer
First, passing a few common examples says nothing about edge cases or unusual input, as the isdigit() example showed: the code worked on normal ports and crashed on a character like the superscript two. Second, the code was generated to look plausible, so it will usually pass the obvious checks. Passing them does not distinguish correct code from plausible code, and the assistant did not verify the requirement or your environment. Next evidence could be a set of tests covering empty, boundary, and malformed input, a type checker or linter run on the change, or a read-through of the diff that checks every behavior against the original requirement.
Exercise
Why is it risky to install a package just because an assistant imported it in the code it wrote?
Show answer
The assistant may have invented the name. If the package does not exist, the install fails, but if someone has registered that name with malicious code, installing it runs their code on your machine. You should confirm that the package exists, that it is the library you intend, and that it is maintained, before adding it as a dependency.