9.9 Debugging
The fine art of debugging can be an exercise in frustration. That’s a nice way of putting it. Fortunately there are many tricks and tools to help you chase down errors.
I confess I’ve mostly been using the old-fashioned way of tracking errors. I’d add outputs to the program. For example, I’d ask the program to print “got here?” at a certain point. If I saw that output, then I knew the error lay after that point. I’d also have it print out variables to check states. I’d do that for testing, yes, but also during the debug process.
Thing is, that really isn’t something I have to do anymore. It wastes time and can lead to a bunch of superfluous lines I have to track down and remove. Instead, I can use Visual Studio’s debug tool to help me out. If you aren’t using Visual Studio but are using an IDE (that’s a compiler with a graphical interface), then you probably have something comparable to what I’m about to show.
To use the debugger, you’ll need to set a breakpoint. The compiler will run the program up until that point and then pause, waiting for instruction. If I think the program is crashing at a certain point I can add a breakpoint and see if the program gets that far.
In this case, I set a breakpoint in run() on the line that calls “play()”. I did this by right-clicking on that line and selecting “set breakpoint”. A red circle appears off to the side. I then went to the toolbar and, instead of picking “start without debugging” I picked “start debugging”. Then program ran for a bit, then paused.
Fig. 09 09 01, click to view a larger version in a new window
You can now start stepping through your program, line by line, and watch the progress. There are three stepping options:
Step Into: Step into just goes to the next line. However, if the next line happens to be a function call, it will also go through the function.
Step Over: Just like Step Into, Step Over will move to the next line. However, if the next line happens to be a function call, it will step over that line. It will execute the function, but you don’t have to hit “Step Into” on every line in the function.
Step Out: If you get into a long block of code that you don’t really care about, say a function call, you don’t have to go through it line by line. Hitting the “step out” () command gets you out of the block and back to the main program.
Fig. 09 09 02, click to view a larger version in a new window
With this, I don’t have to fill my program with a bunch of “got here?”s. And, even better, I don’t have to remove them afterwards.
What’s more, I don’t have to print out any variables. Below, where the error list usually goes, Visual Studio has given a list of variables. All kinds of variables. Actually, any variable used by the program is available at a click of the button. It’s maybe a little confusing, takes a little getting used to, but can be invaluable during the debug process.
Fig. 09 09 03, click to view a larger version in a new window
There are several other debug windows. Take a gander at them. At beginning level programming, the variable window is probably all you need.
Visual Studio is a huge, complex program with lots of useful features that I’m just not going to get into here, even if I knew them all. Even this section is such a brief overview of the debug tool. But it can be a life-saver. I’d recommend playing around with it a bit.
![]() |
![]() |
![]() |
1: The Development Process
2: The Waterfall Method
3: Spiral
4: The Scrum
5: Cowboy Computing
6: Documentation
7: Diagramming
8: Tools
9: Debugging
In this book...
I: Hello World
In which we very quickly cover some basics of programming, and how that applies to building a game.
II: The Map
In which we build a rudimentary map for our game, along with a quick lesson on databases.
III: Data Structures
In which we cover data structures, ways to put our map information into our game program.
IV: Classes
In which we cover the basics of object oriented programming.
V: List Class
In which we build a list class to use in our game.
VI: Sorting and the Big O
In which we take a brief break to cover sorting algorithms, as well as code time and optimization.
VII: Binary Search Tree
In which we build a tree class.
VIII: The Game Begins
In which we apply all we've learned so far to create a game.
IX: Development and Debugging
In which we take an intermission and discuss the development cycle of programming, documentation, and finally the debugging and error-catching process.