java编码规范_声明和语句

来源:互联网 发布:vwap 算法交易 编辑:程序博客网 时间:2024/05/18 01:34

1.       声明(Declarations)

5.1  每行声明变量的数量(Number Per Line)

推荐一行一个声明,因为这样以利于写注释。亦即,

int level; // indentation level

int size; // size of table

要优于,

int level, size;

不要将不同类型变量的声明放在同一行,例如:

int foo, fooarray[]; //WRONG!

注意:上面的例子中,在类型和标识符之间放了一个空格,另一种被允许的替代方式是使用制表符:

int         level; // indentation level

int         size; // size of table

Object currentEntry; // currently selected table entry

5.2  初始化(Initialization)

尽量在声明局部变量的同时初始化。唯一不这么做的理由是变量的初始值依赖于某些先前发生的计算。

5.3  布局(Placement)

只在代码块的开始处声明变量。(一个块是指任何被包含在大括号"{""}"中间的代码。)不要在首次用到该变量时才声明之。这会把注意力不集中的程序员搞糊涂,同时会妨碍代码在垓作用域内的可移植性。

void myMethod() {

int int1 = 0; // beginning of method block

if (condition) {

int int2 = 0; // beginning of "if" block

...

}

}

该规则的一个例外是for循环的索引变量

for (int i = 0; i < maxLoops; i++) { ... }

避免声明的局部变量覆盖上一级声明的变量。例如,不要在内部代码块中声明相同的变量名:

int count;

...

myMethod() {

if (condition) {

int count = 0; // AVOID!

...

}

...

}

5.4  类和接口的声明(Class and Interface Declarations)

当编写类和接口是,应该遵守以下格式规则:

·在方法名与其参数列表之前的左括号“(”间不要有空格。

·左大括号“{”位于声明语句同行的末尾。

·右大括号“}”另起一行,与相应的声明语句对齐,除非是一个空语句,“{”应紧跟在“{”之后。

class Sample extends Object {

int ivar1;

int ivar2;

Sample(int i, int j) {

ivar1 = i;

ivar2 = j;

}

 

int emptyMethod() {}

...

} //方法与方法之间以空行分隔。

}

 

2.       语句(Statements)

6.1  简单语句(Simple Statements)

每行至多包含一条语句,例如:

argv++; // Correct

argc--; // Correct

argv++; argc--; // AVOID!

6.2  复合语句(Compound Statements)

复合语句是包含在大括号中的语句序列,形如"{ 语句 }"。例如下面各段。

被括其中的语句应该较之复合语句缩进一个层次。

左大括号"{"应位于复合语句起始行的行尾;右大括号"}"应另起一行并与复合语句首行对齐。 大括号可以被用于所有语句,包括单个语句,只要这些语句是诸如if-elsefor控制结构的一部分。这样便于添加语句而无需担心由于忘了加括号而引入bug

6.3  返回语句(return Statements)

一个带返回值的return语句不使用小括号"()",除非它们以某种方式使返回值更为显见。例如:

return;

return myDisk.size();

return (size ? size : defaultSize);

6.4  ifif-elseif else-if else语句(if, if-else, if else-if else Statements)

if-else语句应该具有如下格式:

if (condition) {

statements;

}

if (condition) {

statements;

} else {

statements;

}

if (condition) {

statements;

} else if (condition) {

statements;

} else{

statements;

}

注意:if语句总是用"{""}"括起来,避免使用如下容易引起错误的格式:

if (condition) //AVOID! THIS OMITS THE BRACES {}!

statement;

6.5  for语句(for Statements)

一个for语句应该具有如下格式:

for (initialization; condition; update) {

statements;

}

一个空的for语句(所有工作都在初始化,条件判断,更新子句中完成)应该具有如下格式:

for (initialization; condition; update);

当在for语句的初始化或更新子句中使用逗号时,避免因使用三个以上变量,而导致复杂度提高。若需要,可以在for循环之前(为初始化子句)for循环末尾(为更新子句)使用单独的语句。

6.6  while语句(while Statements)

一个while语句应该具有如下格式

while (condition) {

statements;

}

一个空的while语句应该具有如下格式:

while (condition);

6.7  do-while语句(do-while Statements)

一个do-while语句应该具有如下格式:

do {

statements;

} while (condition);

6.8  switch语句(switch Statements)

一个switch语句应该具有如下格式:

switch (condition) {

case ABC:

statements;

/* falls through */

case DEF:

statements;

break;

case XYZ:

statements;

break;

default:

statements;

break;

}

每当一个case顺着往下执行时(因为没有break语句),通常应在break语句的位置添加注释。上面的示例代码中就包含注释/* falls through */

6.9  try-catch语句(try-catch Statements)

一个try-catch语句应该具有如下格式:

try {

statements;

} catch (ExceptionClass e) {

statements;

}

一个try-catch语句后面也可能跟着一个finally语句,不论try代码块是否顺利执行完,它都会被执行。

try {

statements;

} catch (ExceptionClass e) {

statements;

} finally {

statements;

}