C++ variable naming rules

1. The overall principle of the order

1. Identity

When writing a submodule or derived class, it is necessary to follow the command style of its base class or the overall module, and keep the same command style in the overall module.

2. Composition of identifiers - words

Identifiers use English words or their combinations, which should be intuitive and spellable, so that the text can be understood, and the words should be accurate.

3. Minimize the length && maximize the amount of information principle

While maintaining the unambiguous meaning of an identifier, its length should be kept as short as possible.

4. Avoid being too similar

Do not appear next to case-sensitive identifiers, such as "i" and "I", "function" and "Function", etc.

5. Avoid duplicate names at different levels of scope

Local variables and global variables with the same name should not appear in the program. Although the scope of the two is different and no grammatical errors will occur, it is easy to misunderstand.

6. Properly name identifiers that have mutually exclusive meanings

Use the correct antonyms to name mutually exclusive identifiers, such as: "nMinValue" and "nMaxValue", "GetName()" and "SetName()"

7. Avoid numbers in names

Try to avoid numbers with numbers in the name, such as Value0, Value1, Value2, etc., unless the number is really required logically. This is to prevent programmers from being lazy and refusing to use their brains for naming, which leads to meaningless names (because numbers are the most convenient).

2. Class/structure naming

除了异常类等个别情况(不希望用户把类看作一个普通的、正常类的情况)外,C++类结构的命名一般应该遵循以下准则
1. Naming of C++ classes/structures

Class names always start with a capital letter "C" followed by one or more words. For ease of definition, the first letter of each word needs to be capitalized.

2. Recommended organizational form

It is recommended to use the form of "noun" or "adjective + noun" for class naming, for example: "CAnalyzer", "CFVecteor" ...

Third, the naming of C language structures

1. Traditional C naming rules

The names of traditional C structures are all composed of uppercase letters, and words are delimited by underscores, for example: "SERVICE_STATUS", "DRIVER_INFO"...

4. Function naming

1. Function naming

Function names consist of one or more words. For ease of definition, capitalize the first letter of each word.

Recommended organizational form The function name should use the form of "verb" or "verb+noun" (verb-object phrase). For example: "GetName()", "SetName()", "Erase()", "Reserve()"...

2. Protect member functions

An underscore "_" should be added at the beginning of the protected member function to distinguish, for example "_SetState()"...

3. Private member functions

Similarly, private member functions should start with two underscores "__", such as "__DestroyImp()"...

Virtual functions Virtual functions are used to start with "Do", such as: "DoRefresh()", "_DoEncryption()"...

4. Callback and event handler function

Callbacks and event handlers are customary to start with the word "On". For example: "_OnTimer()", "OnExit()"

5. Variable naming

Variables should be the most used identifiers in programs, and variable naming conventions may be the most important part of a set of C++ naming guidelines.

1. Variable naming

A variable name consists of a prefix + type suffix + one or more words. For ease of definition, the first letter of each word is capitalized. For some simple and clear local variables, you can also use simplified methods, such as: i, j, k, x, y, z...

2. Scope prefix

The scope prefix indicates the visible scope of a variable. Scopes can be of the following types:

Prefix Description

no local variables

Member variable of m_ class (member)

Static member variables of the sm_ class (static member)

s_ static variable (static)

g_ external global variable (global)

sg_ static global variable (static global)

sg_ static global variable (static global)

gg_ Data segment global variables shared between processes (global global)

Global variables should be used sparingly unless absolutely necessary.

3. Type of suffix

Prefix Description

n Integer and bit field variables (number)

e enumeration variable (enumeration)

c character variable (char)

b Boolean variable (bool)

f floating point variable (float)

p pointer variables and iterators (pointer)

pfn is especially for pointing to function pointer variables and function pointers (pointer of function)

g array (grid)

The instance of class i can also define some special prefixes for frequently used classes, such as: the prefix of std::string and std::wstring can be defined as "st", the prefix of std::vector can be defined as "v", etc.

