Debugging is like detective work: you gather clues, form a hypothesis, and test it until the real cause of the problem is found. Most of a programmer's time usually goes into this, not into writing new code.

The usual steps#

  1. Reproduce the problem: make sure you can trigger the error every time.
  2. Isolate it: determine which part the problem is in.
  3. Investigate the cause: look at variable values and the execution path.
  4. Fix it: make a small, specific change.
  5. Verify: make sure the problem is gone and nothing else broke.

Tools#

  • Printing values (print/console.log): the simplest method.
  • Debugger: a tool that lets you pause execution at a line and inspect values.
  • Logs: messages a program records while working, which you read later.
  • Error messages: they often show which file and which line the problem occurred in.

Practical tips#

  • Read the whole error message, not just the first line.
  • Change only one thing at a time.
  • Explain the problem to a colleague (or even a rubber duck); very often the explanation itself reveals the cause.

AI and debugging#

AI assistants can suggest hypotheses when they see the error message and the related code. But you must judge their suggestions yourself, and give them only the relevant code, not the whole project or confidential information.