Debugging feels like intuition, but the fastest debuggers follow a method. It turns a vague "it is broken" into a short list of facts.
Reproduce and read the evidence
You cannot fix what you cannot see. Find the smallest reliable way to trigger the problem: exact input, environment, version, steps. If it only happens sometimes, find out what differs between a good and a bad run. A bug with a reproduction is already half solved.
Then read the full error message and the stack trace from the top. Most of the time it names the file and line. Check logs around the time of the failure and what changed recently: a deploy, a dependency, a configuration value, the data.
Shrink the problem
Remove code and inputs until the failure disappears, then put back the last piece. Or use bisection: halve the search space repeatedly. git bisect does this over commits.
git bisect start
git bisect bad # current version is broken
git bisect good v1.4.0 # this older version worked
# Git checks out a commit in the middle; run your test, then answer:
# git bisect good or git bisect bad
git bisect reset # when finishedForm a hypothesis and test it
State a guess that can be proven wrong: "the total is wrong because the discount is applied twice". Then design the cheapest experiment, such as a print statement, a debugger breakpoint or a failing test. Change one thing at a time, otherwise you will not know what worked.
Fix the cause, then lock it in
Ask why the bad state was possible. Catching an exception to hide it, or adding a special case, often buries the real problem.
Then write a regression test that fails before the fix and passes after, so the bug cannot quietly return.
Habits that help
- Explain the problem aloud to a colleague or a rubber duck. Saying it clearly often reveals the gap.
- Take breaks when stuck for long.
- Keep notes of what you tried, so you do not repeat experiments.
Takeaway
Reproduce, read the evidence, shrink, hypothesise, change one thing, fix the cause and add a test.
Comments