Lesson 2 of 6 · 15 min · 1 hands-on step
Reviewing AI-Generated Code
Catch the bugs AI assistants commonly make before they reach production.
Review checklist
Use this every time you accept AI-written code, or review a teammate's PR:
- Does it do what was asked? Re-read the requirement, then the code.
- Edge cases: empty input, one item, exact boundaries, very large input, invalid input.
- Side effects: does it mutate its inputs? Change global state? Make network calls?
- Failure modes: what happens on bad input? Could it loop forever or crash?
- Security: user input in SQL or HTML? Secrets in code?
- Readability: would a teammate understand it in six months?
- Tests: write or run tests that prove it works. Don't just eyeball it.
Common AI mistakes to look for
| Smell | Example |
|---|---|
| Off-by-one | i < arr.length - 1, while (len > size) |
| Mutating inputs | splice, sort() or reverse() on a caller's array |
| Missing guards | no check for size <= 0, null, empty strings |
| Hallucinated APIs | array.chunk(), str.capitalize() (they don't exist in JavaScript) |
| Swallowed errors | catch (e) {} that hides failures |
In interviews
You may be asked to review a snippet or explain how you use AI. A strong answer:
"I use AI for first drafts and boilerplate, then I review it like a teammate's PR: check edge cases, run the tests, and make sure I understand every line before I commit it."
🧪 Practice
A developer asked an AI: "Write a function that splits an array into chunks of a given size." It produced the code in the editor.
It looks fine but has bugs. Review it like a senior engineer, then fix it so that:
chunk([1, 2, 3, 4, 5], 2)→[[1, 2], [3, 4], [5]](the last chunk can be shorter)chunk([], 3)→[]- A
sizeless than 1 throws an error instead of looping forever - The original array is not modified
// AI-generated. Review carefully before trusting it.
function chunk(arr, size) {
const result = [];
while (arr.length > size) {
result.push(arr.splice(0, size));
}
return result;
}
console.log(chunk([1, 2, 3, 4, 5], 2));
Output appears here. Press Run or Ctrl/⌘ + Enter.
✅ Check your understanding
Which review question catches the most AI bugs?
Create a free account to track your progress.