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