Finding a bug that gives no error
After this: Find an off-by-one bug with prints and the trace, and fix it in four steps.
On this page · 8 sections · 3 checks
A till adds up three prices, 4, 5 and 6, and prints 11. There is no error and no message. The program ran to the end and gave an answer. The answer is wrong, and nothing has told you where to look.
This is the other kind of bug, and the more common one. Finding the cause of a wrong result and fixing it is . With no message, the evidence is the values the program made along the way, and this lesson shows two ways to see them.
A loop that misses the first value
The program keeps a total, and a loop adds each price to it by position. Step through it and watch the box i.
pricesholds[4, 5, 6], andtotalstarts at 0.- The loop line gives
iits first value: 1, not 0. - Line 49 adds the price at position 1, which is 5.
totalis 5. ibecomes 2, and line 49 adds the price at position 2, which is 6.totalis 11.- The loop is finished: there is no position 3. Line 511 prints
11.
- Run the programRun it and watch the box i. It starts at 1 and goes to 2. Position 0, which holds 4, is never read, so total ends at 11 instead of 15.
prices = [4, 5, 6]total = 0for i in range(1, len(prices)): total = total + prices[i]print(total)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Position 0 was never visited, so the 4 was never added. A loop that starts one place late, or stops one place early, is an off-by-one bug. It is one of the most common bugs there is, in every language, and it never gives a message: every position the loop asked for was a real one.
Seeing the values: a print
The first way to see what a program is doing is to make it say so. Put a print inside the loop that shows i, and run it.
prices = [4, 5, 6]total = 0for i in range(1, len(prices)): print(i) total = total + prices[i]print(total)1 2 11
import java.util.ArrayList;import java.util.List;
public class Main { public static void main(String[] args) { ArrayList<Integer> prices = new ArrayList<>(List.of(4, 5, 6)); int total = 0; for (int i = 1; i < prices.size(); i++) { System.out.println(i); total = total + prices.get(i); } System.out.println(total); }}1 2 11
Two lines, 1 and 2. No 0. That is the whole bug in two lines of output: the loop never looks at the first position. Once you have seen the values, take the print out again.
Seeing the values: the trace
The second way is the trace band. It shows every box after every line, so you can find the first moment a value is not what you expected. Here the first wrong value is i on its first pass: you expected 0 and saw 1. The line that gave i that value is the loop line, and that is the line to fix.
The four steps
- Say what you expected, and what you got. Here: 15, and 11.
- Find the first place where the values stop matching, with a print or with the trace. Here:
iis 1 on the first pass. - Change one thing, the line that made the first wrong value. Here: start the loop at 0, range(len(prices))int i = 0.
- Run again and check against what you expected. If it is still wrong, go back to step 2 with the new values.
One change at a time matters. Changing three lines and getting the right answer leaves you not knowing which change was the fix, and the other two may have added new bugs.
The fixed loop
prices = [4, 5, 6]total = 0for i in range(len(prices)): total = total + prices[i]print(total)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
i is 0, then 1, then 2. Every price is added, and the program prints 15.
Run it
The block expects 11, the wrong answer, so that you can see the bug first. Now fix the loop line so that it starts at 0, run again, and the output should be 15.
Check yourself
Three prices, one loop. What does this print?
prices = [4, 5, 6]total = 0for i in range(1, len(prices)): total = total + prices[i]print(total)import java.util.ArrayList;import java.util.List;
public class Main { public static void main(String[] args) { ArrayList<Integer> prices = new ArrayList<>(List.of(4, 5, 6)); int total = 0; for (int i = 1; i < prices.size(); i++) { total = total + prices.get(i); } System.out.println(total); }}Complete the loop line so that every price is added and the program prints 15. Write the whole line, including the colon at the endbrackets and the opening brace.
prices = [4, 5, 6]total = 0____________________ # ← visit every position, from the first total = total + prices[i]print(total)import java.util.ArrayList;import java.util.List;
public class Main { public static void main(String[] args) { ArrayList<Integer> prices = new ArrayList<>(List.of(4, 5, 6)); int total = 0; ____________________ // ← visit every position, from the first total = total + prices.get(i); } System.out.println(total); }}A program runs with no error and prints a wrong answer. Which is the best first step?
Honesty notes
- Say what debugging is: finding the cause of a wrong result and fixing it.
- Recognise the off-by-one bug: a loop that starts at 1 and misses the first position, or stops one short and misses the last.
- Find the first place where the values differ from what you expected, by printing them or by stepping through the trace.
- Follow the four steps: say what you expected, find where the values go wrong, change one thing, run again.