Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Debug only" code that should run only when "turned on"

I would like to add some C# "debug only" code that only runs if the person debugging requests it. In C++, I used to do something similar to the following:

void foo() {      // ...   #ifdef DEBUG   static bool s_bDoDebugOnlyCode = false;   if (s_bDoDebugOnlyCode)   {       // Debug only code here gets executed when the person debugging        // manually sets the bool above to true.  It then stays for the rest       // of the session until they set it to false.   }   #endif  // ... } 

I can't do exactly the same in C# since there is no local statics.

Question: What is the best way to accomplish this in C#?

  1. Should I use a private class static field with C# preprocessor directives (#if/#endif DEBUG)?
  2. Should I use the Conditional attribute (to hold the code), and then a private class static field (not surrounded by C# preprocessor directives #if/#endif DEBUG?).
  3. Something else?
like image 317
Matt Smith Avatar asked Feb 22 '11 15:02

Matt Smith


People also ask

What does running in Debug mode mean?

Running an app within a debugger, also called debugging mode, means that the debugger actively monitors everything that's happening as the program runs. It also allows you to pause the app at any point to examine its state and then step through your code line by line to watch every detail as it happens.

What is the Debug mode code?

Debug code is computer code introduced to a computer program to test for errors or to help determine the cause of an error. It can be as simple as an echo command to print the value of a variable at certain points of a program.

Which options are available in Debug mode?

Some of the major functions available with on-chip debug include: Interrupt or break into debug mode on program and/or data memory address. Interrupt or break into debug mode on a peripheral access. Enter debug mode using a DSP microprocessor instruction.

Is Debug mode slower?

One of the main reasons that the debug version is significantly slower is because of these extra diagnostics. as to why you want to run in Debug, it's because those extra diagnostics are doing lots of useful stuff that help you catch bugs in your program so that you have more chance of the release build working.


1 Answers

An instance variable would probably be the way to do what you want. You could make it static to persist the same value for the life of the program (or thread depending on your static memory model), or make it an ordinary instance var to control it over the life of an object instance. If that instance is a singleton, they'll behave the same way.

#if DEBUG private /*static*/ bool s_bDoDebugOnlyCode = false; #endif  void foo() {      // ... #if DEBUG   if (s_bDoDebugOnlyCode)   {       // Code here gets executed only when compiled with the DEBUG constant,        // and when the person debugging manually sets the bool above to true.         // It then stays for the rest of the session until they set it to false.   } #endif  // ... } 

Just to be complete, pragmas (preprocessor directives) are considered a bit of a kludge to use to control program flow. .NET has a built-in answer for half of this problem, using the "Conditional" attribute.

private /*static*/ bool doDebugOnlyCode = false;  [Conditional("DEBUG")] void foo() {      // ...       if (doDebugOnlyCode)   {       // Code here gets executed only when compiled with the DEBUG constant,        // and when the person debugging manually sets the bool above to true.         // It then stays for the rest of the session until they set it to false.   }       // ... } 

No pragmas, much cleaner. The downside is that Conditional can only be applied to methods, so you'll have to deal with a boolean variable that doesn't do anything in a release build. As the variable exists solely to be toggled from the VS execution host, and in a release build its value doesn't matter, it's pretty harmless.

like image 190
KeithS Avatar answered Sep 22 '22 13:09

KeithS