Friday, April 12, 2013

Debugging: What is it?

Debugging is the process of removing "bugs" from code. There are many different ways to do this, and sometimes it can be a long, drawn-out, and frustrating struggle. The problem is: everybody makes mistakes. No one can sit down and write 1000+ lines of code without something going wrong and misbehaving. What distinguishes good coders from great coders, among other things, is their ability to debug. Over the past few weeks I have been doing a lot of debugging, and I have been working on my techniques. I have been getting faster and faster at identifying the root of the problem. There are a few ways that I have become familiar with to watch your code as it executes.

For code with lots of conditional branching, when something goes wrong, you need to know exactly where something goes wrong. In this case, a programmer can insert code that writes to the console at specific points in their code in order to follow-along. I am quite fond of just printing single "*" characters or maybe "+" or "-" characters after condition checks, this allows me to tell exactly which branch of code is being executed when the undesired behavior occurs.

Another way to do this is to print out all the objects involved in an event, and do the condition checks manually. This process can be quite tedious because the programmer needs to define how the object "prints" in text. But thanks to Eclipses' brilliant generate toString() functionality, this process is pretty pain-free.

The last process I have been using to debug my code is really only useful for visual programs such as my Physics Engine. I have defined a few methods in my Entity class that allow me to draw either the entity's previous path or the entity's velocity vector, or both. This allows me to visually watch what is happening behind-the-scenes of my application. In this picture, you can see the path and the velocity vectors of the balls, along with their direction, converted to degrees and printed above their image for clarity. This allows me to slow down their motion, and carefully watch their behavior. This technique has come in quite handy, as I've recently discovered and fixed more than a few minor bugs in my program.

Thanks for reading!

-Jeff

No comments:

Post a Comment