At BataviaLabs we were debating coding guidelines the other day and came across this one: do you use #if or the ConditionalAttribute to indicate to the compiler if a method should be compiled. Let me elaborate...
If you have a method you only want to compile in a debug scenario, you have the following options:
1) Use #if DEBUG as shown below
class Program { static void Main(string[] args) { #if DEBUG SomeMethod(); #endif Console.WriteLine("End"); Console.ReadKey(); } #if DEBUG internal static void SomeMethod() { Console.WriteLine("SomeMethod"); } #endif }
2) Use the ConditionalAttribute as shown below
class Program { static void Main(string[] args) { SomeMethod(); Console.WriteLine("End"); Console.ReadKey(); } [Conditional("DEBUG")] internal static void SomeMethod() { Console.WriteLine("SomeMethod"); } }
The difference between these two methods is enormous. The first sample is very explicit. Any code you don't want to compile into the production build is placed between #if DEBUG and #endif. If you try to call SomeMethod in a production build, the compiler will give you a compile error. The ConditionalAttributeon the other hand doesn't require you to remove the calls to SomeMethod. If a method is marked [Conditional], any calls made to that method are removed from the build by the compiler. A proviso here is that [Conditional] only works with methods that don't return a value (i.e. void).
I much more prefer #if DEBUG, because it is explicit. I can't run into a situation where from reading the code I'm thinking "SomeMethod is being executed", but it actually isn't because the compiler removed the call. Comments anyone?
Remember Me
a@href@title, strike
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.