How do you actually get better at debugging?
Debugging is rarely taught directly, yet it is where a big chunk of programming time goes. A few habits separate fast debuggers from slow ones:
- Reproduce first. If you cannot make the bug happen on demand, you cannot be sure you fixed it. Shrink the reproduction until it is as small as possible.
- Form a hypothesis, then test it. Random changes are guessing. State what you think is wrong and design a check that would prove you wrong.
- Bisect. Cut the search space in half: comment out half the code, check half the inputs, or use
git bisectto find the commit that introduced a regression. - Read the actual error. Stack traces, line numbers and log messages often contain the answer, and it is easy to skim past them.
- Use a real debugger. Breakpoints and stepping through code show you state that print statements miss, though well-placed logging is still valuable, especially in production.
- Explain it out loud. Rubber-duck debugging works because describing the code forces you to notice assumptions.
Afterward, ask why the bug was possible at all. Sometimes the best fix is a test, an assertion or a type that makes the whole class of bug impossible.
What is your go-to debugging technique? And what is the strangest root cause you have tracked down?
Comments · 3
Debugging time is the hidden cost of any team. Anything that shortens it, like good logs and reproducible bugs, has outsized return.
Explaining the problem out loud to someone, or to a rubber duck, works in design too. Saying it forces you to find what you assumed.
Do you prefer a debugger or print statements? I hear strong opinions on both.