Definition
Debugging is the whole process of finding a "bug," an error in a computer program, figuring out its cause, and fixing it correctly. Tracking down and removing bugs is just as important in software development as finishing the program itself.
How a Detective Hunts Down Bugs
Say you're cooking a delicious stew and it comes out way too salty. What do you do? You'd walk back through the cooking steps from the start and check when and how much salt went in. Computer programming works exactly the same way. When a program doesn't do what you want, or suddenly shuts down, you need to trace the exact point where the trouble began.
Programmers use a special tool called a debugger for this. A debugger lets you run a fast program very slowly, pausing it one line at a time. At each pause, you can check which numbers are sitting in the computer's memory, as if peering at them through a magnifying glass.
Step through the code like this and you'll spot the exact moment a strange calculation happened. "This should be addition, but I typed a minus sign!" That's the moment the culprit is revealed. Once you've found the spot, you fix the code and run it again.
A Name That Started With a Real Bug
The name "debugging" hides some fun history. Calling a small flaw in a machine a "bug" was engineering slang that had been in use since the days of Thomas Edison in the 19th century.
One famous event made the term stick in computing. In 1947, an early computer (the Harvard Mark II), so huge it filled an entire room, suddenly started malfunctioning. The technicians searching for the cause found a dead moth caught between the electrical switches (relays).
They pulled the moth out with tweezers, taped it into their logbook, and left a witty note: the "first actual case of a bug being found." The word "debug" itself had been used in engineering before then. But this famous story spread widely, and the word became more familiar as a name for finding and removing errors in computers.
To Be More Precise
Debugging is more than fixing typos. A computer will run code without complaint, even if a person wrote it with faulty logic, as long as the syntax is correct. For example, even if you write a nonsensical rule like "if age is less than 0, allow entry," the computer won't notice anything wrong.
When the syntax is correct but the results come out wrong, it's called a "logic error." On the surface the program runs just fine, so these errors are much harder to find. That's why developers sometimes spend more time tracking down the cause of an error than writing the code.
Today, many modern debugging techniques are used, such as test tools that check programs automatically and log analysis, where the program leaves a record of what it did. These are key techniques for preventing problems in advance and making a program better.
Common misconceptions
Debugging is only for beginner developers or people who make a lot of mistakes.
Even veteran developers spend a large share of their development time debugging. The more complex a system gets, the more likely bugs are to appear from unexpected interactions.
Where you meet it
When you press a login button and nothing happens, you check step by step whether the signal is actually reaching the server.
To stop a game character from walking through walls, you hunt down the collision-detection formula and fix it.
In one sentence
Debugging is the process of finding the cause of an error (a bug) hidden in a program and fixing it correctly.