Class Notes: Code tracing and debugging

Once you come up with an idea for a code, it is almost never the case that your first implementation will work. When this happens, you need to take a breath and start debugging. Debugging literally comes from de-bugging which means cleaning the bugs, flaws, errors, issues related to code and software. Debugging is basically tracing your program in your head (often with the help of some pen and paper) for some particular input, and finding out where and why it fails. Obviously you should choose an input that makes your program fail, but something small enough so that you don't need to run too many steps until failing. Your input should be as simple as possible and as complicated as necessary. If you happen to have a computer around, you can use it to help you trace the code by adding print statements to your code.

Although it can be said that debugging would be best learned through real life practice of fixing errors, there are some basic knowledge and methods that can raise your awareness, save you time and make you a much more efficient debugger as well as coder. Debugging / Testing is a huge topic in software development but as a beginner-intermediate programmer one usually needs much more primitive debugging skills and knowledge. Once testing has uncovered problems, the next step is to fix them. Many novices do this by making more-or-less random changes to their code until it seems to produce the right answer, but that’s very inefficient (and the result is usually only correct for the one case they’re testing). The more experienced a programmer is, the more systematically they debug, and most follow some variation on the rules explained below.

print is a python command used to print things to the console.

Whatever is in the parentheses is going to be printed. If the thing in parentheses is in quotes, it is going to be printed ipsis litteris (word per word). If the thing in parentheses is a variable, python prints the value that is inside that variable.

Quotes and variables can be combined inside prints to make the messages more understandable. A comma indicates there is a space between the parts.

Note that this does not alter the flow of your program. Python does nothing to whatever is printed, and this is only for your scrutinity.

You can use print to find out how many times a loop runs, and what is the value of the loop variable.

prints are particularly useful if there are loops inside loops.

Worked eamples: Catch the bug

Add print statements to trace the code and spot the "bugs"

  1. Try some test cases (localize the bug)
  1. Is it correct? Yes, try another test case. No, we found the bug!
  2. Trace the code by adding print statements to your code.
  1. Do you see something wrong? Yes, the for loop should iterate more times.
  1. Try some test cases (localize the bug)
  1. We have a runtime error! If we cannot immediately see what the problem is, let's trace the code.
  2. Trace the code by adding print statements to your code.
  1. Do you see something wrong? Yes, too many iterations! The value of bit is float! why is that?
  1. Test gain. Do you see something wrong? Yes, too many iterations! Infinite loop

Now it's your turn. Find the bug in the function below: