473,837 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Conditional("DE BUG")] or #if DEBUG

After reading C# documentation the Conditional attribute seemed the way to
go, but after inspecting the IL it seems those methods are still there and I
imagine the CLR removes them. Using #if DEBUG means the code does not even
reach the IL when compiling in release mode.

Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using [Conditional("DE BUG")]
over #if DEBUG.
Nov 15 '05 #1
10 28688
John,
There is no benefit of using [Conditional("DE BUG")] to the called method
itself per se, the benefit is for the calling methods. You can leave all the
calls to a [Conditional("DE BUG")] method and the compiler will quietly
change them into no ops on release builds, or the actual method calls in
debug builds.

What I will do is use both [Conditional("DE BUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly, for public methods in class libraries I will use only
[Conditional("DE BUG")] as assemblies compiled against the class library may
or may not have defined DEBUG.

For an example of the use of [Conditional("DE BUG")], see all the methods in
the System.Diagnost ics.Debug & System.Diagnost ics.Trace classes. :-)

For more details see:
http://msdn.microsoft.com/library/de...TraceDebug.asp

http://msdn.microsoft.com/msdnmag/is...r/default.aspx

Hope this helps
Jay

"John Smith" <so*****@micros oft.com> wrote in message
news:5y******** *************@t wister.southeas t.rr.com...
After reading C# documentation the Conditional attribute seemed the way to
go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG means the code does not even
reach the IL when compiling in release mode.

Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using [Conditional("DE BUG")] over #if DEBUG.

Nov 15 '05 #2
Thank you for the clear and concise answer. Public class libraries seem the
reason d'etre for the Conditional Attribute.

Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL.
Nov 15 '05 #3
I'd say there's another good reason to use the Conditional attribute:
the code that calls the debug method is much easier to read.

With the traditional approach, you can't just #ifdef out the debug
method because the calls to that method will then fail to compile, so
you have to surround them with #ifdef's as well which introduces a
great deal of clutter in the code.

With the Conditional attribute, you can just leave in all the calls,
and the compiler (don't know which one?) will silently remove them in
Release builds at no loss of runtime speed.
--
http://www.kynosarges.de
Nov 15 '05 #4
John,
Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL. No I mean the C# compiler, as the C# compiler already has enough info (DEBUG
is or is not #defined) to decide to include the call or not. A quick test in
VB.NET there were no operations emitted, not even the noop op code.

You can use ILDASM.EXE to see the difference to the IL generated.

Hope this helps
Jay

"John Smith" <so*****@micros oft.com> wrote in message
news:E3******** *************@t wister.southeas t.rr.com... Thank you for the clear and concise answer. Public class libraries seem the reason d'etre for the Conditional Attribute.

Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL.

Nov 15 '05 #5
>>the code that calls the debug method is much easier to read.
Agreed. That is why I have been using them.
the compiler (don't know which one?) will silently remove them in
Release builds at no loss of runtime speed.

No loss of runtime speed is what I am not 100% convinced of. The methods
remain in IL so there must be some work -no matter how small- done by the
JIT(??) compiler to skip those methods. This is worrying because I was
calling a Conditional method from BeginRequest in global.asax (an ASP.NET
event which is called for every web page). For now I just converted them to
use the untidy #if statement which decompiling to IL proved has no
performance penalty.
Nov 15 '05 #6
Hi Jay,
What I will do is use both [Conditional("DE BUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly,


Are you using #if #endif only around the method body then? Cause you
can't exclude the entire method, since removing conditional method
calls is done after the "preprocess ing" step in the compilation
process. The following doesn't compile if DEBUG isn't defined.

#if DEBUG
[Conditional("DE BUG")]
static void Foo() {}
#endif

static void Main()
{
Foo();
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #7
No loss of runtime speed is what I am not 100% convinced of. The methods
remain in IL so there must be some work -no matter how small- done by the
JIT(??) compiler to skip those methods.


JIT compilation is done on a method by method basis. If a method is
never called, it will never be JIT compiled. Also, there's nothing for
the JIT compiler to skip, since the calls are removed by the C#
compiler and aren't present in the IL.

So the only way conditional methods can affect performance is that you
have dead code bloating the assembly the methods are in, which may
cause it to take a millisecond or two longer to load.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #8
Mattias,
Yes the method body, as you still need the method itself, for the rest of
the code to compile... I guess I was not as clear as I wanted to be on that
one ;-)

Didn't you and I discuss this once a long time ago. Whoever it was, is why I
add the #if DEBUG to the method body also. Although most of the time I have
the Conditional on public methods, so I don't have a lot using #if DEBUG.

Thanks for clarifying that!
Jay

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:%2******** **********@tk2m sftngp13.phx.gb l...
Hi Jay,
What I will do is use both [Conditional("DE BUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly,


Are you using #if #endif only around the method body then? Cause you
can't exclude the entire method, since removing conditional method
calls is done after the "preprocess ing" step in the compilation
process. The following doesn't compile if DEBUG isn't defined.

#if DEBUG
[Conditional("DE BUG")]
static void Foo() {}
#endif

static void Main()
{
Foo();
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #9
Didn't you and I discuss this once a long time ago.


Not that I recall, but perhaps your memory is better than mine. :-)

Mattias

--
Mattias Sjögren [MVP]
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5278
by: Harry | last post by:
Hi All, Can anyone clever out there tell me why the below script does not work! - I have a page with two radio boxes with values of "agree" and "not_agree". - The form is set to GET which goes to the below script for processing. - No matter which of the two radio boxes are selected, it always goes to the page "/broadband/order.asp".
3
1658
by: Whiplash | last post by:
I've got a web app that has the debug parameter in the web.config file set to true. Everything works great. However, I want to set debug to false before putting the app into production. When I do so, I get an error that states; "Object reference not set to an instance of an object". It also does not have any more information and tells me to set debug to true to see more info. But of course, when I set debug to true, the app works fine! ...
7
14129
by: What-A-Tool | last post by:
In the following code, "catch" is underlined in blue, and the error "the variable fx is declared but never used" is displayed when I mouse hover over it. Am I doing something wrong, or do I just ignore it? private void btnCalc_Click(object sender, System.EventArgs e) {
3
2315
by: mosscliffe | last post by:
I need to reference a file on my published server as "../mydir/myfile.txt", which is fine and it works. On my local develop computer the "../mydir" refers to C:\Program Files\Microsoft Visual Studio 8\Common7 Is there any way I can get the "../" to refer to somewhere more convenient, ideally as /mydir within my project. I could not find any settings in Visual Studio, which referred to this
1
2314
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
8
2177
by: Bruce | last post by:
I am using VB in Vs2005. Am I missing something or does VB not have the concept of "builds" (release/debug) like in VC? I wrote an assembly and I would like to have a debug version of the DLL and a release version of the DLL. I would like to create some sample code that demonstrates my assembly in VB and would like to have two builds, one using my debug version and the other using the release version of the assembly. How can I do...
4
3350
RMWChaos
by: RMWChaos | last post by:
Darnit all, I expect the code I steal from others to work! =D Below is some code that I got to initiate multiple javascripts on page load (rather than using the "onload=" attribute). According the blog where it was posted, it seems that it works for everyone who posted replies...not a single "this is not working for me" post. The errors I receive are as follows: IE7: expected ";" and then "conditional compilation is turned off" FF &...
0
9693
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7012
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5862
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.