美文网首页
sonar常见bug类型及解决方法

sonar常见bug类型及解决方法

作者: 雪飘千里 | 来源:发表于2020-06-16 11:56 被阅读0次

写博客记录的目的,一方面是为了总结,另一方面也是为了提醒自己写代码的时候尽量避免同样的问题。

1、Equality tests should not be made with floating point values

解释

非整型数,运算由于精度问题,可能会有误差,所以比较的时候建议使用BigDecimal类型

BigDecimal data1 = new BigDecimal(totalArea);
BigDecimal data2 = new BigDecimal(s1);
//num =0 相等  >0前者大于后者 ,反之 <0 前者小于后者
int num = data1.compareTo(data2);

2、Strings and Boxed types should be compared using "equals()"

字符串和包装类型对比时应该使用equals方法。

解释

使用引用相等==或!=比较java.lang.String或包装类型(如java.lang.Integer)的两个实例几乎总是false,因为它不是在比较实际值,而是在内存中的位置(地址)。

在Java 中包装类型与基本数据类型存储位置不同。

  • Java 基本数据类型存放位置
    方法参数、局部变量存放在栈内存中的栈桢中的局部变量表
    常量存放在常量池中

  • 包装类型如Integer存放位置
    常量池
    堆内存

Integer 存储在常量池中时可以使用==对比,但当在堆内存中时,使用==对比,实际对比的是两个内存地址而非值。

3、Reduce the number of conditional operators (4) used in the expression (maximum allowed 3)

在一个表达式中条件判断不应该超过三个

//修改前
boolean rollback = (receiveType == Constants.ONE || receiveType == Constants.TWO || receiveType == Constants.THREE) && (result == null || !result.isSuccess());

//修改后,提取成一个方法
boolean rollback = checkReceiveType(receiveType) && (result == null || !result.isSuccess());

private boolean checkReceiveType(int receiveType) {
        return receiveType == Constants.ONE || receiveType == Constants.TWO || receiveType == Constants.THREE;
    }

4、The Cyclomatic Complexity of this method "xxx" is 19 which is greater than 15 authorized.

代码的复杂度太高了,通常可以把代码中独立的业务逻辑抽取成单独的方法;
sonar扫描是按照一个条件判断算一个复杂度的,也就是说在一个方法中不要写超过15个条件判断;
灵活使用StringUtils.isAnyBlank/isNoneBlank/BooleanUtils.and/BooleanUtils.or可以把多个条件判断变成一个,

//修改前
if(this.acId== null||this.userId==null||!Md5Util.encrypt(inputSign).equals(this.getMd5Sign())){
                flag = false;
            }
//修改后
if(StringUtils.isAnyBlank(this.acId,this.userId)||!Md5Util.encrypt(inputSign).equals(this.getMd5Sign())){
                flag = false;
            }

5、Return an empty collection instead of null

不能直接返回null

//修改前
return null

//修改后
return Collections.emptyList()/Collections.EmptyMap/Collections.EmptySet/new String[0]

6、Refactor this code to not nest more than 5 if/for/while/switch/try statements.

if/for/while/switch/try嵌套不能超过5层

7、Iterate over the "entrySet" instead of the "keySet"

遍历map时,使用entrySet,而不是keySet;

解释

"entrySet()" should be iterated when both the key and value are needed;
通过查看源代码发现,调用方法keySetMap.keySet()会生成KeyIterator迭代器,其next方法只返回其key值,而调用entrySetMap.entrySet()方法会生成EntryIterator 迭代器,其next方法返回一个Entry对象的一个实例,其中包含key和value。
所以当我们用到key和value时,用entrySet迭代性能更高

8、Use "Integer.parseInt" for this string-to-int conversion

使用Integer.parseInt 代替 Integer.valueOf

解释

Integer.parseInt(s)是把字符串解析成int基本类型,
Integer.valueOf(s)是把字符串解析成Integer对象类型

9、Replace "Collections.EMPTY_LIST" by "Collections.emptyList()

Collections.EMPTY_LIST VS Collections.emptyList()

Collections. emptyList()返回的也是一个空的List,它与Collections.EMPTY_LIST的唯一区别是,Collections. emptyList()支持泛型,所以在需要泛型的时候,可以使用Collections. emptyList()。
注意,这个空的集合是不能调用add来添加元素的,会直接抛异常

为啥不直接new ArrayList()呢?
因为new ArrayList()在初始化时会占用一定的资源

相关文章

网友评论

      本文标题:sonar常见bug类型及解决方法

      本文链接:https://www.haomeiwen.com/subject/rvuatktx.html