Blog about Web Programming and Electronics
💬 👤
back Back to blog
Programming

What Is Debugging and Why Every Developer Needs It

ikona

Debugging is the process of finding and fixing errors in program code. A bug is any unexpected behaviour of a program — from a total crash to a minor wrong output. Debugging is not a side task but one of the most important skills of every developer: some data suggests developers spend as much as half of their working time hunting bugs.

console.log("I made it this far");

Where the Word Bug Comes From

The story goes back to 1947, when a real moth (bug) was found inside the Harvard Mark II computer causing problems, and it was taped into the logbook with the note about the »first actual bug«. The truth is even older — engineers had called errors bugs a century earlier, but the famous moth made the word world-famous.

Reading Error Messages

The most fundamental skill is reading the error. Every error message tells you three things: what happened, in which file, and on which line. For developers who read messages to the end, the error often reveals itself on its own.

Fatal error: Uncaught TypeError:
Unsupported operand types: string + int
in /var/www/register.php on line 42

Console Output

The most common technique is printing values during execution. What does the variable contain right now? Did the function even run? Console or log output tells us what the program actually does — not what we think it does.

console.log(user); // JavaScript: prints the object
var_dump($data);  // PHP: prints the type and content
print_r($result); // PHP: prints readably

The Debugger

A more advanced tool is the debugger — a program that lets you stop code execution on any line (breakpoint), then step through lines and watch variable values at every moment. PhpStorm has a debugger built in, and the same applies to browser developer tools (DevTools).

  1. Set a breakpoint on the suspicious line.
  2. Run the program in debug mode.
  3. When execution stops, inspect the variable values.
  4. Step forward and watch where the values go in the wrong direction.

The Divide Method

When the bug is not obvious, dividing helps. Comment out half the code and check whether the error remains. If it does, it is in the remaining half; if not, it is in the commented part. Repeating this process quickly narrows the search down to a few lines — with long files this is a lifesaver.

Bug Prevention

Many bugs can be prevented before they even occur: clear variable naming, small functions with a single task and type checking (TypeScript) reduce the space for the kinds of mistakes bugs grow from. In PHP even a small change shows: enabling strict typing at the top of the file.

declare(strict_types=1);

Debugging is not a sign of a bad developer — quite the opposite. A program without bugs has never been written, so finding and fixing them is not an annoying interruption of work, but a part of the work itself.

back Back to blog