Sonar建议汇总
来源:互联网 发布:手机淘宝官网 编辑:程序博客网 时间:2024/06/04 01:18
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
下表描述了类和接口声明的各个部分以及它们出现的先后次序。
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即可。例:
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 messagesLOG.error("Unable to open file " + csvPath, e); // NoncompliantPreconditions.checkState(a > 0, "Arg must be positive, but got " + a); // Noncompliant. String concatenation performed even when a > 0Preconditions.checkState(condition, formatMessage()); // Noncompliant. formatMessage() invoked regardless of conditionPreconditions.checkState(condition, "message: %s", formatMessage()); // Noncompliant
推荐如下:
logger.log(Level.SEVERE, "Something went wrong: %s ", message); // String formatting only applied if neededlogger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazilyLOG.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 Exampleif (myClass.compareTo(arg) == -1) { // Noncompliant // ...}Compliant Solutionif (myClass.compareTo(arg) < 0) { // ...}
- Sonar建议汇总
- Sonar建议汇总
- sonar常见问题及修改建议
- sonar
- sonar
- Sonar
- sonar
- sonar
- maven + sonar, gradle + sonar
- 【Sonar】Sonar理论篇
- 【Sonar】Sonar实战篇
- 初试Sonar
- hudson sonar
- Hudson+Sonar
- Sonar 安装
- Sonar Eclipse
- Sonar语言
- sonar网址
- 人人都是产品经理之第二章摘要02
- leetcodeOJ 77. Combinations
- 系统知识
- 为何volatile不能保证原子性操作
- 一起做RGB-D SLAM (3)
- Sonar建议汇总
- handler和message传值两例
- JSP 学习笔记
- bzoj4330 JSOI2012 爱之项链
- 多线程、委托、事件的范本
- Android 热修复原理与实现方案学习
- 卡常暴搜——bzoj4239: 巴士走读
- IDEA常用技巧
- linux 开机一段时间自动重启脚本