"Go" writing high-quality Code 157 recommendations for improving C # Programs--Recommendation 134: conditionally using prefixes

Source: Internet
Author: User

Recommendation 134: Conditionally use prefixes

Prefixes are not recommended in the design specification for. Net. However, even Microsoft itself is still widely used in this prefix.

The most typical prefix is m_, which, on the one hand, takes into account the customary problems of historical evolution, and perhaps we do indeed need to do so.

In a type that is not very large, we really should not use any prefixes. Various design specifications also always suggest that we keep a petite type, but often backfired, large types often exist. In the case of task, it has more than 2000 lines of code. In this type, it is difficult to distinguish whether a type is an instance variable or a static variable, or a const variable, without using a prefix.

The most common practice is to:

The prefix m_, which indicates that this is an instance variable.

The predecessor S_, which indicates that this is a static variable.

Note that sometimes, if the type has only an instance variable or only a static variable, we also use the prefix directly to distinguish the variable from a local variable.

The const variable is often represented by a noun with an underscore, such as:

internal const int task_state_canceled=0x400000;

Remember, prefixes are limited to this, and other rules in the Hungarian nomenclature (such as prefixes with type names) are absolutely forbidden.

An example of using prefixes correctly is as follows:

    classSampleClass {Private Static intS_price; Private intM_price; Private Const intBased_price = +;  Public Static voidSetstaticfield (intPrice ) {S_price=Price ; }         Public voidSetclassfield (intPrice ) {M_price=Price ; }    }    classSampleClass2 {Private int_price;  Public voidSetprice (intPrice ) {_price=Price ; }    }

In this example, we know that it is necessary to use a prefix for an instance variable or a static variable, even if the type itself is not very long, but there is a duplicate of the method parameter and the type instance variable.

An example of an abusive prefix is as follows:

    class SampleClass    {        privateint  int_price;          Public void Setprice (int price )        {            = Price ;        }    }

In this example, the developer tries to specify a prefix int for price, trying to name the variable as int. However, this is not necessary for a strongly typed language.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

"Go" writing high-quality Code 157 recommendations for improving C # Programs--Recommendation 134: conditionally using prefixes

Related Article

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.