I'm writing some debug code at work, and I'm wondering if what I'm doing might hurt performance or not.
Let's get to the code:
foreach (var item in aCollection)
Debug.WriteLine(item.Name);
I know that the Debug class uses the Conditional attribute to avoid compilation in release mode (or whenever DEBUG is undefined), but would this end up as a futile/empty iteration when compiled in release mode or would it be optimized by the compiler?
You are not leveraging any compiler symbols here. Wrap it inside these:
#if DEBUG
// your code here
#endif
Advantages of this approach:
[Conditional]
method attribute. It is not obvious on the invoking side that an invocation will not take place in the compiled code. Teams should refrain from that practice in favor of more explicit conditional compilation methods. Even commenting is not advisable because there's always that someone in large teams who forgets to comment stuff like this. The example above, instead, is easy to read (VS2010+ even shades the text when it is not part of the current build profile).That foreach will not be wiped away.
The compiled code will be something like:
foreach (var item in aCollection)
{
;
}
the collection will be enumerated anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With