Sonar建议汇总

Sonar建议汇总

标签(空格分隔): 工具 编码规范


The Cyclomatic Complexity of this method “deleteMission” is 14 which is greater than 10 authorized

嵌套复杂度为14,高于限定值10

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

重构此代码,不得超过3行

Move this variable to comply with Java Code Conventions

构造函数应遵守Java代码约定,请移动变量的代码。参考:Java Code Conventions
下表描述了类和接口声明的各个部分以及它们出现的先后次序。

次序 类/接口声明的各部分 注解
1 类/接口文档注释(/*……/) 该注释中所需包含的信息,参见”文档注释”
2 类或接口的声明
3 类/接口实现的注释(/……/)如果有必要的话 该注释应包含任何有关整个类或接口的信息,而这些信息又不适合作为类/接口文档注释。
4 类的(静态)变量 首先是类的公共变量,随后是保护变量,再后是包一级别的变量(没有访问修饰符,access modifier),最后是私有变量。
5 实例变量 首先是公共级别的,随后是保护级别的,再后是包一级别的(没有访问修饰符),最后是私有级别的。
6 构造器
7 方法 这些方法应该按功能,而非作用域或访问权限,分组。例如,一个私有的类方法可以置于两个公有的实例方法之间。其目的是为了更便于阅读和理解代码。

Reorder the modifiers to comply with the Java Language Specification.

调整修饰符次序,反例:public final static PropertyMap voMap = new PropertyMap()

Rename this constant name to match the regular expression ‘^[A-Z][A-Z0-9](_[A-Z0-9]+)$’

常量要求大写

Add a private constructor to hide the implicit public one.

工具类不应该有默认或者公共的构造函数,也就是说这个类里可能方法都是static,那就不需要构造它的实例,因此应该给加一个private的构造函数,就不会报这个错了。
a class which only has private constructors should be final
例如上一个,加了private构造函数,又会出这个,把class设置成final即可。例:

  public class Shape {
    private Shape() { 
       /* set something here */
    }

    public static Shape makeShape(/* arglist */) {
       System.out.println("here is the shape you ordered");
       return (new Shape());
    }
 }

Invoke method(s) only conditionally

以下代码会报错:

logger.log(Level.DEBUG, "Something went wrong: " + message);  // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages

LOG.error("Unable to open file " + csvPath, e);  // Noncompliant

Preconditions.checkState(a > 0, "Arg must be positive, but got " + a);  // Noncompliant. String concatenation performed even when a > 0

Preconditions.checkState(condition, formatMessage());  // Noncompliant. formatMessage() invoked regardless of condition

Preconditions.checkState(condition, "message: %s", formatMessage());  // Noncompliant

推荐如下:

logger.log(Level.SEVERE, "Something went wrong: %s ", message);  // String formatting only applied if needed

logger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazily

LOG.error("Unable to open file {}", csvPath, e);

if (LOG.isDebugEnabled() {
  LOG.debug("Unable to open file " + csvPath, e);  // this is compliant, because it will not evaluate if log level is above debug.
}

Preconditions.checkState(arg > 0, "Arg must be positive, but got %d", a);  // String formatting only applied if needed

官方解释:

“Preconditions” and logging arguments should not require evaluation

Passing message arguments that require further evaluation into a Guava com.google.common.base.Preconditions check can result in a performance penalty. That’s because whether or not they’re needed, each argument must be resolved before the method is actually called.

Only the sign of the result should be examined

While most compareTo methods return -1, 0, or 1, some do not, and testing the result of a compareTo against a specific value other than 0 could result in false negatives.

大部分compareTo()比较方法,返回结果都是-1, 0, or 1,但并非全部,建议如下:

Noncompliant Code Example

if (myClass.compareTo(arg) == -1) {  // Noncompliant
  // ...
}
Compliant Solution

if (myClass.compareTo(arg) < 0) {
  // ...
}

你可能感兴趣的:(编码规范,工具)