Sub topic 3
Start with the requirement, not the code
What this chapter assumes
You can read a Git diff and write unit tests in at least one language. You have also used an AI coding assistant to produce a change. This chapter covers the first step of a review, which happens before you read any code.
Why the order matters
When you open a diff first, the code frames your judgment. Well-formatted functions with sensible names look finished. You end up asking "Is this code reasonable?" The question that matters is "Is this the change I needed?"
AI output is especially good at passing the first question. The model writes fluent code with consistent style, and it rarely leaves a syntax error or a half-finished thought. A change can be fluent and still solve a different problem from the one you had.
Starting from the requirement reverses the order. You decide what correct looks like, then check whether the code matches. This works like a test written before the implementation: the expectation exists before the thing it judges.
Write the requirement as checkable statements
A requirement in your head, or a loose prompt such as "add a function for recent orders", is too vague to check. Before you read the diff, turn it into a short list of statements that can each be marked true or false.
Good statements have these properties:
- They describe observable behavior, not implementation.
- They include inputs and outputs, including the boundaries.
- They state what must not change.
Take this prompt: "Add a function that returns a customer's recent orders." You might sharpen it into:
- Given a list of orders and a customer id, return only that customer's orders.
- Exclude orders whose status is
"cancelled". - Sort newest first by
created_at. - Return at most three orders. Return fewer if fewer exist, and an empty list if none do.
- Do not modify the input list or any order.
Each line can be checked against the code, and most of them can later become a test. If you cannot write the list because you are unsure what the right behavior is, you have found a problem with the requirement. The code cannot fix that.
If you wrote the prompt, check that your sharpened list still matches what you originally asked. The model saw only the prompt. The statements you never wrote down are the ones it guessed.
Compare the change against the list
Once you have the list, read the diff with three questions in mind. They match three ways a change can diverge from intent.
| Question | What a "yes" means | Typical example |
|---|---|---|
| Is something missing? | A requirement is not implemented | Cancelled orders are never filtered |
| Is something different? | A requirement is implemented in a way that contradicts it | Sorted oldest first instead of newest first |
| Is something extra? | The change does more than you asked | A refactor of an unrelated helper, a new config option |
At this stage you are not hunting for bugs line by line. That comes in a later chapter. You are checking coverage: for each statement, can you point at the lines that satisfy it?
If you cannot find the lines for a statement, mark it as unmet and keep going. Do not assume it lives somewhere you have not looked.
A worked example
Here is a function an assistant might return for the prompt above.
def recent_orders(orders, customer_id):
mine = [o for o in orders if o["customer_id"] == customer_id]
return sorted(mine, key=lambda o: o["created_at"])[:3]It reads cleanly and is short. Now check it against the list:
- Only that customer's orders: met.
- Exclude cancelled orders: missing. Nothing looks at
status. - Newest first: different.
sorteddefaults to ascending, so this returns the three oldest orders. - At most three: met. Slicing handles fewer than three and empty input.
- No mutation: met.
sortedreturns a new list.
Two of five statements fail, and neither failure would show up in a casual read. The function would also pass a quick manual test with only one or two orders for one customer. This is the kind of mistake that survives skimming because the code looks correct on its own.
Here is a version that satisfies all five statements, with checks that tie each one back to the list.
def recent_orders(orders, customer_id):
mine = [
o
for o in orders
if o["customer_id"] == customer_id and o["status"] != "cancelled"
]
return sorted(mine, key=lambda o: o["created_at"], reverse=True)[:3]
orders = [
{"id": 1, "customer_id": 7, "status": "paid", "created_at": "2026-01-01"},
{"id": 2, "customer_id": 7, "status": "cancelled", "created_at": "2026-01-05"},
{"id": 3, "customer_id": 7, "status": "paid", "created_at": "2026-01-03"},
{"id": 4, "customer_id": 7, "status": "paid", "created_at": "2026-01-04"},
{"id": 5, "customer_id": 7, "status": "paid", "created_at": "2026-01-02"},
{"id": 6, "customer_id": 8, "status": "paid", "created_at": "2026-01-06"},
]
result = recent_orders(orders, 7)
assert [o["id"] for o in result] == [4, 3, 5] # statements 1, 2, 3, 4
assert recent_orders(orders, 99) == [] # statement 4, no orders
assert len(orders) == 6 # statement 5, input not changed
print("all requirement checks passed")The ISO date strings sort correctly as text, which is why the example uses them. If your real timestamps are in another format, check that the sort key compares them correctly.
The review sequence
The process has a fixed order. Only after the coverage check do you start reading code for its own sake.
Watch for silent substitutions
AI assistants often do not tell you when they changed the problem. Common patterns:
- Easier problem. You asked for retry with exponential backoff, and you got a fixed one-second sleep in a loop.
- Different interface. You asked for a function that takes a list, and it takes a generator, which breaks callers.
- Mock instead of real. You asked for an integration with a service, and the code returns hardcoded sample data with a comment saying "replace later".
- Swallowed failure. The code wraps a call in
try/exceptand returns an empty result so that nothing visibly fails. - Extra scope. You asked for one function, and the diff also renames a module, reformats a file, or adds a dependency.
Several of these never appear in the assistant's summary message. Trust the diff and ignore the description. The summary says what the model believes it did.
A useful habit is to list the changed files before reading any of them, then ask whether each file belongs in this change.
git diff --statAny file you did not expect to see needs an explanation before the review continues.
When the requirement is the weak point
Sometimes the code does what you wrote and is still wrong, because your requirement left a decision open. In the example, "recent" could mean the last three orders, or orders within the last 30 days. "Cancelled" could include refunded orders.
When you find an open decision, do not settle it by reading the code and deciding the model's guess is acceptable. Make the choice explicitly. Write it into the list, then check the code against that choice. If the code's behavior is a reasonable default, record that you accepted it on purpose. A guess you accepted is a decision. A guess you never noticed is a defect waiting to happen.
If the work is for someone else, such as a teammate or a ticket owner, ask them. A short question now costs less than a rework later.
Common errors in this step
- Reading the diff first and writing requirements afterward. You will tend to write statements that the code already satisfies.
- Statements that describe the implementation. "Uses
sortedwith a key function" is not a requirement. "Newest first" is. - Skipping the negative statements. Requirements such as "does not modify the input" and "does not add dependencies" are the ones most often violated and least often checked.
- Treating passing tests as proof of intent. If the assistant wrote the tests, they reflect its understanding of the task. They can pass while the requirement is unmet. A later chapter covers how to use tests as real evidence.
- Accepting extras because they look helpful. A bonus refactor adds code you have to review and maintain. Ask for it as a separate change.
Summary of the habit
Before you open a diff, write down what the change must do, what it must not do, and what it must leave alone. Then check coverage statement by statement and look for anything missing, different, or extra. Settle open questions in the requirement before you judge the code. The line-by-line review works better once you know what the code is meant to achieve.
Exercise
An assistant is asked: "Write a function that returns the highest-scoring player from a list of players, ignoring players who have been banned." It returns max(players, key=lambda p: p["score"]). Write three checkable requirement statements for this prompt, then say which ones the code meets and which it does not.
Show answer
Possible statements: (1) Return the player with the highest score. (2) Exclude players whose banned flag is true. (3) Define the result for an empty list, or for a list where every player is banned, for example by returning None or raising an error. The code meets statement 1. It does not meet statement 2, because nothing checks the banned flag, so a banned player can win. It does not meet statement 3 either: max raises ValueError on an empty list, and the code does not decide what should happen when every player is banned. Statement 3 also shows an open decision in the requirement, which you should settle explicitly instead of accepting whatever the code does.