美文网首页
常见sonar问题

常见sonar问题

作者: 04974ba324f9 | 来源:发表于2018-07-27 11:11 被阅读411次
这次改sonar一共400多个严重问题,很多易犯共性的地方整理如下:
  1. Either log or rethrow this exception——异常处理200多个此类问题!
Noncompliant Code Example:
(1) Nested blocks of code should not be left empty
try {
     业务代码             
} catch (Exception e) {
    //null
}

(2) Throwable.printStackTrace(...) should not be called0 —— Use a logger to log this exception
try {
     业务代码             
} catch (Exception e) {
    e.printStackTrace();
}

(3) Exception handlers should preserve the original exceptions
try {
     业务代码             
} catch (Exception e) {
    logger.error("send failed" + e.getMessage());
}

>>>
Compliant Solution:
try {
     业务代码             
} catch (Exception e) {
    ExceptionsHelper.errorMessage("send failed ", e);
}
  1. "public static" fields should be constant——公共静态成员应该加上final70多个此类问题!
Noncompliant Code Example:
public static String TYPE_NAME_METADATA = "大数据元数据";
>>>
Compliant Solution:
public static final String TYPE_NAME_METADATA = "大数据元数据";
  1. Math operands should be cast before assignment——数字操作在操作或赋值前要转化
Noncompliant Code Example:
long t = 12 * 60 * 60 * 1000;

>>>
Compliant Solution:
long t = 12 * 60 * 60 * 1000L;
  1. makes inefficient use of keySet iterator instead of entrySet iterator——用keySet 方式遍历Map的性能不如entrySet性能好
Noncompliant Code Example:
for (Long key : map.keySet()) {
}
>>>
Compliant Solution:
for (Entry<Long, Long> entry : map.entrySet()) {
}
  1. Use isEmpty() to check whether the collection is empty or not. ——集合的是否为空集合的判断
Noncompliant Code Example:
if (myCollection.size() == 0) { 
}
>>>
Compliant Solution:
if (myCollection.isEmpty()) {
}
  1. Dead stores should be removed——没用的存储应该除移

  2. Nullcheck of value previously dereferenced——多余的null检查;前边废弃null值检查的

System system = systemRepository.findOne(rel.getSystemId());
system.setLicence(null);
if (system == null || system.getId() == null) {
   continue;
}
>>>
System system = systemRepository.findOne(rel.getSystemId());
if (system == null || system.getId() == null) {
   continue;
}
system.setLicence(null);
  1. Move the "0" string literal on the left side of this string comparison.——字符串比较的左边放常量,右边放变量
String myString = null;
System.out.println("Equal? " + myString.equals("foo"));
>>>
System.out.println("Equal?" + "foo".equals(myString));
  1. Load of known null value——加载已知是null的值
if (topicId == null) {
    logger.error("havingRelationTopic fail topicId:" + topicId + " is null ");
    return false;
}
>>>
if (topicId == null) {
    logger.error("havingRelationTopic fail! topicId is null ");
    return false;
}
  1. Method invokes inefficient Number constructor; use static valueOf instead——Integer.ValueOf(int)的效率比Integer(int)快大约3.5倍
new Integer(offsetData.size()).equals(counts)
>>>
Integer.valueOf(offsetData.size()).equals(counts)

相关文章

  • 常见sonar问题

    这次改sonar一共400多个严重问题,很多易犯共性的地方整理如下: Either log or rethrow ...

  • Sonar常见问题总结

    1.Try catch异常输出printStackTrace(),相当于直接打印到控制台,不利于后续排查错误,应该...

  • try-with-statement语法糖总结

    最近处理的sonar问题,近一半都是资源未关闭的问题,sonar提示我们可以使用try-with-statemen...

  • sonar代码扫描常见问题以及处理方案

    sonar代码扫描常见问题以及处理方案 最近用sonarqube对目前各系统的开发源码进行代码规则的扫描,发现了部...

  • sonar 问题总结

    前言 sonar 是一个代码静态扫描工具,可以在开发阶段规避一些较为明显的问题。现在总结一些工作中经常遇到的扫出来...

  • nutch的编译问题

    问题1:Could not load definitions from resource org/sonar/an...

  • Sonar检测Math.abs(new Random().nex

    今天早上旁边同事喊我看一个Sonar检测出的问题: 当时看了好几眼没觉得这个有太大问题,于是又看了下Sonar建议...

  • webhook Response: Server Unreach

    定位到问题,应该是域名相关。 sonar服务:测试云环境上的sonar服务sonarqube:测试环境虚机上搭建的...

  • jenkins sonar

    sonar.projectKey=XXX sonar.projectName=XXX sonar.projectV...

  • Sonar报错问题处理

    ERROR: Error during SonarQube Scanner execution ERROR: Fa...

网友评论

      本文标题:常见sonar问题

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