Sub topic 1
Why AI-Generated Code Needs Review
Prerequisites
You should know basic programming, Git, and unit tests. You should be able to read a diff, which is a line-by-line view of changes. You should also have used an AI coding assistant.
AI-generated code needs the same review as code from a human contributor, and sometimes stricter review. An assistant predicts plausible code from patterns. It does not understand your full requirement, system, data, or production risks. Code can look clean while producing the wrong result.
How AI-Generated Code Typically Goes Wrong
Common failure modes include:
- Hallucinated APIs: the code calls a package function that does not exist.
- Missed edge cases: empty input, missing values, duplicate records, or unexpected permissions cause failures.
- Outdated patterns: the code uses an old library interface or an unsafe configuration method.
- Hidden scope changes: the assistant edits unrelated files or changes behavior beyond the request.
- Security flaws: string-built SQL, hardcoded secrets, weak validation, or overly broad access.
A successful example is not proof that the general solution is safe. Check the assumptions behind it.
Start with the Requirement, Not the Code
Rewrite the request as observable behavior. Identify what must change, what must stay unchanged, and how failure should behave. Then compare the proposed change with that list before studying implementation details.
Ask:
- Does the change solve the requested problem?
- Does it alter unrelated behavior?
- Are error cases defined?
- What evidence would show that the requirement is met?
This prevents polished code from distracting you from an incorrect goal.
Keep Changes Small Enough to Review
Ask the assistant to make one focused change at a time. A small diff is easier to understand, test, and undo. Reject unrelated formatting, dependency upgrades, or refactors unless they are necessary.
git diff --stat
git diff
git add -pReview each staged hunk. Do not accept a large change merely because its tests pass.
Reading the Diff Line by Line
Read imports, control flow, data transformations, error handling, and configuration. For every new line, ask what input reaches it, what it returns, and what happens when an assumption fails. Verify unfamiliar names against the package documentation and your codebase.
Exercise
Why should you check the requirement before reading implementation details?
Show answer
A change can be elegant and tested yet still solve the wrong problem or change behavior outside its scope.