Reading an error
The program crashed. Where do you look first?
A traceback lists the calls that led to an error, oldest first. Read the last line for what went wrong and the frame above it for where.
A crash prints a wall of text, and the instinct is to skip it. Read it instead, in this order. The last line first. It names the kind of error and gives a message: a type error, cannot multiply a sequence by a float. Then the frame just above: the file, the line number and the line itself. This is where it happened. Only if that is not enough, read upwards: each frame is the caller of the one below, back to the start of the program.
Common exceptions · Going deeper · 1 of 2
Eight exceptions cover most crashes, and each names a kind of mistake
Read the exception class first: it says what kind of mistake to look for.
The debugger · Going deeper · 2 of 2
breakpoint() stops the program so you can look at the values
breakpoint() opens the debugger at that line: p prints, n steps, c continues.
Narrowing down
It worked last week. Which of 16 changes broke it?
With a known good state and a known bad one, test the middle and repeat: 16 suspects need 4 tests. git bisect automates it.
The script worked at change one and fails at change seventeen. Sixteen changes could be to blame. Do not read them. Test the one in the middle, change nine. It works, so the fault came later, and eight suspects are gone. The middle of what is left is thirteen. It fails. Eleven works. Twelve fails, and the change before it works, so change twelve broke it. Four tests, not sixteen.
git bisect run · Going deeper
Give bisect a command and it finds the commit without you
git bisect run uses the exit code of your test: 0 is good, non-zero is bad.
Fixing
How do you know the fix fixed it?
Reproduce the fault as a failing test, change one thing, and see the test pass. The test then guards against the fault returning.
You have found the line. Before touching it, pin the fault down. Write the smallest input that fails as a test, and run it. It fails, which proves the test can see the fault. Now make one change, and only one: convert the quantity to a number. Run the same test again. It passes. Because one thing changed between failing and passing, you know that change was the cause. Keep the test.
AI assistants
An AI assistant drafts; you remain the author
It drafts. The checks run. You read every line.
AI coding assistants now sit inside this loop, and the loop tells you how to use them. You say what you want, and why. The assistant drafts the code, and the tests for it. The automatic checks run. If they are red, you hand the error back. When they are green, you read every line of the difference. If it is not what you meant, you say so and go round again. Then a colleague reviews the pull request, exactly as for code you typed yourself.
Using AI safely
Treat generated code like a stranger's pull request
Verify what it writes. Protect what you paste. Limit what it can do.
Four habits make these tools safe to rely on. An assistant can be confidently wrong, so run and test what it gives you. It knows only what it has been shown, so show it the files, the error and the rules of your project. Whatever you paste leaves your machine, so never paste secrets or customer data, and use the tool your company has approved. And an agent that can run commands needs limits: a branch to work on, minimal permissions, and no keys to production.
Using AI safely · Going deeper
Asking well
Goal, context, constraints, and what done means.
Lesson complete
You finished “Debugging and working with AI tools”
Nicely done.