Naming rules for C + + code specifications _c Language

Source: Internet
Author: User
Tags constant lowercase naming convention

First, two basic principles of nomenclature
1, the meaning is clear, not easy to confuse;
2, do not and other modules, the System API namespace conflict.

Ii. general rules of nomenclature
1. In all nomenclature, the standard English words or abbreviations shall be used; no phonetic or phonetic abbreviations shall be used unless the name describes the text
Specific content (such as Half-width, Full-width, initials, finals, etc.); Do not use numbers or strange characters to define identifiers unless necessary.
2, all names should follow the principle of meaning, that is, the name should be clear and clear.
3, all names are not easy too long, should be controlled within the maximum length of the specified.
4, all names should use the full name as far as possible.
5, the naming if the use of special conventions or abbreviations, you need to have a note.
6, their own unique naming style, to keep consistent throughout, not to change back and forth.

Description:
The personal naming style is available only if it conforms to the naming rules of the project group or product group in which it is located. (That is, where there is no rule in the naming convention, you can have a personal naming style).

7. In the same software product, should plan the interface part identifier (variable, structure, function and constant) naming, prevents the compilation, the link when produces the conflict.
8, for variable naming, prohibit a single character (such as I, J, K ...). ), it is suggested that in addition to the specific meaning, but also to indicate its variable type, data type, etc., but I, J, K as a local loop variable is allowed.

Description:
variables, especially local variables, if expressed in a single character, can easily be mistaken (as I write j), and cannot be checked out at compile time, and it is possible to spend a lot of time searching for this small error.
Example: the definition of local variable name as shown below can be used for reference.
int Liv_width
the variable name is interpreted as follows:
L local variable (other: G global variable (global) ... )
I data type (Interger)
V Variable (Variable) (Other: C constant (Const) ... )
Width variable Meaning
This prevents local variables from being the same as global variables.

9. The naming specification must be consistent with the system style used, and unified in the same project, such as the full lowercase of Unix with an underlined style or mixed-case, do not use the case and underline the way, as a special identity such as identifying member variables or global variables m_ and g_, The combination of the case and the casing is allowed in the following way.
Example: Add_user not Allowed, Add_user, AddUser, M_adduser allowed.
10, with the correct set of antonyms named mutually exclusive variables or functions of the opposite action.
Description: Here are some of the antonyms groups commonly used in software.

Copy Code code as follows:

Add/remove begin/end Create/destroy Insert/delete
First/last get/releaseincrement/decrement Put/get
Add/delete Lock/unlockopen/closemin/max
Old/newstart/stopnext/previoussource/target
Show/hide send/receive Up/downcut/paste

Cases:
Copy Code code as follows:

int min_sum;
int max_sum;
int Add_user (BYTE *user_name);
int Delete_user (BYTE *user_name);

11, in addition to compiling switch/header files and other special applications, should avoid the use of _example_test_ such as the beginning and end of the definition of underscore.
12, in the program does not appear the identifier exactly the same local variables and global variables, although the scope of the two is not a grammatical error, but it will make people misunderstand.

third, the specific norms
1. Project name
Do not force uniformity.

2, File name:
Based on the project name, the first 3 letters should indicate which project is relevant.
The following letters should be able to distinguish between different functions.
is case-insensitive.
Length is not limited to 8.3 format and is not recommended for more than 30 characters.
If the file is used to define and implement classes, the recommended file name is consistent with the class name.

3, Function name:
Refer to the naming conventions for Windows APIs.
The use of movable object structure is recommended. Function names should clearly reflect the function and purpose of the function.
The maximum number of function names must not exceed 30 characters.
The first letter of a function name must be capitalized.
Global functions must begin with the lowercase prefix "g". The name of the global function should use "verb" or "verb + noun" (moving object phrase). A member function of a class should use only "verbs", and the omitted noun is the object itself.
Cases:
Drawbox (); Global functions
Box->draw (); member functions of a class

4, variable name:
In principle, the naming of variable names complies with the Hungarian notation. That is: prefix + type + variable name
Variable names must be no more than 20 characters long.
The name of the variable should use "noun" or "adjective + noun".
Cases:
float value;
float OldValue;
1) format: [M_|s_|g_] Type [class name|struct name] variable name
2) Explanation:
M_: Member Variable of class
Ms_: Static member Variable for class
S_: Static global variables
G_: Normal global variable
Types abbreviation (type)
Char, Tchar:ch
Char[],tchar[]: SZ
BOOL, BOOL:B
int, __int16,__int32,__int64:n
Long:l
Double:d
Float:ft
Byte:by
Word:w
Dword:dw
Unsigned:u
Function:fn
P:pointer
Lp:pointer
5, class name:
Must begin with the uppercase "K", followed by the letter to reflect the specific meaning, with a clear expression of the purpose and function of the class principle.
The interface must begin with an uppercase "I", representing Interface.
When a name is composed of multiple words, the first letter of each word must be capitalized.

6, structure name, macro name, enumeration name, Union name:
All caps.
Enum name plus lowercase prefix "enum".
Cases:

Copy Code code as follows:

typedef enum _KFILE_OPEN_MODE
{
enumopen_readonly = 0;
Enumopen_readwrite = 1;
Enumcreate_alway = 3;
} Kfile_open_mode;
Macro name plus lowercase prefix "def".

Example:
#define DEFMAXNUMBER 100
The structure name is prefixed with the lowercase prefix "tag" and must begin with the uppercase "K".
Example:
Copy Code code as follows:

typedef struct TAGKPOINT
{
int x;
int y;
} Kpoint;

Union name plus lowercase prefix "uni".
Example:
Copy Code code as follows:

typedef Union _variant
{
Char Unichval;
int uninval;
Long Unilval;
float Uniftval;
} VARIANT;

Description:There should be more stringent restrictions on the identifiers of the interface parts to prevent conflicts. If you can specify the interface part of the variables and constants preceded by the "module" logo and so on.
Description:You should make the necessary notes about the abbreviations or conventions used in the file, especially the special abbreviations, at the beginning of the source file.
Description:Shorter words can be shortened by removing "vowels", and longer words are abbreviated to the first few letters of the word.
Some words have a recognized abbreviation.
Example: Temp can be abbreviated to TMP
Copy Code code as follows:

Flag can be abbreviated to FLG
Statistic can be abbreviated to stat
Increment can be abbreviated as INC
Message can be abbreviated as MSG

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.