Type suffixes can be combined, such as "gc" for character arrays, "ppn" for pointers to pointers to integers, and so on.

4. Recommended Composition Form

Variable names should use "noun" or "adjective + noun". For example: "nCode", "m_nState", "nMaxWidth"...

6. Constant naming

C++ introduces support for constants. The naming rules of constants are as follows:

1. Naming rules for constants

The constant name is composed of type prefix + all uppercase letters, and the words are delimited by underscores, such as cDELIMITER, nMAX_BUFFER... The definition of the type prefix is ​​the same as that in the variable naming rules.

Seven, enumeration, union, typedef

Names of enumerations, associations, and typdefs The type names generated by enumerations, associations, and typedef statements are all capital letters, and words are delimited by underscores, such as: FAR_PROC, ERROR_TYPE...

Eight, macro, enumeration value

Macros, named macros of enumeration values, and enumeration values ​​are composed of all uppercase letters, and words are delimited by underscores, such as: ERROR_UNKNOWN, OP_STOP...

Nine, function name modification

A function's Decorated Name is a string created by the compiler during compilation. Used to indicate the definition or prototype of a function. LINK programs or other tools sometimes need to specify the name decoration of the function to locate the correct location of the function.

Another situation where you need to specify the name decoration of the function is to call a C or C++ function in the assembler.

Ten, C compiler function name modification rules

For the __stdcall calling convention, the compiler and linker will prefix the output function name with an underscore, followed by an "@" symbol and the number of bytes of its argument. For example _functionname@number. The __cdecl calling convention simply prefixes output function names with an underscore. Such as _functionname. The __fastcall calling convention adds an "@" sign before the output function name. It is also followed by a "@" symbol and the number of bytes of its parameters, such as @functionname@numbe

Eleven, C++ compiler function name modification rules

C++'s function name decoration rules are somewhat complicated. But the information is more sufficient. By analyzing the modified name, you can not only know the calling method of the function. The return value type, the number of parameters and even the parameter type. Regardless of the __cdecl, __fastcall or __stdcall calling method, the function decoration starts with a "?", followed by the name of the function. Then there is the start mark of the parameter list and the parameter list spelled out according to the parameter type code.

For the __stdcall method, the start identifier of the parameter table is "@@YG", and for the __cdecl method it is "@@YA". For the __fastcall method, it is "@@YI". The spelling code of the parameter table is as shown below:

X--void

D--char

E--unsigned char

F--short

H--int

I--unsigned int

J--long

K--unsigned long(DWORD)

M--float

N--double

_N--bool

U--struct

....

There is something special about pointers. Use PA to represent the pointer, and PB to represent the pointer of const type.

The following code indicates the pointer type. Assuming that pointers of the same type appear continuously, they are replaced by "0", and a "0" represents a repetition. U indicates the structure type. Usually followed by the type name of the structure, use "@@" to indicate the end of the structure type name. The return value of the function is not treated specially, and its description is the same as the function parameter. Immediately following the start flag of the parameter table, ie. The first item in the function parameter table actually represents the return value type of the function. After the parameter list, "@Z" marks the end of the entire name. Assuming that the function has no parameters, it ends with a "Z" mark.

Note:

  • 1.__stdcall: Start with "?" to identify the function name. followed by the function name. The function name is followed by "@@YG" to identify the beginning of the parameter list, followed by the parameter list.
  • 2 __cdecl calling convention: The rules are the same as the above _stdcall calling convention, except that the start identifier of the parameter table is changed from the above "@@YG" to "@@YA"
  • 3 __fastcall calling convention: The rules are the same as the above _stdcall calling convention, except that the start identifier of the parameter table is changed from the above "@@YG" to "@@YI".
  • VC++'s default declaration for functions is "__cedcl", which can only be called by C/C++.

Guess you like

Origin blog.csdn.net/cyy1104/article/details/129815199