"Effective C #" Item 4: Use the Conditional property instead of the IFENDIF block

xiaoxiao2021-03-30  193

When encoding, sometimes you need to add some debug information to facilitate debugging programs, but you don't want to compile this information when you are really released. So when you previously c or C encoding, use if / Endif to define this function, and this method can continue in C #.

This uses if / Endif to increase the DEBUG information block, as follows:

Way, embedding in a function, for example:

#if Debug

Trace.WriteLine ("ErrorMessage!");

#ENDIF

Second, fill the entire function with if / endif, and then call this function in the need to load, for example:

Private void debug_trace (String strmsg)

{

#if Debug

Trace.writeline (strmsg);

#ENDIF

}

In these two ways, there are some indifferent places.

For the way, the first increase of too many if / endif blocks will make the program become obscured, and the code is too confusing. In addition, the program may have potential bugs, consider the following code.

String strmsg = NULL;

#if Debug

Strmsg = "Hello World!";

Trace.writeline (strmsg);

#ENDIF

Console.writeLine (strmsg);

This code does not have a little problem in the debug version, but in the Release version, it is equivalent to as follows: Because the IF / ENDIF block block is not compiled.

String strmsg = NULL;

Console.writeLine (strmsg);

Obviously, such a code is problematic, and this problem is also difficult to find.

For the way, it is possible to block the defects of the way, that is, at least the program is not so embarrassed, and the potential bug exists in the way. But its implementation is a function, although in Release, this function is equivalent to an empty function, nothing, but when the program is executed, the call will be in this function, and return a lot of efficiency.

In the above two ways, I prefer the latter, that is, I would rather lose some efficiency, but I don't want to reduce the code readability. However, in C #, there is a better way to implement the ability of IF / Endif, which is to increase the Conditional property on the function, thereby achieving the effects of the same as the way, for example:

[Conditional ("Debug")]]

Private void debug_trace (String strmsg)

{

Console.writeLine (strmsg);

}

Obviously this kind of writing is very clear, the code is simple, and it is easy to read. And this kind of writing and mode can also be masked the potential bug. For efficiency, its efficiency and mode, I use the following call code.

Debug_trace ("Error Message1");

Console.writeline ("Hello World!");

Debug_trace ("Error Message2");

For this code, the IL code generated in Release is the same as the following code.

Console.Writeline ("Hello World!"); According to the above, a comparison table can be easily obtained.

IF / ENDIF block

Conditional properties

First way

Second way

Code readability

Poor, obscure

Better

effectiveness

high

low

high

other

Potential bug

There is no potential bug

There is no potential bug

However, the function using the conditional property has a limit, that is, there is no return value, that is, the return value must be defined as a void.

And use Conditional to increase multiple properties, for example:

[Conditional ("Debug"), Conditional ("trace")]]]]

Private void debug_trace (String strmsg)

{

Console.writeLine (strmsg);

}

However, the relationship between the two attributes is or the relationship, "Debug" or "trace" is defined, then this code will be executed. If you need to add two and the properties, use Conditional that cannot be implemented, you need to do it by means of if / endif, as follows:

#if (Debug && Trace)

#define Both

#ENDIF

[Conditional ("Both")]]]]

Private void debug_trace (String strmsg)

{

Console.writeLine (strmsg);

}

Methods have been said to be basically finished. Next, talk about how to set up environment variables, there are three sets of settings in C #.

Method 1, directly in the program, need to be defined in one file;

Method 2, setting in the compilation command line in the project;

Method 3 is to increase environment variables in the operating system.

These three methods, the most commonly used, the first two, how to set it, I don't make a preliminary, see

MSDN

Can complete

转载请注明原文地址:https://www.9cbs.com/read-130488.html

New Post(0)