[关闭]
@zero1036 2017-03-23T06:13:04.000000Z 字数 2922 阅读 5287

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<Event, EventVO> voMap = new PropertyMap<Event, EventVO>()

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即可。例:

  1. public class Shape {
  2. private Shape() {
  3. /* set something here */
  4. }
  5. public static Shape makeShape(/* arglist */) {
  6. System.out.println("here is the shape you ordered");
  7. return (new Shape());
  8. }
  9. }

Invoke method(s) only conditionally

以下代码会报错:

  1. logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages
  2. LOG.error("Unable to open file " + csvPath, e); // Noncompliant
  3. Preconditions.checkState(a > 0, "Arg must be positive, but got " + a); // Noncompliant. String concatenation performed even when a > 0
  4. Preconditions.checkState(condition, formatMessage()); // Noncompliant. formatMessage() invoked regardless of condition
  5. Preconditions.checkState(condition, "message: %s", formatMessage()); // Noncompliant

推荐如下:

  1. logger.log(Level.SEVERE, "Something went wrong: %s ", message); // String formatting only applied if needed
  2. logger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazily
  3. LOG.error("Unable to open file {}", csvPath, e);
  4. if (LOG.isDebugEnabled() {
  5. LOG.debug("Unable to open file " + csvPath, e); // this is compliant, because it will not evaluate if log level is above debug.
  6. }
  7. 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,但并非全部,建议如下:

  1. Noncompliant Code Example
  2. if (myClass.compareTo(arg) == -1) { // Noncompliant
  3. // ...
  4. }
  5. Compliant Solution
  6. if (myClass.compareTo(arg) < 0) {
  7. // ...
  8. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注