C ++ naming convention

Source: Internet
Author: User
Tags uppercase letter

General rules:
1. All names should use standard English words or abbreviations. Do not use Pinyin or pinyin abbreviations, unless the name describes the unique content of Chinese characters, such as halfwidth, fullwidth, initials, and vowels.

2. All naming rules should follow the concept principle, that is, the name should be clear and clear.

3. All names are not too long and should be controlled within the specified maximum length.

4. Full names should be used whenever possible.

5. If the name is abbreviated, the abbreviation in the general abbreviation table (see appendix) should be used. In principle, abbreviations other than the general abbreviation table are not recommended, it must be commented out and described.

Specific specifications:

1. Project name:

Not forced unification.

2. File Name:

· Based on the project name, the first three letters should indicate which project is related.

· The following letters should be able to differentiate different functions.

· Case insensitive.

· The length is not limited to 8.3. It is recommended that the length be no more than 30 characters.

· If the file is used to define and implement classes, it is recommended that the file name be consistent with the class name.

3. Function Name:

· Refer to the naming rules for Windows APIs.

· The dynamic object structure is recommended. The function name should clearly reflect the function and purpose.

· The function name cannot exceed 30 characters.

· The first letter of the function name must be in uppercase.

· Global functions must start with the lowercase prefix "G.

4. variable name:

In principle, the name of a variable follows the Hungarian notation. That is, prefix + Type + variable name

1) format:

[M _ | S _ | G _] type [class name | struct name] variable name

2) Explanation:

· M _: member variable of the class

· Ms _: static member variable of the class

· S _: static global variable

· G _: common global variables

· Type)

· Char, tchar: CH

· Char [], tchar []: SZ

· Bool, bool: B

· Int, _ int16 ,__ int32 ,__ int64: N

· Long: l

· Double: d

· Float: ft

· Byte:

· Word: W

· DWORD: DW

· Unsigned: u

· Function: FN

· P: pointer

· LP: pointer

The variable name cannot exceed 20 characters.

5. Class Name:

· You must start with an uppercase letter "K" and then use letters to clearly express the usage and functions of the class.

· The interface must start with "I" and represent the interface.

· When a name consists of multiple words, the first letter of each word must be capitalized.

6. Structure name, macro name, enumeration name, and Union Name:

· All are capitalized.

· The enumeration name must be prefixed with "Enum" in lower case ".

Example:

Typedef Enum _ kfile_open_mode

{

Enumopen_readonly = 0,

Enumopen_readwrite = 1,

Enumcreate_alway = 3

} Kfile_open_mode;

// Add the macro name with the lowercase prefix "def ".

Example:

# Define defmaxnumber 100

· The structure name must be prefixed with "tag" in lower case and must start with "K" in upper case.

Example:

Typedef struct tagkpoint

{

Int X;

Int y;

} Kpoint;

// Add the Union name with the lowercase prefix "uni ".

Example:

Typedef union _ variant {

Char unichval;

Int uninval;

Long unilval;

Float unitval;

...

} Variant;

C/C ++Source codeWriting specifications (Trial)

1. At the beginning of. h/. cpp, there should be a unified description of the format, including:

A. File Name (filename );

B. creater );

C. File Creation Time (date );

D. Briefly describe the functions and purposes of the file (comment ).

2. Unless it is extremely simple, the function should be annotated. The content includes: function, entry/exit parameters, and remarks or additional instructions if necessary.

3. Each lineCodeIt is recommended that the length of the column be 80, and the maximum length should not exceed 120 columns. The line is aligned.

Example: handle ksopenfile (const char cszfilename [],

Int nmode );

Or:

Bool ksreadfile (

Handle hfile,

Void * pvbuffer,

Int nreadsize,

Int * pnreadsize

);

4. Loop and branch code. The judgment conditions and Execution Code cannot be on the same line.

For example: Correct:

If (n =-2)

N = 1;

Else

N = 2;

Do not write:

