java编码规范常见错误

1.Merge this if statement with the enclosing one

应该合并可折叠的语句

2.String a = null;

需要初始化 String a;

3.捕捉到的异常 用日志打印

} catch (Exception e) {
      Log logger.info(e);
     //或者 Logger logger.info("",e);
}

4.判断是否大于0(是否为空)

不用orderMainBeanList.size() > 0

使用 !orderMainBeanList.isEmpty()

5.case  1

    ...

这里的业务不能大于5行,如果大于5行,另写一个private的方法去写这里的业务,然后这里调用。


6.public static final String GET_VIEW_ROWS

不是public final static  String GET_VIEW_ROWS

7.Replace the type specification in this constructor call with the diamond operator ("<>"). (sonar.java.source not set. Assuming 7 or greater.)  

Map<String, String> M_FILETYPES= new HashMap<String, String>();

改成

Map<String, String> M_FILETYPES= new HashMap<>();

8.方法名称排序

先构造

再有参构造

再其他方法

9.return null改成  Collections.emptyList();

10.NullPointerException might be thrown as 'dt2' is nullable here

先判断是否为空再去操作

11.Introduce a new variable instead of reusing the parameter "prefixKey"

不要用传递过来的参数去重新赋值做判断等

可以新建一个参数  等于传递过来的参数  用新的参数去操作

private String getBatchSaveKey(Map<String, String> map, String prefixKey, String... keys) {
        //创建一个newPrefixKey
        String newPrefixKey;
        newPrefixKey = prefixKey.endsWith(RedisKeys.DELIMITER) ? prefixKey.substring(0, prefixKey.length() - 1) : prefixKey;
        if (keys.length == 0) {
            return newPrefixKey;
        }

不直接用prefixKey = prefixKey.endsWith(RedisKeys.DELIMITER) ? prefi。。。
也不直接用return prefixKey;而是去创建newPrefixKey

12.Cast one of the operands of this division operation to a "double"

使用int pageSize=(int) Math.ceil(count/3);

需要转double  int pageSize=(int) Math.ceil((double) count/3);

13.Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation

对于bean里面的类 手写构造
  public  CodeBean(){}
 

猜你喜欢

转载自my.oschina.net/u/3559695/blog/1795231