Read Code Fast
Lesson 26 Verify, don't trust
write the failing test, lean on linters and types, make the AI explain and check it
What you'll learn
- Write the failing test first and watch it fail for the right reason
- Use type checkers and linters before reading
- Check an AI's explanation by running its claims
AI code reads well and sounds sure of itself. Neither is evidence. The only evidence is something that ran.
The idea
Three habits turn "it looks right" into "it is right". First, write the failing test before the AI writes the code, run it, and watch it fail for the right reason. A green result later then means something. Second, let machines do the boring checks: a type checker (such as mypy) and a linter catch a whole class of mistakes without you reading a line. Third, make the AI explain its code, then check the explanation. Each claim in it is something you can run.
That is the whole course in one sentence: read in passes, name the patterns, distrust anything that can't fail, and run what you can't tell.
1# test_orders.py: written first, before asking the AI2from orders import top_orders34def test_top_orders_gives_3_biggest_largest_first():5 amounts = [9, 250, 40, 1200, 75]6 assert top_orders(amounts, 3) == [1200, 250, 75]78# orders.py: the AI's first attempt9def top_orders(amounts: list[int], n: int) -> list[int]:10 return sorted(amounts)[-n:]
- Line 1 Because you wrote the test first, the AI's claim "this works" has something to be measured against.
- Line 6 The test states the answer in advance, with numbers chosen so a wrong order shows. Run it before the AI's code exists: it must fail.
- Line 10 Reads fine: sort, take the last n. But
sortedgoes smallest to largest, so the answer comes out in the wrong order. The test catches it in a second. The type checker would not: every type here is correct.
Take away
- Write the failing test first, and watch it fail for the right reason.
- Let the type checker and the linter do the boring checking before you read.
- Ask the AI to explain, then turn each claim into something you can run.
Quiz: test yourself
7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.