Debugging with Claude Code

11 min read text
Course contents (20 lessons)
  1. Meet Claude Code
  2. What Claude Code is and how it differs from a chat window
  3. Installing Claude Code and signing in
  4. Your first session: explore, ask, change
  5. Asking well and staying in control
  6. Asking for changes: context, goal, constraints and acceptance criteria
  7. Reading and approving edits
  8. Permission modes: how much freedom to give
  9. Project memory and slash commands
  10. CLAUDE.md: teaching the tool about your project
  11. Slash commands and custom commands
  12. Everyday coding workflows
  13. The plan-first workflow
  14. Understanding unfamiliar code
  15. Debugging with Claude Code
  16. Refactoring safely
  17. Writing tests with Claude Code
  18. Git, small changes, cost and security
  19. Git workflows: branches, commits and pull request descriptions
  20. Keeping changes small and reviewable
  21. Context and cost awareness
  22. Security habits: secrets, commands and untrusted input
  23. Extending the tool, teamwork and a final project
  24. Hooks and MCP servers explained
  25. Team practices for AI-assisted coding
  26. Final project: building a small app step by step

Give it evidence, not just a complaint

"The checkout is broken" is where you start, but it is not enough to give. A debugging request should carry the same information you would give a colleague: what you did, what happened, what you expected, and anything that looks relevant, such as an error message or a log line.

Bug: when a customer applies a 10% coupon and then removes an item, the order total stays at the discounted amount of the old cart.
Steps: add 2 items (total 200), apply coupon SAVE10 (total 180), remove one item (100 expected before discount, so 90 expected). Actual: 180.
Where I looked: CartService.RemoveItem does not seem to recalculate totals.
Do not fix yet. First, find the code that calculates the total and list 2 or 3 hypotheses for the cause, with the evidence in the code for each.

Why it works

The reproduction steps and the numbers make the problem exact. My own guess is included but marked as a guess, so it helps without limiting the search. "Do not fix yet" prevents a premature patch. Asking for hypotheses with evidence trains the tool, and you, to reason before acting.

Reproduce with a test first

A bug that cannot be reproduced cannot be proven fixed. Ask for a failing test before the fix.

Write a failing test that reproduces this bug, in the existing CartServiceTests file, using the same style as the other tests. Run it and show me that it fails for the reason we expect, not for some other reason. Then stop.

The last condition matters. A test that fails because of a typo in the test is not a reproduction. Check the failure message yourself.

Then fix, minimally

Now fix the bug with the smallest change that makes the test pass. Do not refactor. Run the whole cart test suite and report the result.

"Smallest change" and "do not refactor" keep the diff reviewable. If the real fix needs a refactor, do it in a separate step.

Paste errors and logs

Paste the exact error message and the relevant stack trace. Trim noise, but do not paraphrase, because the exact wording is what the tool searches for. Before you paste, remove secrets, tokens and personal data from logs.

This exception appears in production logs about twice a day:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at OrderProcessor.Process(Order order) in OrderProcessor.cs:line 88

Look at OrderProcessor and find what could modify the collection during enumeration. Consider concurrency. Do not change anything, explain the likely cause and how to confirm it.

When the tool is stuck

If three attempts at a fix have failed, stop. More attempts at the same approach rarely help. Instead:

  1. Ask it to summarise what it has tried and what each attempt showed.
  2. Run /clear and restart with that summary plus new facts.
  3. Add evidence by running the code with logging or a debugger yourself, and give the output.
  4. Question the assumptions: is the bug where we think it is?

Do not accept a fix you do not understand

Explain why this change fixes the bug, in three sentences. What was the root cause, and why did the old code behave that way? Could the same mistake exist elsewhere in the project?

The last question often turns up the same flaw in a sibling method, which is a good catch.

Watch for symptom fixes

Some fixes hide a bug instead of solving it: a null check that returns a default value, a retry loop around a failing call, a try-catch that logs and continues. Ask yourself: does this solve the cause or only silence the symptom?

The common mistake

The common mistake is asking the tool to "fix the failing test" when the test is correct and the code is wrong, or the reverse. Say which one is the truth: "The test expectation is correct. Fix the code, not the test."

Try it yourself

Find a real bug from your project's history, or create a small one on purpose in a scratch branch. Run the whole sequence: report with evidence, hypotheses, failing test, minimal fix, explanation. Note which step caught the most mistakes. For me it is almost always the failing test.

This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.

Sign up free I already have an account