SonarQube常见代码优化记录(一)

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值