Comparing Conditional Attributes in C/C++ versus C#


I am a C++ programmer, but today I am assigned to a C# project and I found an article on your site. However, I guess the article is inaccurate and hides information. Author may tried to expose beauties of C# by fading others' features. The intention should never be comparing languages, because each have its own features that other does not have. However, C++ compilers improved too much during last 20 years, and language became mature.

--------
(taken from http://www.c-sharpcorner.com/Effectivecs/DebugTechniquesMB001.asp)

[Conditional("DEBUG")]
public void Debug( string msg )
{
// this is where the debug statement display code would be
}

What this will do is when this program is compiled it will check if DEBUG was #defined and if it was then the call to this function will be remain, however if DEBUG is not #defined all calls to this function not will be compiled leaving us with no performance hit.

Now, some of you may probably say that you could solve some of the problems above in C/C++ and Java. I will note that neither language solves all of them. For example in C/C++ you can #define and #ifdef much like you can use ConditionalAttribute class and C# defines. But getting useful meta-information in C/C++ is limited. And in Java getting meta-information is possible but as far as I know all the code is always compiled (i.e. theres no Conditional type functionality).

Thankfully there is C# which does solve (gracefully I might add) all the problems I mentioned above and quite a few more.

-------

Yes, I claim "I can do even better with C++". meta-information is totally useless, it is a garbage in our metadata. What would i do this meta data on final release?

Author hides a fact. ConditionalAttribute class cannot be used inside a method. that is

private void f()
{
// do something
[Conditional("condition1")] // error!
// do something else
}

but C++ does

private:
void f()
{
#ifdef condition1
/*..*/
#else
/*..*/
#endif
}

C++ (or C) can do this:

const char g_szText[] = "HELLO "
#ifdef condition1
"CONDITION1"
#else
"SOME OTHER CONDITION"
#endif
;

you can do even more crazy stuff..

ConditionalAttribute has a quite smaller scope/use than C's #ifdef preprocessor directives. because when we build, that means we already know enough about target and the code that we should generate. so meta information becomes useless. for debugging, we have trace facility. if you insist, in C++, you can place meta info explicitly by preprocessor directives again (into executable file, not much like meta data).

This is just a correction with no insult intended.

Up Next
    Ebook Download
    View all
    Learn
    View all