Code writing specification for C + + source code

Source: Internet
Author: User

Code writing for C + + source code 1. At the beginning of. cpp there should be a uniform description of the format, including the following:
A. File name (filename);
B. Brief description of the document function, purpose (Comment);
C. Founder (creater);
D. File creation time (date).
Cases:
FileName: ***.cpp//creator:piaocoder//Date: ****-**-**//Comment: * * *////////////////////////////////////////////


2. There should be an explanatory note for the main function function. The contents include: function, inlet/outlet parameters, notes or supplemental instructions if necessary, as well as explanatory notes for the main function code in the function. Comments are based on English.

3. The length of each line of code is recommended to be 80 columns, maximum no more than 120 columns, and the lines will be aligned.

Cases:
HANDLE csopenfile (const char cszfilename[],                  int nmode);
Or:
BOOL Csreadfile (HANDLE hfile,                void *pvbuffer,                int nreadsize,                int *pnreadsize                );


4. Loop, Branch code, judging conditions and execution code must not be on the same line.

Example: Correct:
if (n = =-2)    n = 1;else    n = 2;
Do not write:
if (n = =-2) n = 1;else n = 2;


5. The pointer definition, the * number can be either immediately following the type, or before the variable name.

Example: Can write: int* pnsize;

can also be written: int *pnsize;

But do not write: int * pnsize;


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


7. When the function entry parameter has a default value, the description should be commented.

Cases:
BOOL kssavetofile (const char cszfilename[],bool bcanreplace/* = TRUE */);
Or:
BOOL kssavetofile (const char cszfilename[],bool bcanreplace);//= TRUE


8. Else if must be written on one line.

9. Provisions relating to ' {', '} ':

9.1 ' {', '} ' should be exclusive on one line. There can be comments within the line.

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);p rintf ("%s\n", Pfilelines);}

9.2 ' {' must be on a different line, the code after ' {' must be indented one tab. ' {' and '} ' must be on the same column.

Example: Correct:
if (i > 0) {    m = 1;    n++;}
Do not write:
if (i > 0) {        m = 1;        n++;}
Exception:
if (i = = 0) {ASSERT (FALSE); return;}

9.3 In the loop, after the branch if there is only one line of code, although you can omit ' {', '} ', but this is not recommended. If the ellipsis may cause ambiguity, you must add ' {', '} '.

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. Provisions relating to spaces.

10.1 There must be spaces on both sides of all two-mesh and trinocular operators. There are no spaces at both ends of the monocular operator. However, there must be no spaces after the operators such as '-a ', ': ', '. ', ' [', '] ', and ' & ' (take address), ' * ' (value).

Example: Correct:
int n = 0, ntemp;for (int i = nminline; I <= nmaxline; i++) (condition)? Funct1 (): Func2 (); or    (condition)      ? Long statement      : another long statement;
Do not write:
int n=0, ntemp;for (int i=nminline; i<=nmaxline; i++)

10.2 For, while, if and other keywords should have 1 spaces, then the ' (', then no space; at the end of ') ' must not have a space.

Example: Correct:
if ( -2 = = N)
Do not write:
if ( -2 = = N)
Or
if (-2 = = N)

10.3 When calling functions, macros, there must be no spaces before or after ' (', ') '.

Example: Correct:
printf ("%d\n", NIndex);

Do not write:

printf ("%d\n", NIndex);
printf ("%d\n", NIndex);

10.4 type cast, no spaces before and after ' (') '

Example: Can write:
(ksfile*) PFile;
can also be written to do:
(Ksfile *) pFile
Do not write:
(ksfile*) PFile (Ksfile *) pFile

10.5 Space rules within the line of code:

(1) You need to add a space after the keyword

(2) cannot leave spaces after function name

(3) ' (' Back immediately, ') ', ', ', '; ' Move forward, followed by a blank space.

(4) ', ' After leaving a blank, if '; ' is not a line of Terminator and should then be left blank.

(5) The general two-dollar operator should be left blank before and after.

(6) The general unary operator before and after the space is not added.

(7) Such operators as "[]", ".", "and" are not preceded by spaces.


11. Provisions relating to indentation

11.1 indents are in Tab units. 1 Tabs for 4 spaces

11.2 The code indents one Tab in the following cases:

1. function body relative function name and ' {', '} '.

Cases:
int Power (int x) {return (x * x);}
2. Code after if, else, for, while, do, and so on.

3. Within one line, the code after the folding line should be folded in a reasonable position. If you have a +-*/equal operator, the operator should be at the end of the previous line and not at the beginning of the next line.

11.3 The following conditions do not have to be indented: Case after switch, default.

Cases:
Switch (NID) {case    Id_play: ... break;    Case Id_stop: ... break;    Default: ... break;

Code writing specification for C + + source code

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.