Home > General, Technology, Tutorials > Debugging 101

Debugging 101

As a frequent visitor and contributor to VBForums, I am constantly amazed at the number of times when some one suggests that the code needs to be debugged or stepped through, and the response is “How do I do that?” Granted, there is a great number out there that are learning programming on their own, and it may or may not be something that is obvious. In other cases, it’s apparent the debugging isn’t being taught in many classes. I find this a little sad if not maddening. Part of the interview process of my previous job was to go through some unfamiliar code (based on VB6) in an unfamiliar IDE (if it can be called an IDE, it was texted based, almost like I was back in DOS). I had been given some basics of what the app was supposed to do when it was done, and what errors it was generating. The accompanying information sheet then also gave a quick break down of the commands needed to use the IDE. For those really new, IDE stands for Integrated Development Environment. Visual Studio is an IDE. Mono, Eclipse are examples of two more. There are others, but you get the idea.

In my opinion, debugging is such a simple and basic technique that I’m surprised it isn’t taught in may curriculum. and yet, here we are with a great number of people that simply do not know how to debug properly. So I am going to run through some of the more typical ways one would debug an application. While the focus will be on tackling a VB.NET app, most will also be applicable to C#, and where I can, I’ll also share some VB6 equivalents.

Message Boxes

Generally debugging involves two kinds of thinking. The first is the logic flow, where is the path of execution going? The other is what is the value of something? The where and the what. the quickest and dirtiest method for this is to use Message Boxes. I’ve done this… in an attempt to find out where the heck my application is running off too, I’ve peppered my code with MessageBox.Show() (or MsgBox in VB6) with unique messages to let me know where the execution is going. The good aspect is that they are super easy to put in, copy and paste, paste, paste. The down side is if you use too many, you spend more time clicking  “OK” and may hit it with out paying attention to the messages, and so you then wonder where the heck you just were.  Another problem with using Message Boxes is their tendency to stop execution, as well as take focus away from the current form. I’ve run into problems with both of these before, needless to say I don’t use that method very much any more.Plus if you stick in in a loop, if it is sizable, you’re going to be clicking a lot of OK buttons.

Debug Print

The next typical debugging method is to use Debug.Print (for write, writeline, or even the Assert class). This dumps the output to the output window in the IDE. This handy for high-speed code or loops, where there’s a lot of messages. Saves the wear and tear on your mouse as you don’t need to click an OK button each time a message goes out. And since it goes into the Output Window in the IDE, even after you stop the app, you can view what what printed out. Most of it at any rate. The window does have a limit on the number of lines. You can also get a lot more complex on the output, adding some basic formatting. I do this when checking data in parent/child relations, or in a loop. the header will be fully left justified, and child data will be indented or offset in some manner.

That’s it for the really and truly basics of debugging. Using Message Boxes and debug print statements should be basic techniques that work in just about any language or IDE that supports them. A word of caution. Message Boxes WILL ship with the app if you forget to take them out, so be careful to remove every that you add. Debug.Print statements, while they will compile with the app, they only have meaning during a debug run of the app in the IDE.

Next up I’ll show how to use breakpoints, and how to determine variable state & values using intellisense, the watch window and the quick watch feature.

I am going to turn my attention now to something a bit more advanced than simple messages. In this section, I plan to talk about how to set breakpoints, use the Watch Window, tool tips and the Quick watch feature. All of these options should be available in all versions of Visual Studio from 2005 and on, with the exception of the Quick Watch, which I believe was introduced in VS2008. I use the “General Developer Keyboard Settings” for Visual Studio, and so the function keys that I mention reflect this.

Breakpoints

Breakpoints are probably just about the easiest thing to work with. With a single key press or a single click they can be toggled on or off. From inside the IDE, open up any code file, click on any line that has executable code (like a sub header, or an if statement, or something like that) and press the F9 key (from the menu it is Debug -> Toggle Breakpoint – if you aren’t using the General Developer settings, it should also tell you what the keyboard shortcut for it is). With the default colors, the line should light up with a red background. In addition a red dot should appear to the far left in the code window margin. That’s the second common way to toggle a breakpoint. Clicking the red orb will cause the breakpoint to disappear. If you then click that same spot again, the breakpoint reappears.

So what good is a break point? Plenty. It is like a checkpoint for your code. It causes execution to stop (again, only when running in the IDE in debug mode) and then highlight your code. From there you can check the value and state of just about any object/variable that is currently in scope. You can also then step through the code one line at a time, using F10/F11. The F10 key does a what is called a “Step Over” operation. What this means is that if you press it while on a line that calls a function, it will execute the function, then return you to where you are when it is done. It steps over the function. Step Into (F11) will actually go into the function and put you on the first line in the function, where you can Step Into or Step Over through the function. Sometimes you know a particular function is working and don’t need to go into it. In those cases, Step Over will save lots of time.

Variable State & Values

Using Intellisense/Tool Tips

When the application is paused, the quickest way to determine the value of a variable simply by hovering over it.

This even works for complex data, the tool tip tag can be expanded out to show more properties, values and objects.

Sometimes, hovering over an object will return a sub-object, when you really want to see the parent object. This is easy to overcome by highlighting the portion you’re interested in and hovering over that.

Quick Watch

Another option  for viewing the values of variables and objects is something known as the Quick Watch. It’s similar to the Watch Window (explained below), But allows better drill down of the object, and you can manipulate what you are seeing a little easier. To access it, highlight your object, then right-click and select “Quick Watch.”

The window that gets displayed is similar to the tool tip tag that pops up when you hover. You can also expand child properties that allow it.

Watch Window

The last thing I want to cover in this Debugging 101 posting is the Watch Window. To add something to the Watch Window, it’s the same as using the Quick Watch. Select a varaible or object during a paused debugging session, right click and select “Add Watch”.

This will add it to the Watch Window at the bottom of the IDE.

How is this different from the Quick Watch? The Quick Watch is single object oriented. With the Watch Window, you can actually track multiple objects  at the same time, you can view the data as you step through the code – the Quick Watch is a dialog window, so you can’t continue code execution until you close it.

The Wrapup

That’s it.  It’s a brief introduction into some of the tools available to you to aid in debugging your application. This by  no means is a be-all, end-all discussion on debugging, but at the very least, it should introduce you to some of the options and how they can be used.

Advertisements
Categories: General, Technology, Tutorials
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: