Topic 4 of 5 · 4 min read
Debugging as a learning exercise
Goal: Diagnose a bug from the error output and AI hints alone before accepting a full fix.
When code fails, read the error and the stack trace yourself first, form a theory, and ask the AI for hints before you accept a ready-made fix.
Check yourself · 1 of 3
A Python run ends with KeyError: 'email' and a long stack trace. Where do you look first?
Why it matters
Debugging is where most programming skill is built. Finding out why code fails teaches you how the language and the program actually work. If you paste every error into a chatbot and paste the fix back, the bug is gone but the learning is too. You also never practise the part that matters most on a real job: an AI fix can be wrong, and only someone who can read the error can tell.
Anthropic's 2026 trial found the largest gap between AI users and non-users on debugging questions. So this is the skill most at risk.
How to do it
- Read the last line of the error first. It names the error type and usually the cause. For Python, JavaScript or Java the message tells you what failed.
- Find your own file in the stack trace. Start at the top-most line that points at your code, not inside a library.
- Write a theory in one sentence. "I think
userisNonebecause the lookup found no row." - Test the theory with a print, a log or a debugger breakpoint. Do not change code yet.
- Ask for a hint, not a fix. Paste the error, the code and your theory. Say: "Give me a hint about where to look. Do not give the corrected code."
- Escalate in steps. First hint, then a pointed question, then the explanation, and only then the fix. Take the next step only if you are still stuck after real effort.
- After the fix, write down what caused it and how you could have found it faster.
Before and after
Before: It crashes, here is the error, fix it.
After: I get
TypeError: Cannot read properties of undefined (reading 'name')atcart.js:14. I thinkitemis undefined for an empty cart. I checked with a log anditems[0]isundefined. Give me a hint about where the empty case should be handled. Do not write the fix.
A short branch for engineers:
// cart.js:14
const label = items[0].name; // TypeError when items is []
The error names line 14 and the property name. That is already enough to guess an empty array. The AI's job is to confirm your reading, not to find the line.
What goes wrong
- Pasting the error with no code. The model guesses and invents a plausible cause. Paste the relevant lines.
- Accepting a fix that silences the error. A
try/catcharound the line removes the crash and hides the bug. - Skipping the theory. Without one, you cannot tell if the AI's answer is right.
- Fixing one symptom. If three errors come from one cause, an AI patch for each leaves the cause alone.
Examples
Hint ladder
Step 1: Where in this trace should I look first?
Step 2: Which variable could be undefined on this line, and why?
Step 3: Explain why an empty array makes items[0].name fail.
Step 4 (last): Show a fix and say what test would have caught this.
Each step gives away a little more, and you stop as soon as you can finish alone.
A fix that hides the bug
try {
label = items[0].name;
} catch (e) {}
The crash is gone, but label is now unset and the cart page shows a blank. Ask the AI: "Why does items have no first element here, and where should that be handled?"
Key points
- Read the last line of the error and the first line of the trace that points at your code before asking anything.
- Write a one-sentence theory and test it with a log or breakpoint before you change code.
- Ask for a hint and say that you do not want the corrected code.
- Escalate in steps: hint, pointed question, explanation, and only then the fix.
- Be wary of a fix that only hides the error, such as a broad try/catch.
Common mistakes
- Pasting only the error message and no code, so the model guesses a cause.
- Accepting a fix that silences the error instead of removing its cause.
- Skipping the theory step, so you cannot judge the AI's answer.
- Asking for the full fix first and never trying the hint steps.
Check yourself · 2 of 3
Which message asks for help in a way that keeps the debugging work with you?
Check yourself · 3 of 3
An AI suggests wrapping a failing line in an empty try/catch. What is the main problem?
Practice items, not counted towards your mastery.
Start here
- How AI assistance impacts the formation of coding skills (Anthropic) (opens in a new tab)ArticleFree
A randomized trial of 52 developers learning a new Python library: delegating to AI lowered comprehension, asking for explanations did not.
- Output styles, including the learning style (Claude Code docs) (opens in a new tab)DocsFree
How to switch Claude Code to the Learning style, which explains choices and leaves small pieces for you to write.
- CS50.ai, the CS50 duck tutor (Harvard) (opens in a new tab)ToolFree
A tutor built for beginners that guides with questions and hints rather than finished solutions.