Monday, June 4, 2012

Debugging tricks

I was sitting with someone the other day and watching him debug an application and it made me want to post a couple tricks with the debugger that can help. 

first: learn some of the simple shortcuts. F5 starts the app with debugging ctrl+F5 starts it without. I was surprised by the fact that he took his hands off the keyboard to look for the mouse so that he could click the little icon on the toolbar every time he wanted to run something.

F9 sets (and clears) breakpoints – this is probably the handiest thing--  and F11 will “step into” a method, while F10 will step over it.
Those are the basics.

In addition, you can go to view->other windows->command window in Studio and have access to all the debugger info (and then some). A couple of the most useful things are:

        “Debug.DisableAllBreakpoints” and “Debug.EnableAllBreakpoints”. These alone are so useful, it’s scary. On any complex application, you know, the one that you’ve been working on for the last 3 days without coming up for air, you’ve got lots of steps to do before you get to the “good stuff”. Once you do actions X + Y, you want to step through loop Z to see what it’s doing. But you don’t want to step through Z until you’ve done X and Y. So you can end up stepping through lots of code that you really don’t care about. But if you disable all breakpoints, then run, do X and Y, then Enable all breakpoints, you will fly right past your breakpoints until you need them. Very cool.



      typing ? followed by a variable (there needs to be a space between the ? and the variable name) will print the contents of the variable. This is basically the same as hovering over the variable with your mouse or doing a quick watch, but typing it is easier when you’re looking for someArray[i].someObject.someProperty . I’ve seen lots of people spend all day trying to “drill down” in hover over without clicking on the wrong thing and making it vanish. I do it too, but the debug command is way easier. 

 
      Also, remember conditional breakpoints. When you set a break point, you can right click on it and select “condition”. This will open a dialog that will allow you to enter whatever you like and stop only when the condition is true. So, you can break on the 321st iteration of the loop without having to step through the first 320. 

.  Hope this all helps. Please list some of your favorites below. 
-    --kevin

1 comment: