High Quality C ++C Programming Guide (Chapter 3 Name Rules)

zhaozj2021-02-17  67

Chapter 3 Naming Rules

More famous naming rules When pushing Microsoft's "Hungary" method, the main idea of ​​this naming rule is "to add prefix in variables and function names to enhance people's understanding". For example, all character variables are prefixed in CH, and if the pointer variable is added, the prefix P is added. If a variable starts by the PPCH, it indicates a pointer to the character pointer.

The biggest disadvantage of "Hungary" law is cumbersome, for example

INT I, J, K;

Float X, Y, Z;

If the "Hungary" naming rules are used, they should be written.

INT II, ​​IJ, IK; // Prefix i Represents INT Type

FLOAT FX, FY, FZ; // Prefix F Represents Float Type

Such a cumbersome procedure will make the most programmers can't stand it.

According to investigation, there is no name rule that allows all programmers to agree, and the programming textbooks generally do not specify naming rules. Naming rules are not "success or failure" for software products. We don't have much effort to present the best naming rules in the world, but should develop a naming rule that makes most project members, and Implementation in the project.

3.1 Community Rules

The common rules discussed in this section are adopted by most programmers, and we should expand specific rules, such as 3.2.

l [Rules 3-1-1] The identifier should be intuitive and can be spent, and it is desirable to know that "decoding" is not required.

The identifier is preferably used in English words or its combination, which is easy to remember and read. Don't use Chinese Pinyin to name. The English words in the program generally don't be too complicated, and the words should be accurate. For example, don't write CurrentValue into NOWVALUE.

l [Rules 3-1-2] The length of the identifier should comply with the principle of "min-length".

A few decades ago, the old ANSI C specified that the name was not allowed to more than 6 characters. Today's C / C no longer restrictions. In general, long names can better express me, so the function name, variable name, and class names are not blame. So is the longer the name? Do not see! For example, the variable name MaxVal is easy to use than MaxValueuntiloverflow. The name of a single character is also useful, common as i, j, k, m, n, x, y, z, etc., which are usually used as partial variables within the function.

l [Rules 3-1-3] Naming rules should be consistent with the style of the operating system or development tool used.

For example, the identifier of the Windows application typically uses "case" mixing, such as addChild. The identifier of UNIX applications typically uses the "lowercase loopline" approach, such as add_child. Don't mix these two types of styles together.

l [Rules 3-1-4] Do not appear similar identifiers that only rely on case in cases.

E.g:

INT X, X; // Variable X and X easy confusion

Void foo (int x); // Function foo is easy to confuse with foo

Void foo (float x);

l [Rules 3-1-5] Do not appear on the identifier exactly the same local variables and global variables, although both the scope of the two do not have a grammatical error, but will misunderstand.

l [Rule 3-1-6] The name of the variable should use "noun" or "adjective noun".

E.g:

FLOAT VALUE;

Float oldValue;

Float newValue;

l [Rules 3-1-7] The name of the global function should use "verb" or "verb noun" (mobile phrase). The member function of the class should only use "verbs", and the noun omitted is the object itself.

E.g:

Drawbox (); // global function

Box-> DRAW (); // member function L [Rule 3-1-8] Named the correct antisense word group named mutually exclusive variables or function of the opposite action.

E.g:

Int minValue;

Int maxValue;

Int setValue (...);

Int getValue (...);

2 [Recommendations 3-1-1] Try to avoid digital numbers in the name, such as Value1, Value2, etc. unless the number is logically required. This is to prevent programmers from being lazy, refuse to name the brains, resulting in the meaningless name (because of the most expensive thing).

3.2 Simple Windows Application Naming Rules

The author has reasonably simplified the "Hungary" naming rules, and the following naming rules are simple and easy to use, compare the development of Windows applications.

l [Rule 3-2-1] The class name and function name are combined with the word starting with uppercase letters.

E.g:

Class node; // class name

Class LeafNode; // Classification

Void Draw (void); // Function Name

Void setValue (int value); // function name

l [Rule 3-2-2] variables and parameters are combined with a word starting with lowercase letters.

E.g:

Bool flag;

Int drawMode;

l [Rules 3-2-3] Constants use uppercase letters and segment words with underscore.

E.g:

Const int max = 100;

Const int max_length = 100;

l [Rule 3-2-4] Static variable add prefix S_ (represents static).

E.g:

void init (...)

{

Static int s_initvalue; // static variable

...

}

l [Rule 3-2-5] If a global variable is not required, the global variable is prefixed G_ (represents global).

E.g:

INT g_howmanypeople; // global variable

INT g_howmuchmoney; // global variable

l [Rule 3-2-6] The data member of the class is prefix M_ (represents member) so that the data member and the member function are the same name.

E.g:

Void Object :: setValue (int width, int hotht)

{

m_width = width;

m_height = height;

}

l [Rules 3-2-7] In order to prevent some identifiers in a single software library and other software libraries, it can be used to reflect the prefix of the software properties for various identifiers. For example, all library functions of the 3D graphic standard OpenGL are starting with GL, all constants (or macro definitions) start with GL.

3.3 Simple UNIX Application Nameout

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

New Post(0)