Debugging Formula

Debugging is the systematic process of finding, diagnosing, and correcting errors (bugs) in a program.

The Formula

bug report→hypothesis→test→fix

Ask yourself: Do you already have a program that does the wrong thing, and need to find and fix the cause?

Quick example

Program prints 'Hello Worl' instead of 'Hello World'. Find the missing 'd', fix it.

What this formula means

The systematic process of finding, diagnosing, and correcting errors (bugs) in a program. Debugging involves reproducing the problem, isolating its cause through testing and inspection, applying a targeted fix, and verifying the fix resolves the issue without introducing new problems.

A good doctor does not hand out random medicine. They ask what is wrong, guess a cause, run one test to check the guess, then treat the cause instead of only the cough. Debugging follows the same order. One difference helps you: a program can be run again in exactly the same way, so you can repeat a test as often as you like.

Formal view

Debugging applies the scientific method to code: observe unexpected behavior, hypothesize a cause, design a test to confirm or reject the hypothesis, and iterate until the root cause is identified and corrected.

Worked examples

Example 1

medium
This code should print numbers 1 to 5 but has a bug: SET i = 1. WHILE i < 5: OUTPUT i. i = i + 1. Find and fix the bug.
Show solution

Answer

Change `i < 5` to `i <= 5`. Off-by-one error.

First step

1.
Step 1: Trace: i=1 (output 1), i=2 (output 2), i=3 (output 3), i=4 (output 4), i=5 (5 < 5 is false, loop ends).

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

Example 2

medium
This code calculates an average but gives the wrong answer: SET total = 0. FOR each num in [10, 20, 30]: total = total + num. SET average = total / 2. OUTPUT average. The input has 3 numbers. Find the bug.

Example 3

medium
The code `SET i = 0; WHILE i <= 5: OUTPUT i` prints how many numbers?

Common mistakes

  • Making random changes hoping to fix the bug instead of systematically isolating the cause
  • Fixing the symptom instead of the root cause, which leads to the bug reappearing in different forms
  • Not testing the fix thoroughly, accidentally introducing new bugs while fixing the original one

Common mistakes guide

If this formula keeps breaking in real problems, read the most common errors before you practice again.

Why this formula matters

Almost no program works the first time, so debugging is how programs get finished. A careful debugger can find one wrong line among hundreds by checking one part at a time. Someone who guesses may change five things, make the bug vanish for now, and add two new bugs without noticing.

Frequently asked questions

What is the Debugging formula?

The systematic process of finding, diagnosing, and correcting errors (bugs) in a program. Debugging involves reproducing the problem, isolating its cause through testing and inspection, applying a targeted fix, and verifying the fix resolves the issue without introducing new problems.

How do you use the Debugging formula?

Debug when a program already exists and does something wrong: a wrong answer, a crash, a character that moves the wrong way, a word spelled wrong on screen. The words bug and error, or 'it should do this but it does that', are signals. If there is no program yet and you are planning the steps, you need an Algorithm. If you only need to find out whether a program works, that is Testing.

Why is the Debugging formula important in CS Thinking?

Almost no program works the first time, so debugging is how programs get finished. A careful debugger can find one wrong line among hundreds by checking one part at a time. Someone who guesses may change five things, make the bug vanish for now, and add two new bugs without noticing.

What do students get wrong about Debugging?

The bug might not be where the error appears—trace backward.

What should I learn before the Debugging formula?

Before studying the Debugging formula, you should understand: algorithm.