Chapter 1 naming rules </P> <p> the famous naming rules are promoted by Microsoft's "Hungary" method, the main idea of this naming rule is to "add a prefix to the variables and function names to enhanceProgram". For example, all character variables are prefixed with CH, and if it is a pointer variable, the prefix P is appended. If a variable starts with PPCH, it indicates that it is a pointer to a character pointer. </P> <p> the biggest disadvantage of the "Hungary" method is that it is cumbersome, for example
Int I, J, K;
Float x, y, z;
If the "Hungary" Naming rule is used, it should be written as: </P> <p> int II, IJ, ik; // prefix I indicates int float FX, fy, Fz; // prefix F indicates the float type </P> <p> so cumbersome programs will make the vast majority of programmers intolerable. According to the investigation, there is no naming rule that can be approved by all programmers. Generally, No naming rule is specified in the programming textbooks. Naming rules are not a "success or failure" thing for software products. We should not try to invent the best names in the world with too much energy.
Rules, but a naming rule should be formulated to satisfy most project members and implemented in the project. </P> <p> 3.1 Common rules the common rules discussed in this section are adopted by most programmers. We should expand the specific rules while following these common rules, for example, section 3.2.
[Rule 3-1-1] The identifier should be intuitive and readable. It is expected to be informative and does not need to be decoded ".
It is recommended that identifiers use English words or their combinations to facilitate memory and reading. Do not use Chinese pinyin for naming. The English words in the program are generally not too complex and should be accurate. For example, do not write currentvalue as nowvalue.
[Rule 3-1-2] The length of the identifier must comply with the "Min-length & Max-Information" principle.
A few decades ago, ansi c specified that the name should not exceed 6 characters. Today's c ++/C does not have this limit. In general, long names can better express meanings. Therefore, function names, variable names, and class names can contain up to a dozen characters. So is the name a longer appointment? No! For example, the variable name maxval maxvalueuntiloverflow is easy to use. Single-character names are also useful. Common ones include I, J, K, M, N, x, y, and z. They are usually used as local variables in functions.
[Rules 3-1-3] naming Rules should be consistent with the operating system or development tools used.
For example, the identifiers of Windows applications are usually in a case-insensitive manner, such as addchild. The identifier of a Unix application is usually "lowercase and underlined", for example, add_child. Do not mix these two types of styles.
Rules 3-1-4 do not contain similar identifiers that are case sensitive.
For example:
Int X, X; // variable X and X are easy to confuse </P> <p> void Foo (int x); // The Foo and foo functions are easy to confuse.
Void Foo (float X); </P> <p>
[Rule 3-1-5] do not show local variables and global variables with identical identifiers in the program. Although the scopes of the two are different and there is no syntax error, it may be misunderstood. </P> <p> [Rule 3-1-6] The variable name should use a "noun" or "Adjective + noun ".
For example:
Float value;
Float oldvalue;
Float newvalue;
[Rule 3-1-7] The name of a global function should be "verb" or "Verb + noun ). Class member functions should only use the "verb", and the omitted nouns are the object itself.
For example:
Drawbox (); // global function
Box-> draw (); // member functions of the class
Rules 3-1-8: Use a correct antgroup to name variables with mutex meanings or functions with opposite actions.
For example:
Int minvalue;
Int maxvalue;
Int setvalue (...);
Int getvalue (...);
[3-1-1] avoid numbers in the name, such as value1 and value2, unless a number is required logically. This is to prevent programmers from being lazy and refuse to name their brains, resulting in meaningless names (because numbers are the most convenient ). </P> <p> 3.2 simple naming rules for Windows Applications
The authors have simplified the naming rules of Hungary. The following naming rules are simple and easy to use, and are suitable for the development of Windows application software.
Rule 3-2-1: The class name and function name are combined with words starting with an upper-case letter.
For example:
Class node; // Class Name
Class leafnode; // Class Name
Void draw (void); // function name
Void setvalue (INT value); // function name </P> <p> L
Rule 3-2-2: a combination of variables and parameters starting with a lowercase letter.
For example:
Bool flag;
Int drawmode; </P> <p>
[Rule 3-2-3] constants are all separated by uppercase letters and underscores.
For example:
Const int max = 100; </P> <p> const int max_length = 100; </P> <p>
[Rule 3-2-4] Static variables are prefixed with S _ (indicating static ).
For example:
Void Init (...)
{
Static int s_initvalue; // static variable
...
} </P> <p>
[Rule 3-2-5] If you have to require a global variable, add the global variable with the prefix G _ (indicating global ).
For example:
Int g_howmanypeople; // global variable
Int g_howmuchmoney; // global variable </P> <p>
Rules 3-2-6: The data member of the class is prefixed with m_( indicating Member). This prevents the data member from having the same name as the parameter of the member function.
For example:
Void object: setvalue (INT width, int height)
{
M_width = width;
M_height = height;
} </P> <p>
[Rules 3-2-7] to prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software for various identifiers. For example, all library functions of 3D graphics standard OpenGL start with GL, and all constants (or macro definitions) start with GL.