Debug Pre-Processor Directives

March 29, 2011

If you use any of the methods in the Debug class (such as Debug.Write), you should be aware that these statements will still be in your compiled code (release or debug build) and will still run on your production server.

It could be that such statements make a negligable difference to the performance of your code, but imagine the scenario where the developer wants to check the sum or 3 or 4 items, or worse still a string concatenation to show maybe 3 or 4 variables, within a Loop – This is additional processing that is completely needless!

To avoid this, I use Pre-Processor Directives – Don’t worry about the name, like alot of development related speak they sound scary and complicated, when in fact its a very simple thing to do in order to avoid all they needless processing…

// The first line in your code, define the DEBUG symbol
#define DEBUG

// Then, where ever you use the DEBUG class, wrap your code in an if statement as follows
#if DEBUG
    DateTime startTime = DateTime.Now;
    Debug.WriteLine("ProcessRequest() Commenced: " + startTime);            
#endif

The code within the #if/#endif will be excluded from the compiled code, except of course when you are debugging.

This is a much more elegant solution than having these statements run in release code, or having to strip them all out before a release build.

Advertisements

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: