Skip to the content ForoHackers
Log inJoin

Debugging: start here

Debugging
#3353 · · Quote · Report

Debugging is finding why a program does something other than what you meant, and fixing the cause rather than the symptom. It is a method, not luck: observe, form a hypothesis, test it, repeat. Programmers spend more time here than writing new code, so the skill pays back every day.

Key concepts

  • Reproduce first: a bug you can trigger on demand is half solved; shrink it to the smallest failing case.
  • Hypothesis and experiment: change one thing at a time and predict the result before you run it.
  • Bisection: halve the search space, over code, inputs or commits, until the culprit is alone.
  • Observation tools: debugger, logs, assertions, core dumps and tracing show state without guessing.
  • Root cause: ask why until the answer is a design flaw, then add a test that would have caught it.

Vocabulary

  • Breakpoint: a place where the debugger pauses the program.
  • Stack trace: the chain of calls that led to the current point.
  • Heisenbug: a bug that changes or vanishes when you observe it.
  • Regression: something that worked and no longer does.

What to study, in order

  1. Use a debugger on a small program: breakpoints, stepping, inspecting variables and the call stack.
  2. Read stack traces and error messages completely, from the first line, before searching.
  3. Practise bisection with your version control tool on a regression.
  4. Learn memory and thread debugging tools for your language, such as sanitizers and race detectors.
  5. Explain a bug aloud to someone, or to a rubber duck, before changing any code.

Related tags

#testing #observability #version-control #performance

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply