The Side Effects of Debugging Code

David Kline makes a great point about the side effect of debugging code.  In particular, if you have properties that actually do more than simply returning a value, you can unexpectedly modify the state of the system just by exploring an object in the watch window. 

Visual Studio 2005 supports a number of attributes that can help control how objects are viewed during debugging:

DebuggerDisplay – This controls how the single line summary of the object is rendered when you hover your mouse over an object when in break mode.

DebuggerHidden – Can be used to attribute a method/property that you don’t want the debugger to step into when debugging.  Note that you can’t place a breakpoint in a method/property with this attribute, nor will this method/property appear in the call stack if you place a breakpoint further down the call stack.

DebuggerStepThrough – This marks code that will appear as “external” code when debugging.  Again you can’t place a breakpoint in this code.

DebuggerNonUserCode – Just when you thought there couldn’t be any more permutations on what will/will not be stepped through…. This attribute controls whether your code is stepped through when the “Just My Code” option is enabled (see Tools –> Options).

DebuggerTypeProxy – This attibute allows you to redirect the debugger data tip to display a substitute object in place of the object you are hovering over.  This can be great if you want to simplify the way your object is displayed during debugging.

DebuggerVisualizer – Use this attribute if you want a richer display for your object.  For example if you hover over a String object you will notice there is a magnifying glass in the data tip.  Clicking this brings up the String visualizer which is a window where you can view the entire string.  You can write your own visualizer by inheriting from the DialogDebuggerVisualizer class and then attributing your class with the DebuggerVisualizer attribute.

<Shameless plug>Samples of using these attributes is available in Professional Visual Studio 2005</Shameless plug>


Leave a comment