Effective C # principle 4: precompiling blocks with conditional attributes instead of #if

Source: Internet
Author: User

Using the #if/#endif block can generate different compilations (results) on the same source code, most of the debug and release two versions. But they are by no means the tools we like to use. Because the #if/#endif很容易 is abused, the code that is written is difficult to understand and more difficult to debug. Programming language designers have a responsibility to provide better tools for generating machine code in different operating environments. C # provides conditional attributes (Conditional attribute) to identify which methods can be used to determine whether they should be invoked, depending on the environment setting.

There are two words in C #, one is property, the other is attribute, they have no meaning, but when translated into Chinese it is usually translated for attribute. Property refers to the nature of an object, that is, the attribute in Item1. The attribute here refers to the properties attached to. NET as a particular class, method, or property. You can find attribute in MSDN to get more help, in short, note: attribute and property meaning is completely different. )

This method is #endif更加清晰明白 than conditional compilation #if/. The compiler can recognize the conditional property, so when the conditional attribute is applied, the compiler can do a good job. Conditional attributes are used on methods, so this uses the code you have to use under different conditions to write to different methods. When you want to generate different code for different conditions, use the conditional attribute instead of the #if/#endif块.

Many programmers use conditional compilation in their projects to detect prerequisites (per-conditions) and subsequent conditions (post-conditions).

Per-conditions, a prerequisite, refers to the conditions that must be met in order to complete a work, and post-conditions, subsequent conditions, refers to the completion of a certain work will be achieved after the conditions. For example, a function that converts an object, which requires that the object cannot be empty, after conversion, the object must be plastic, then: Per-conditions is that the object cannot be empty, and post-conditions is the object for shaping. The example is not good, but we can understand these two concepts. )

You may write a private method to detect all classes and persistent objects. This method might be a conditional compilation block, which would make it valid only when Debug.

private void CheckState( )
{
// The Old way:
#if DEBUG
 Trace.WriteLine( "Entering CheckState for Person" );
 // Grab the name of the calling routine:
 string methodName =
  new StackTrace( ).GetFrame( 1 ).GetMethod( ).Name;
 Debug.Assert( _lastName != null,
  methodName,
  "Last Name cannot be null" );
 Debug.Assert( _lastName.Length > 0,
  methodName,
  "Last Name cannot be blank" );
 Debug.Assert( _firstName != null,
  methodName,
  "First Name cannot be null" );
 Debug.Assert( _firstName.Length > 0,
  methodName,
  "First Name cannot be blank" );
 Trace.WriteLine( "Exiting CheckState for Person" );
#endif
}

Using the #if and #endif compilation options (pragmas), you have compiled an empty method for your release. This checkstate () method is invoked in all versions (Debug and release). And in release it does nothing, but it's going to be called. So you still have to pay a small part of the cost of calling it routine.

In any case, the above practice works correctly, but it can cause a small bug that will only appear in release. Here's a common mistake that tells you what happens when you compile with a condition:

public void Func( )
{
 string msg = null;
#if DEBUG
 msg = GetDiagnostics( );
#endif
 Console.WriteLine( msg );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.