1. The program block should be written in indentation style, and the number of indented spaces is 4 .
2. Delimiters (such as curly braces ' { ' and ' } ' ) should each be on their own line and in the same column, and left-aligned with the statement that references them. At the beginning of the function body, the definition of the class and interface, and the program in if , for , do , while , switch , and case statements should be indented as above.
Example:
The following example does not meet the specification
for (...) { ... // program code }
if (...) void example_fun( void ) |
should be written as follows
for (...) if (...) void example_fun( void ) |
3. Longer statements, expressions or parameters ( >80 characters) should be written in multiple lines. Long expressions should be divided into new lines at low-priority operators. The operators should be placed at the beginning of the new lines. Lines should be properly indented so that the typesetting is neat and the statements are readable.
Example:
if (filename != null |
4. It is not allowed to write multiple short statements in one line, that is, write only one line per line
Example:
The following example does not meet the specification
LogFilename now = null; LogFilename that = null; |
should be written as follows
LogFilename now = null; |
5. If, for, do, while, case, switch, default and other statements have their own line, and the execution statements of if, for, do, while and other statements should be enclosed in brackets {} no matter how many . Example:
The following example does not meet the specification
if(writeToFile) writeFileThread.interrupt(); |
应如下书写:
If (writeToFile) { writeFileThread.interrupt(); } |
6.在两个以上的关键字、变量、常量进行对等操作时,它们之间的操作符之前、之后或者前后要加空格;进行非对等操作时,如果是关系密切的立即操作符(如.),后不应加空格。示例:
(1) 逗号、分号只在后面加空格。
int a, b, c; |
(2)比较操作符,赋值操作符"="、"+=", 算术操作符"+"、"%", 逻辑操作符"&&"、"&",位域操作符"<<"、"^"等双目操作符的前后加空格。
if (current_time >= MAX_TIME_VALUE) a = b + c; a *= 2; a = b ^ 2; |
(3)"!"、"~"、"++"、"--"、"&"( 地址运算符)等单目操作符前后不加空格。
flag = !isEmpty; // 非操作"!"与内容之间 i++; // "++","--"与内容之间 |
(4)"."前后不加空格。
p.id = pid; // "."前后不加空格 |
(5)if、for、while、switch等与后面的括号间应加空格,使if等关键字更为突出、明显。
|
7.类属性和类方法不要交叉放置,不同存取范围的属性或者方法也尽量不要交叉放置。
格式:
{ 类的公有属性定义 } |