Debuggers, breaking bad code


That program you’re using to type all your code in to? It’s not just for typing code in and pressing “play”. It’s an IDE (Integrated Development Environment). This means it contains many tools to help you develop, one of those tools is the debugger. I suggest you read more about debuggers here.

How do you use the debugger?

It’s going to depend on your IDE of choice (they likely contain very similar debuggers, but just go with it). For Java there are a few major ones: Eclipse, Netbeans and more. Regardless they all have some similar features. One of the most powerful features is breaking, breaking can happen automatically from an exception (as I’m sure you’ve seen), or it can happen manually via the use of breakpoints. For situations like yours, breakpoints are your best friend. They will allow you to stop execution of your code at a specific point, and examine the state of your program at that time.

How do you use breakpoints?

Again, this is IDE specific. Commonly it’s a right click off to the left side column of the code window and selecting “Enable breakpoint”. Yes, it’s that easy. After setting a breakpoint, you’ll start your program in debug mode. This mode lets the IDE know that it should pay attention to breakpoints and other debug features. Now, when the code execution gets to the point in your code where you set your breakpoint, it stops, and turns control over to you. This is awesome. You can now view what variable values are, many IDEs let you execute code snippets to see their result against the current state of your program, change values and more! This is SO powerful. Breakpoints rock.

Now that your program execution has stopped, it’s not dead. It’s waiting for you to tell it what to do next. Again, you have some powerful options. You can continue execution as normal, you can step into functions, step out of functions or step over line by line. Amazing. It’s often useful to set a breakpoint at the beginning of a function, then step through the code line by line, seeing the variables change and following the execution of the code. The line by line stepping is by far the coolest way to follow the execution of your program.

Short URL for this post: