Debugging
Every Programmer Makes Bugs
Here is a secret that beginners are rarely told: professional programmers spend much of their time fixing their own mistakes β and that is completely normal.
An error in an algorithm is called a bug, and hunting them down is called debugging. It is not a punishment for bad work; it is a skill, and one of the most valuable in computing.
Kinds of Bugs
| Bug | What happens | Example |
|---|---|---|
| Sequence error | Steps in the wrong order | butter the toast before toasting it |
| Logic error | Runs fine, wrong thinking | "add 2" where you meant "multiply by 2" |
| Condition error | The test is wrong or flipped | IF answer NOT = correct THEN "Correct!" |
| Loop error | Repeats wrongly β or forever | a countdown whose counter goes up |
| Missing step | A needed action was never written | forgetting "open the jar" entirely |
The sneakiest are logic errors: nothing crashes, no warning appears β the algorithm cheerfully does exactly what you wrote, which is not what you meant.
The Debugging Method
Debugging is detective work, and detectives follow a method:
- Reproduce it β find an input that makes the failure happen every time
- Trace it β follow the algorithm by hand with that exact input
- Locate it β find the first step where what happens differs from what you expected
- Fix one thing β change only that step
- Re-test β the failing input should now work, and the old working inputs must still work
Rule 4 matters most. Change three things at once and you will not know which one fixed it β or which one quietly broke something else.
Tracing With a Trace Table
Tracing (also called a dry run) means pretending to be the computer: follow each step exactly, keeping a trace table of every variable's value.
Buggy countdown β should print 3, 2, 1 and stop:
SET count = 3
REPEAT UNTIL count = 0:
OUTPUT count
ADD 1 TO count β suspiciousβ¦
| Step | count | Output |
|---|---|---|
| SET count = 3 | 3 | |
| output, add 1 | 4 | 3 |
| output, add 1 | 5 | 4 |
| output, add 1 | 6 | 5 |
The table makes the bug visible immediately: count is rising, so it will never equal 0 β an infinite loop. The fix: subtract 1 instead of adding. One change, then re-test.
Test the Weird Cases
An algorithm that works for the input 7 might still fail for:
- 0 β does dividing or counting still make sense?
- Negative numbers β did anyone plan for β5?
- Huge values β a million? a billion?
- Empty input β the user pressed enter without typing
These are edge cases, and bugs love hiding in them. When you test an algorithm β including any flowchart you draw β deliberately attack it with strange inputs. Finding a bug before users do is a victory, not a failure.
Explain It Out Loud
One more professional trick: explain your algorithm, step by step, out loud β to a friend, a parent, or even a rubber duck on your desk. Saying each step forces you to slow down, and mid-sentence you will often hear the bug yourself: "β¦then it checks if the answer is wrong and says Correct β wait."
Programmers really do this. It is called rubber duck debugging.
Worked Example β Debugging a Real, Slightly Trickier Bug
Here is pseudocode meant to find the average of a list of test scores, submitted by a student who is confused why it always reports a suspiciously low number:
SET total = 0
SET count = 0
FOR EACH score IN scores
total β total + score
NEXT
SET average = total / count
OUTPUT average
Applying the debugging method from this lesson, step by step: Reproduce it β running with scores = [60, 70, 80] gives an error or a strange result instead of the expected 70. Trace it β build a table tracking total and count through each turn of the loop:
| Step | score | total | count |
|---|---|---|---|
| start | 0 | 0 | |
| turn 1 | 60 | 60 | 0 |
| turn 2 | 70 | 130 | 0 |
| turn 3 | 80 | 210 | 0 |
Locate it β total climbs correctly, but count never changes at all, staying at 0 through every turn of the loop! Fix one thing β the loop is missing a line to increase count, so add count β count + 1 inside the loop body. Re-test β tracing again confirms count now reaches 3, and average correctly computes 210 / 3 = 70.
Notice that the trace table didn't just reveal that something was wrong (dividing by zero, or a wrong answer) β it pinpointed exactly which variable was misbehaving and when, turning a vague "it's giving weird numbers" complaint into a precise, one-line fix. This is the payoff promised throughout this course: a mechanical, patient process beats guessing, every time.
Key Words
- Bug β an error that makes an algorithm behave incorrectly
- Debugging β finding and fixing bugs
- Trace / dry run β following an algorithm by hand, exactly, with a real input
- Trace table β a record of every variable's value at each step
- Edge case β an unusual input (0, negative, empty, huge) where bugs often hide