If (n =-2) n = 1;

Else n = 2;

5. pointer definition. * can be followed by the type or before the variable name.

Example: writable: int * pnsize;

It can also be written as: int * pnsize;

But cannot be written as: int * pnsize;

6. When calling a non-member function in a class member function, you must add ":" Before the non-member function name.

7. If the function entry parameters have default values, comment them out.

Example:

Bool kssavetofile (

Const char cszfilename [],

Bool bcanreplace/* = true */

);

Or:

Bool kssavetofile (

Const char cszfilename [],

Bool bcanreplace // = true

);

8. Else if must be written in one line.

9. Rules related:

9.1 '{', '}' should have an exclusive row. This row can contain comments.

For example: Correct:

For (I = 0; I <cbline; I ++)

{//.....

Printf ("line % d:", I );

Printf ("% s \ n", pfilelines );

}

Do not write:

For (I = 0; I <CB; I ++)

{Printf ("line % d:", I );

Printf ("% s \ n", pfilelines );

}

9.2 '{' must start with another line, and the code after '{' must be indented to a tab. '{' And '}' must be in the same column.

For example: Correct:

If (I> 0)

{

M = 1;

N ++;

}

Do not write:

If (I> 0 ){

M = 1;

N ++;

}

Exceptions:

If (I = 0)

{Assert (false); return ;}

9.3 If there is only one line of code after loops and branches, although '{' and '}' can be omitted, This is not recommended. If this keyword is omitted, '{', '}' must be added '{','}'.

For example: Correct:

If (n =-2)

N = 1;

Else

N = 2;

Or:

If (n =-2)

{N = 1 ;}

Else if (n! = Ntemp)

{N = 2 ;}

Else

{N = 3 ;}

Do not write:

If (n =-2)

N = 1;

Else if (n! = Ntemp)

N = 2;

Else

N = 3;

Not recommended:

If (I <1)

{N = 1 ;}

Else

{

If (I = 1)

{N = 2 ;}

Else

{

If (I> 1)

{N = 3 ;}

}

}

10. Space-related provisions.

10.1 there must be spaces on both sides of all the two-object and three-object operators. Spaces are not required at both ends of the single-object operator. But in '-> ','::','. ',' [','] 'and other operators, and' & '(take address),' * '(value) and other operators must not have spaces.

For example: Correct:

Int n = 0, ntemp;

For (INT I = nminline; I <= nmaxline; I ++)

Do not write:

Int n = 0, ntemp;

For (INT I = nminline; I <= nmaxline; I ++)

10.2 For, while, if, and other keywords should be followed by a space, followed by '(', followed by no space; 'At the end.

For example: Correct:

If (-2 = N)

Do not write:

If (-2 = N)

Or

If (-2 = N)

And so on.

10.3 when a function or macro is called, no spaces are allowed before or after.

For example: Correct:

Printf ("% d \ n", nindex );

Do not write:

Printf ("% d \ n", nindex );

Printf ("% d \ n", nindex );

And so on.

10.4 when the type is forcibly converted, '('')' must have no spaces before and after

Example: writable:

(Ksfile *) pfile;

It can also be written as follows:

(Ksfile *) pfile

Do not write:

(Ksfile *) pfile

(Ksfile *) pfile

11. indentation-related provisions

11.1 indent is in the unit of tab. One tab contains four spaces.

11.2 In the following cases, the Code indent a tab:

1. Relative function name and '{', '}' of the function body '{','}'.

Example:

Int power (int x)

{

Return (x * X );

}

2. Code after if, else, for, while, and do.

3. The code after the lines cannot be written within one line. The lines should be broken at a reasonable position. If there are +-*/and other operators, the operators should be at the end of the previous line, rather than at the beginning of the next line.

11.3 In the following cases, do not indent: Case and default after switch.

Example:

Switch (NID)

{

Case id_play:

......

Break;

Case id_stop:

......

Break;

 

Default:

......

Break;

}

Related Article

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.