How to debug with breakpoints

Hayk Margaryan
4 min readMar 27, 2021

Being able to use the breakpoints correctly is one of the main parts of debugging. In this article I am going to present how to debug and use breakpoints. Below is Python code that is supposed to solve the following problem:

A fruit store sells 3 kinds of fruit (apple, orange, banana). Each fruit has its own price per pound. We assume that each fruit is sold the same amount every day. The amount per fruit is different (for example, the store sells 10 pounds of apple every day but 20 pounds of orange).

PROBLEM: We need to find out if the yearly sales is greater, equal or less than $50,000.

1. applePrice = 1          # $1 per pound
2. orangePrice = 2 # $2 per pound
3. bananaPrice = 3 # $3 per pound
4.
5. appleSold = 10 # 10 pounds of apple is sold every day
6. orangeSold = 20 # 20 pounds of orange is sold every day
7. bananaSold = 30 # 30 pounds of banana is sold every day
8.
9. appleDailySales = applePrice * appleSold # expected: $10
10. orangeDailySales = orangePrice * orangeSold # expected: $40
11. bananaDailySales = bananaPrice + bananaSold # expected: $90
12.
13. totalDailySales = appleDailySales + orangeDailySales + bananaDailySales # expected: $140
14.
15. totalYearlySales = 365 * totalDailySales # expected: $51,100
16.
17. if totalYearlySales > 50000:
18. print("greater")
19. elif totalYearlySales == 50000:
20. print("equal")
21. else:
22. print("less")

THIS CODE HAS BUG(S) that we need to find in this article. Here is the general approach that I would take. This may not be the only or the best way to go about debugging, just some general idea how to approach debugging and breakpoints.

1) Firstly I run the program and see what the result is. After running the program, it prints “less”. In order to know if it is the correct outcome, I need to figure out the EXPECTED result of the program. After doing the calculations (see in the comments of the code snippet) I see that the “totalYearlySales” should be $51,100 and therefore, our program should have printed “greater”. Now that I know that the EXPECTED result (“greater”) is different from the ACTUAL result (“less), I know that something is wrong in the program.

2) The last print statement happens in the last block of the code, so I put a breakpoint on the line 17:

17. if totalYearlySales > 50000:
18. print("greater")
19. elif totalYearlySales == 50000:
20. print("equal")
21. else:
22. print("less")

to see what the “totalYearlySales” is at that point. If I now run the program in the debug mode, it will stop on the line 17, and I can see that the ACTUAL value of “totalYearlySales” at that point is 30,295 that is different from our
EXPECTED value (51,100). Now, we know that something went wrong before the line 17.

3) Next I see that the “totalYearlySales” is last set at the following line, so I put another breakpoint on the line 15.

15. totalYearlySales = 365 * totalDailySales

After running the program, it stops at that line, and I check the EXPECTED and ACTUAL values of “totalDailySales”. After doing the calculations, the EXPECTED value of “totalDailySales” is 140, however, the ACTUAL value of of that variable is 83. That means that something went wrong before that line.

4) Going backwards I see that the “totalDailySales” is last set at the following line, so I put another breakpoint on the line 13.

13. totalDailySales = appleDailySales + orangeDailySales + bananaDailySales # expected: $140

After running the program, it stops at that line, and I check the EXPECTED and ACTUAL values of the following variables: “appleDailySales”, “orangeDailySales”, “bananaDailySales”.

  • The EXPECTED value of “appleDailySales” is 10 (1x10), and the ACTUAL value of our program is also 10.
  • The EXPECTED value of “orangeDailySales” is 40 (2x20), and the ACTUAL value of our program is also 40.
  • The EXPECTED value of “bananaDailySales” is 90 (3x30), however, the ACTUAL value of our program is 33. There is a mismatch for “bananaDailySales”, that means that something went wrong with that variable.

5) Going backwards I see that the “totalDailySales” is last set at the following line, so I put another breakpoint on the line 11.

11. bananaDailySales = bananaPrice + bananaSold

Looking more closely at that line, I notice that there is a logic error. Instead of multiplying the price with quantity (*), I added them together (+), that’s why the variable “bananaDailySales” is incorrect.
Voila, we found a bug. We fix it by replacing “+” with “*” and run the whole program again to see if it worked this time. After running the program
we see that it prints out “greater”, which we would expect, so it is likely that our program is working correctly.

6) It is possible that after fixing that bug and running the program, we would still get a wrong result (maybe the price of apple was typed wrong, or maybe the problem uses a leap year with 366 days instead of 365 etc.). In that case we would need to repeat the steps mentioned above until we find and fix all the bugs.

Conclusion: throughout this whole debugging process we used the basic principle of debugging that is comparing the EXPECTED and ACTUAL results at different points in the code. This code example was fairly simple, and experienced programmers could probably find the bug just by glancing at it without doing all these comparisons. However, in a more complicated code with with 1000s lines of code and multiple classes, functions and files, it won’t be as easy, but following these debugging steps, it will help get closer to finding the bugs in the code.

--

--