Sub topic 7
Reading generated code before you accept it
This chapter assumes you can write functions, conditionals and loops in Python, run a script in a terminal, and have used a chat-based AI assistant. Here you will learn a repeatable way to read AI-generated code before it goes into your project.
Why reading comes before running
Generated code usually looks confident and tidy. It often runs on the first try. That makes it easy to paste it in, see output, and move on. But code that runs is not the same as code you understand, and it is not the same as code that is correct.
Reading first gives you two things. You catch wrong assumptions before they spread. You also practice the skill this tutorial is about: building your own mental model of how code behaves.
A four-pass reading routine
Read the code in passes instead of all at once.
- Purpose. In one sentence, state what the code should do. If you cannot, the request was unclear, and that needs fixing first.
- Inputs and outputs. List what goes in, what comes out, and what happens at the edges: empty input, one item, wrong types.
- Line by line. Trace the code with a small example. Write down the value of each variable as it changes.
- Questions. Mark every line, name or library call you cannot explain. Look these up or ask the AI to explain them, then explain them back in your own words.
Worked example
Suppose you asked for a function that returns the median of a list, and the AI gave you this:
def median(values):
if not values:
raise ValueError("median() requires at least one value")
ordered = sorted(values)
mid = len(ordered) // 2
if len(ordered) % 2 == 1:
return ordered[mid]
return (ordered[mid - 1] + ordered[mid]) / 2Apply the passes.
Purpose: return the middle value of a list, or the average of the two middle values.
Inputs and outputs: a list of numbers goes in, one number comes out. An empty list raises ValueError. A one-item list should return that item.
Trace with [4, 1, 3, 2]:
orderedbecomes[1, 2, 3, 4].len(ordered)is 4, somidis4 // 2, which is 2.- 4 is even, so the odd branch is skipped.
- The result is
(ordered[1] + ordered[2]) / 2, which is(2 + 3) / 2, or 2.5.
Now trace [3, 1, 2]. The sorted list is [1, 2, 3], mid is 1, the length is odd, and the function returns ordered[1], which is 2.
Questions: Why sorted and not .sort()? Because sorted returns a new list and leaves the caller's list unchanged. Why / 2 and not // 2? Because the average of two integers can be fractional.
Reading also shows what the code does not do. It does not check that the items are numbers. Passing ["a", "b"] gives a TypeError from the addition or sort, not a clear message. Whether that matters depends on your project, and that decision is yours.
Common things to look for
| Check | What to ask |
|---|---|
| Off-by-one | Do indexes and ranges include or exclude the right ends? |
| Mutation | Does the code change the caller's data without saying so? |
| Edge cases | What happens with empty, single or very large input? |
| Invented APIs | Does this function or option exist in the library version you use? |
| Unused or extra code | Is every line needed? Why is it there? |
| Silent failure | Does it catch errors and hide them? |
Common errors when reviewing
- Skimming because it looks professional. Neat formatting says nothing about correctness.
- Trusting the AI's explanation over the code. The explanation can describe what the code was meant to do. Trace what it actually does.
- Accepting code you can only half explain. If you cannot explain a line, you cannot debug it later.
- Checking only the happy path. The first example that works is the least informative one.
Reading tells you what the code seems to do. The next chapter shows how to confirm it with tests you write yourself.
Exercise
Trace median([7, 2, 9, 4]) by hand. State the sorted list, the value of mid, which branch runs, and the result. Then name one input the function does not handle clearly.
Show answer
The sorted list is [2, 4, 7, 9]. The length is 4, so mid is 2. The length is even, so the second return runs: (ordered[1] + ordered[2]) / 2 = (4 + 7) / 2 = 5.5. One unhandled input is a list containing non-numbers, such as ["a", "b"], which fails with a TypeError from the arithmetic instead of a clear message. A list with None would also fail when sorted with numbers.