A Guide to Debugging
Author: Zoe Plaxco

A. Introduction to Debugging

We have discussed why testing is important and how to do it efficiently, but what do we do when we have found the method where the bug is, but everything looks fine? We are now going to walk through the most important tool we have access to in cs61b - the debugger. This is a tool built into which allows you to step through your code line by line, providing a visual layout showing you the values of every variable at that moment in the code (using the Java Visualizer).

B. Running the Debugger

First, read this.

Open this assignment in IntelliJ. Open up the provided signpost file and try setting a breakpoint by clicking to the right of the line numbers. Breakpoints are lines of code where we want the dubugger to stop running and let us start stepping through at our own pace. This is useful as our aim is to use the debugger to test a specific method.

To run the debugger, click the bug icon in the top right, or right click the green run arrow to the left of the method and select the second option, "Debug".

Now, how do we actually walk through our code? Read this breakdown of the step buttons. Note that the first four are likely the only ones you will use. Try stepping through the entirety of our loop!

Next, try right clicking your breakpoint (on the red dot). A small dialog should appear with a section for a "Condition". Try putting in a condition for a specific variable (such as f==1). See how the debugger acts differently!