Debugging with println()

Finding Bugs

In programming, any unwanted or unintended property of a program or piece of hardware, especially one that causes it to malfunction, is referred to as a bug. The term originated in 1945 when Admiral Grace Hopper traced a malfunction with the Harvard Mark II computer to an actual bug (specifically, a moth) caught in one of the electronic relays. She taped the moth into her log book and noted it as the “First actual case of bug being found.” The name stuck and ever since, programmers have been plagued (no pun intended) with bugs.

Finding software bugs in your programs is hard. It may be the most difficult part of programming. Of course, the simplest way to prevent bugs is to not have any in the first place! Planning the logic and anticipated input/output of your code before actually typing it into the interpreter is important. This has spawned the humorous adage:

Weeks of programming can save you hours of planning.

Of course, programs are written by people, and people make mistakes. Even the best programmers are not immune to buggy code. Computer scientists have developed a multitude of methods for finding errors in programs:

  1. Examples of intended behavior on specific inputs help people understand what a program is supposed to do. Programmers can add comments within the program to describe what should happen based on specified inputs.
  2. Programs should be kept as short and concise (while doing what it is intended to do) as possible. Duplicated code and longer code segments can make it harder to reason about a program’s functionality.
  3. Comments should be used effectively so a programmer can explain how a piece of code is used within the program. Comments can also help justify and explain a program’s correctness.
  4. The simplest—and the one you should concentrate on using—is called “print debugging.”

Printing Variables

Consider the following code that exchanges the x and y values of a point:

size(100 , 100);
strokeWeight(5);
stroke(0);
int x = 5;
int y = 95;
point(x , y);
x = y;
y = x;
point(x , y);
Expected Output Actual Output
Two points at opposite corners Two points at adjacent corners

Clearly the variables x and y are not what we expect them to be. So, what are they? Adding println(x); after each point in the program where we make a change will tell us just that (similarly for y):

size(100 , 100);
strokeWeight(5);
stroke(0);
int x = 5;
int y = 95;
point(x , y);
x = y;
println(x);     // for debugging
y = x;
println(y);     // for debugging
point(x , y);

This prints the following to the output window — not quite the values that we expected. Where have we seen this error before?

95
95

Printing Checkpoints

Another type of error occurs when your program crashes and you cannot easily determine where. To figure out where the problem is, you can add println() statements to your code at various points:

// ...a bunch of stuff...
println("You reached point A");
// ...some more stuff...
println("You reached point B");
// ...just a little bit more stuff...
println("You reached point C");
// ...this is the last bit of stuff I promise...

Imagine that when the above program is run, it prints the following before crashing:

You reached point A
You reached point B

Clearly the problem lies in the area ...just a little bit more stuff.... The program execution never made it through these statements to print You reached point C.