5 缩进
一种改进代码可读性的原则是对一组的statement进行分块,并且对每一块进行缩进设置。
如果你的代码是用Java的开发环境的话,那么就使用开发环境的默认缩进,如果不是的话,那么使用4个空格保证可读性,不是使用太多的空格。
public class MyClass {
void function(int arg){
if (arg < 0) {
for (int index = 0; index <= arg; index++) {
// ......
}
}
}
}
让每个块的 { 操作符在块开始的那一行的最后一个字符的位置上,并且把结束操作符 } 放在单独的一行上,并且与块开始的第一个字符相对应。具体参照如下的例子:
类定义
public class MyClass {
………..
}
内部类定义
public class MyClass {
………
class InnerClass{
………..
}
}
方法定义
void function(int arg){
}
静态块
static {
}
for循环
for (int index = 0; index <= arg; index++) {
// ......
}
if else块
if (j < 0) {
.....
} else if (j > 0) {
.....
} else {
}
try,catch 和finally块
try{
...
} catch (Exception e){
...
} finally {
....
}
switch 块
switch (value) {
case 0:
...
break;
default:
...
break;
}
匿名内部类
button.addActionListener(
new ActionEventListener() {
public void actionPerformed() {
...
}
}
)
while 块
while (++k <= j) {
...
}
do-while块
do{
...
} while (++k <= j);
如果你正在管理一个团队,不要让自己的组员随意使用各自的风格,建立一个大家都要遵守标准,保证每个人的风格都符合这个标准。
6 折叠长的行
一个标准的基于窗体的编辑器可以用横的滚动条来控制长的代码,但是打印机却必须要折行,或者在不同的sheet上来打印长的行,为了确保代码打印出来也具有可读性,需要对代码的长度进行控制。一般来说是80或者132个字符。
首先,不要多个操作放在一行上,尽管行的最大字符数允许,也不要这么做。如果两个操作放在一行上:
double x = Math.random(); double y = Math.random(); // Too long
看起来很乱,需要用一个新行来分割另外的一个操作
double x = Math.random();
double y = Math.random();
其次,如果一个行太长的话,那么它肯定包含了复杂的操作
double length = Math.sqrt(Math.pow(Math.random(),2.0) + Math.pow(Math.random(),2.0)); // Too long
把这个复杂的操作,分割成小的,独立的操作,利用单独的行,把每行的结果保存到一个临时变量里面。
double xSquared = Math.pow(Math.random(),2.0);
double ySquared = Math.pow(Math.random(),2.0);
double length = Math.sqrt(xSquared + ySquared);
最后,如果一个长的行,实在无法分割成单独的操作的话,那么用下面的原则进行折行,缩进
第一步
如果一个行包含一个或者多个逗号
double length = Math.sqrt(Math.pow(x,2.0),Math.pow(y,2.0));
以逗号为分割点引入新的行,并且对其每个操作的头一个字符
double length = Math.sqrt(Math.pow(x,2.0),
Math.pow(y,2.0));
第二步
如果一个长的操作没有逗号
public class MyClass {
private int field;
boolean equals(Object obj) {
return this == obj || (obj instanceof MyClass && this.field == ((MyClass)obj).field); }
}
以最低优先级的操作符为基准进行分割,引入新的行,并且对齐
public class MyClass {
private int field;
boolean equals(Object obj) {
return this == obj
|| (obj instanceof MyClass
&& this.field == ((MyClass)obj).field);
}
}
第三步
重复第一步和第二步,直到符合标准。
7 包含空白
空白是代码不可缺少的,只有很少空白的代码是非常难与阅读的和理解的。所以使用廉价的,大量的空白在方法,注释,代码块,和操作中,使它们容易理解。
使用单独的空格进行分割
一个右括号 ) ,或者大括号 } 和任何的关键字之间 ,一个关键字和任何的左括号 ( 或者 大括号 { 之间都应该插入一个空格
for_(..)_{
...
}
while_(...)_{
}
do_{
...
}_while_(...);
switch_(...)_{
...
}
if_(...)_{
...
}_else_if_(...)_{
...
}_else_{
...
}
try_{
...
}_catch_(...)_{
}_finally_{
}
任何的二元操作符,除了”.”操作
double length_=_Math.sqrt(x_*_x_+_y_*_y);
使用空白行
方法中的每一个逻辑块
void handleMessage(Message message) {
DataInput content = message.getDataInput();
int messageType = content.readInt();
switch (messageType) {
case WARNING:
...
break;
case ERROR:
...
break;
default:
...
break;
}
}
类(接口)中每个成员定义
public class MyClass {
/**
* Define an inner class
*/
class InnerClass{
}
/**
* The Bar associated with this class.
*/
private Bar bar;
/**
* Construct a Class whit the specified Bar
*/
MyClass(Bar bar){
this.bar = bar;
}
}
在一个文件中定一个每一个Class和interface
/**
* file description
*/
package com.code;
/**
* interface description
*/
interface FooInterface{
}
/**
* class description
*/
public class Foo implements FooInterface{
}