Lesson 8: Review AI Code Like an Editor
| Course home | Previous: Lesson 7 | Next: Lesson 9 |
Time: about 60 minutes.
Goals
By the end of this lesson you will be able to:
- Read a diff and understand exactly what changed.
- Use Git to compare, undo and restore changes safely.
- Run a five-minute review checklist on any AI change.
- Spot the classic AI mistakes: scope creep, invented APIs, deleted tests, silent behaviour changes.
- Put your project on GitHub.
Concepts
Reading a diff
function currentStreak(habit, today = todayKey()) {
const done = new Set(habit.done);
- let day = today;
+ let day = done.has(today) ? today : addDays(today, -1);
let streak = 0;
- Lines starting with
-were removed. Lines with+were added. - Lines with a space are unchanged context around the change.
- Here the change means: “if today is not ticked yet, start counting from yesterday”.
Your editor shows the same information with colours. In VS Code, the Source Control panel lists changed files; click one to see a side-by-side diff.
Git commands for reviewers
| Command | What it does |
|---|---|
git status |
Which files changed, which are new |
git diff |
Show unstaged changes line by line |
git diff --stat |
Just the list of changed files and line counts |
git diff --staged |
Show changes you have staged with git add |
git restore app.js |
Throw away uncommitted changes to one file (careful: cannot be undone) |
git restore . |
Throw away all uncommitted changes |
git log --oneline |
List recent commits |
git show <commit> |
Show what one commit changed |
git revert <commit> |
Make a new commit that undoes an old one (safe for shared history) |
The Pro Git book explains all of these in depth, free.
Classic AI mistakes to hunt for
| Mistake | What it looks like in a diff |
|---|---|
| Scope creep | Files changed that you did not mention; “while I was there I also refactored…” |
| Reformatting noise | Hundreds of lines changed because of quotes or indentation, hiding the real change |
| Invented APIs | Calls to functions or options that do not exist (check MDN or the library docs) |
| Weakened tests | Tests deleted, skipped or changed to expect the wrong value so they pass |
| Silent behaviour change | A default value, sort order or condition quietly flipped |
| Swallowed errors | catch {} around code that should report a problem |
| Unsafe shortcuts | innerHTML with user text, eval, disabled checks |
| New dependencies | A <script src="https://...">, a new package, a new config file |
| Leftover debugging | console.log everywhere, commented-out code, TODOs |
| Dead code | New functions nothing calls |
The five-minute review checklist
Before accepting any change:
- Scope:
git diff --stat. Only expected files? - Size: is it as small as it could be?
- Read: read every added line. Anything you cannot explain, ask about.
- Red flags: scan for the table above.
- Run: open the app, try the feature and one thing next to it; run tests (from Lesson 9 on).
Then commit, or restore and try again.
Walkthrough
Step 1: Make a change on purpose and review it
Ask for something small:
Add a line under the "Today" heading that shows today's date in a friendly
format like "Tuesday, 1 December", using toLocaleDateString. Change only
what is needed.
When it finishes, do not look at the app yet. Run:
git status
git diff --stat
git diff
Go through the five-minute checklist. Write down:
- Which files changed?
- Was any change outside what you asked for?
- Can you explain each added line?
Then check it in the browser.
Step 2: Ask the agent to explain its own diff
Explain the diff since my last commit line by line, as if I have never
seen JavaScript. For each line say why it is needed. Point out anything
you changed that I did not ask for.
Agents are usually honest when asked directly about extra changes. If it admits to unrequested changes, decide whether to keep them.
Step 3: Undo something
Practise the undo button while the stakes are low:
git restore .
git status # clean again
The date line is gone. Now ask the agent to do it again, review again, and this time commit:
git add -A
git commit -m "Show today's date under the Today heading"
Step 4: Review with a second opinion
Start a fresh session (so it is not reviewing its own work with its own assumptions) and ask:
Review the last three commits as a strict senior developer. Use git log
and git show. Look for bugs, accessibility issues, security problems
(especially innerHTML with user text), unnecessary code, and anything that
breaks the rules in AGENTS.md. List findings by severity with file and
line. Do not change anything.
For each finding, decide: real problem, or not? Fix the real ones in small commits. You will notice that reviewers, human or AI, sometimes flag non-problems. Your judgement decides.
Step 5: Spot the planted bugs
Here is a diff an agent might produce for “make the delete button work”. Find four problems before opening the answers.
+import confetti from "https://cdn.example.com/confetti.js";
+
function renderHabit(habit, today) {
const item = document.createElement("li");
- item.className = "habit";
+ item.innerHTML = `<span class="habit-name">${habit.name}</span>
+ <button onclick="deleteHabit('${habit.id}')">Delete</button>`;
...
+function deleteHabit(id) {
+ habits.splice(habits.findIndex((h) => h.id === id), 1);
+ render();
+}
Problems
1. **New third-party script** (`confetti` from a CDN) that nobody asked for. 2. **`innerHTML` with `habit.name`**: an XSS hole. 3. **Inline `onclick`** attribute: breaks under a strict Content Security Policy and needs a global function. 4. **No confirmation, and no save**: deleting is instant and not persisted, so it comes back on refresh. Also `splice` with `findIndex` returning `-1` would delete the **last** habit if the id is not found. 5. Bonus: the `className = "habit"` line was removed, so styling breaks.Step 6: Put the project on GitHub
You need a GitHub account. The GitHub Hello World guide walks through the website.
- On github.com, create a new empty repository named
tiny-habits(no README, since you already have one). -
GitHub shows commands for “push an existing repository”. They look like:
git remote add origin https://github.com/YOUR-USERNAME/tiny-habits.git git branch -M main git push -u origin main - If your agent offers to push for you, read the command before approving. Pushing publishes your code.
- Refresh GitHub. Your files and commit history are there.
Before pushing, run git log -p | grep -i -E "key|secret|token|password" (macOS, Linux or Git Bash) to double-check nothing sensitive was ever committed. For this project there should be nothing.
What good output looks like
- Every commit in
git log --onelinedescribes one change in plain words. - No commit mixes unrelated changes.
- You found at least one thing in a review that you decided to change, and one you decided to leave.
- Your code is on GitHub.
Checklist
- I can read a diff and explain what changed.
- I used
git status,git diff,git restoreandgit log. - I ran the five-minute review on a real change.
- I asked a fresh session for a review and judged its findings.
- I found the planted bugs.
- My project is pushed to GitHub with no secrets in history.
Mini-quiz
- In a diff, what do lines starting with
+and-mean? - What does
git restore .do, and why be careful with it? - Why ask a fresh session to review instead of the one that wrote the code?
- The agent changed 400 lines but you asked for a button. What is the first command you run?
- Name three red flags from the classic mistakes table.
Answers
1. `+` lines were added, `-` lines were removed. 2. It discards all uncommitted changes in the working folder. They cannot be recovered with Git afterwards. 3. A fresh session does not carry the original session's assumptions and reasoning, so it is more likely to question them. 4. `git diff --stat` (or `git status`) to see which files changed and how much, before reading the details. 5. Any three: scope creep, reformatting noise, invented APIs, weakened tests, silent behaviour changes, swallowed errors, `innerHTML` with user text or `eval`, new dependencies, leftover debugging, dead code.Assignment
- Go back through your history with
git log --oneline. Pick your messiest commit and write what a better commit message would have been. - Ask the agent to make a deliberately bad change: “For practice, add a feature that sorts habits alphabetically, but make two subtle mistakes and do not tell me where.” Review it and find them. Then
git restore .. - Add a short “How I review AI changes” section to your project’s
README.mdwith your own checklist.