1、判断一个集合是否有值的时候,用isEmpty而不用size,两者主要是返回类型的不同
//Noncompliant Code Example
if (myCollection.size() == 0) { // Noncompliant
/* ... */
}
//Compliant Solution
if (myCollection.isEmpty()) {
/* ... */
}
2、不要直接在catch块中throw捕获的异常,因为这样毫无意义
//Noncompliant Code Example
public String readFile(File f) {
StringBuilder sb = new StringBuilder();
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
//...
}
catch (IOException e) { // Noncompliant
throw e;
}
return sb.toString();
}
//Compliant Solution
public String readFile(File f) {
StringBuilder sb = new StringBuilder();
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
//...
}
catch (IOException e) {
logger.LogError(e);
throw e;
}
return sb.toString();
}
//or
public String readFile(File f) throws IOException {
StringBuilder sb = new StringBuilder();
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
//...
return sb.toString();
}
3、数组的设计器最好位于类型而不是变量上,这样开发人员可以根据类型确定变量是数组
//Noncompliant Code Example
int matrix[][]; // Noncompliant
int[] matrix[]; // Noncompliant
//Compliant Solution
int[][] matrix; // Compliant
4、Java7以后,不必同时在列表的声明和构造函数中声明列表的类型,编译器会自动推断类型,减少代码冗余
//Noncompliant Code Example
List<String> strings = new ArrayList<String>(); // Noncompliant
Map<String,List<Integer>> map = new HashMap<String,List<Integer>>(); // Noncompliant
//Compliant Solution
List<String> strings = new ArrayList<>();
Map<String,List<Integer>> map = new HashMap<>();
5、Class variable fields should not have public accessibility(类变量不应该是公共访问属性)
//Noncompliant Code Example
public class MyClass {
public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked
public String firstName; // Noncompliant
}
//Compliant Solution
public class MyClass {
public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked
private String firstName; // Compliant
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
6、**异常一定要打印到日志
//Noncompliant Code Example
try {
/* ... */
} catch(Exception e) {
e.printStackTrace(); // Noncompliant
}
//Compliant Solution
try {
/* ... */
} catch(Exception e) {
LOGGER.log("context", e);
}
7、try-with-resources自动关闭资源比try/catch/finally更好用
//Noncompliant Code Example
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
return br.readLine();
} catch (...) {
} finally {
if (br != null) {
try {
br.close();
} catch(IOException e){...}
}
if (fr != null ) {
try {
br.close();
} catch(IOException e){...}
}
}
//Compliant Solution
try (
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr)
) {
return br.readLine();
}
catch (...) {}
8、常量最好不要在接口中定义,常量使用在类中是为了实现细节,如果在接口中定义,那么实现该接口的类会导致细节泄露,同时也会造成常量污染,所以如果是单独的常量,还是采用枚举或者final类去定义
//Noncompliant Code Example
interface Status { // Noncompliant
int OPEN = 1;
int CLOSED = 2;
}
//Compliant Solution
public enum Status { // Compliant
OPEN,
CLOSED;
}
or
public final class Status { // Compliant
public static final int OPEN = 1;
public static final int CLOSED = 2;
}
9、if条件的执行语句要么在{}中,要么在if语句判断后缩进
//Noncompliant Code Example
if (condition) // Noncompliant
doTheThing();
doTheOtherThing();
somethingElseEntirely();
foo();
//Compliant Solution
if (condition)
doTheThing();
doTheOtherThing();
somethingElseEntirely();
foo();
10、switch语句,应该在最后添加default子句。除非switch条件是枚举,并case列举了所有可能常量除外
//Noncompliant Code Example
switch (param) { //missing default clause
case 0:
doSomething();
break;
case 1:
doSomethingElse();
break;
}
//Compliant Solution
switch (param) {
case 0:
doSomething();
break;
case 1:
doSomethingElse();
break;
default:
error();
break;
}