C ++ coding standards I follow is this "high-quality C ++ / C Programming Guide"
http://www.scholat.com/course/V_courseResourceDownload.html?courseId=1227&resourceId=12836
1. Structure of header files
In order to prevent the repeated header is referenced, should be produced by preprocessing block ifndef / define / endif structure.
With #include <filename.h> header file format to reference standard library (the compiler from the standard library directory to start the search).
With #include "filename.h" format to refer to a non-standard library header files (the compiler from the user's working directory to start the search).
Header files stored only "declaration" rather than store the "definition"
In the C ++ syntax, the class member functions can be defined at the same statement, and automatically inline functions. While this will bring the convenience of writing, but it resulted in inconsistent style, more harm than good. Suggested that the definition of the member function declarations separately, regardless of how small the function body.
Does not promote the use of global variables, as not a phenomenon in the header file extern int value of such statements.
2.1 blank line
Blank line functions as the partition of the program section. Blank lines decent (but more than nothing but less) layout will make the program more clearly. Blank lines do not waste memory, although the print program contains blank lines that will consume some paper, but it is worth. So do not be reluctant to use blank lines.
After each class declaration, function definition after the end of each blank line will be added.
A function in the body, without a blank line between the closely related bowed logic statements, should be added to other parts of the partition blank line.
// Blank line void Function1(…) { … } // Blank line void Function2(…) { … } // Blank line void Function3(…) { … }
|
// Blank line while (condition) { statement1; // Blank line if (condition) { statement2; } else { statement3; } // Blank line statement4; } |
2.2 lines of code
A line of code do one thing, just as the definition of a variable, or write only one statement. This code is easy to read and easy to write a comment.
if, for, while, do so on account statement from his party, the statement is executed not followed. Regardless of how much will be added to execute the statement {}. This prevents writing errors.
2.3 spaces within the lines
After the keywords you want to leave a space. After at least as const, virtual, inline, case and other keywords you want to leave a space, or can not discriminate keywords. As if, for, while after keywords such talk should leave a space left parenthesis '(', to highlight keywords.
Do not leave a space after the function name, followed by the left parenthesis '(' to distinguish keywords.
'(' Followed by back, ')', ',', ';' forward followed by, followed at no spaces.
',' Then to leave a space, such as Function (X, Y, Z) . If ';' symbol is not the end of a line, thereafter to leave a space, such as for (initialization; condition; update) .
Assignment operators, comparison operators, arithmetic operators, logic operators, bit field operators, such as "=", "+ =", "> =", "<=", "+", "*", "% "," before and after && "," || "," << "," ^ " operator should dihydric spaces.
The unary operator "-", "+", "!", "-", "&" (address operator) without spaces before and after.
As " [] ", ".", "->" no space before and after such operator.
For expression longer for the statement and the if statement, for compactness reasons may suitably remove some spaces, such as for (i = 0; i <10; i ++), and if ((a <= b) && (c <= d ))
void Func1 (int x, int y, int z); // good style void Func1 (int x, int y, int z); // bad style |
if (year> = 2000) // good style if (year> = 2000) // bad style if ((a> = b) && (c <= d)) // good style if (a> = b && c <= d) // bad style |
for (i = 0; i <10; i ++) // good style for (i = 0; i <10; i ++) // bad style for (i = 0; I <10; i ++) // excessive space |
x = a <b a: b; // good style? x = a <b a:? b; // bad style |
int * x = & y; // good style int * x = & y; // bad style |
array [5] = 0; // Do not write array [5] = 0; a.Function (); // instead of writing a Function ().; b->Function(); // 不要写成 b -> Function();
|
2.4 对齐
程序的分界符‘{’和‘}’应独占一行并且位于同一列,同时与引用它们的语句左对齐。
{ }之内的代码块在‘{’右边数格处左对齐。
void Function(int x) { … // program code } |
void Function(int x){ … // program code }
|
if (condition) { … // program code } else { … // program code } |
if (condition){ … // program code } else { … // program code } |
for (initialization; condition; update) { … // program code } |
for (initialization; condition; update){ … // program code } |
While (condition) { … // program code } |
while (condition){ … // program code } |
如果出现嵌套的{},则使用缩进对齐,如: { … { … } … } |
|
风格良好的对齐 风格不良的对齐
这边书有详细全面的代码规范讲解,有助于我们养成良好的代码习惯,在这里不再一一举例;
《数学之美》读后感