sonar常见扫描问题总结
总结我们在开发中常见的代码问题,同时将sonar中的问题也归纳了进来
一、异常处理中的未打印或者抛出异常信息
这个主要是没有将异常信息打印出来,又或者异常没有抛出。比如我们在action中,异常信息是打印成日志,而service中是将异常信息抛出,按照这样做了,就不会再有这类的错误。
l Sonar
Either log or rethrow this exception. |
l 错误示例
略
l 处理方案
将异常通过日志打印或者抛出
二、使用非同步的类来代替同步的类
非同步的类在执行效率上会高于同步的类,所以在不是必须同步的情况下,建议使用非同步类。
l Sonar
Replace the synchronized class "StringBuffer" by an unsynchronized one such as "StringBuilder". |
l 错误示例
略
l 处理方案
ArrayList or LinkedList instead of Vector Deque instead of Stack HashMap instead of Hashtable StringBuilder instead of StringBuffer |
三、移除没有用到的包、变量、方法
l Sonar
Remove this useless assignment to local variable xxx |
l 错误示例
略
l 处理方案
移除无用的代码
四、增加有效注释,移除无用注释
我们在开发中,需要增加有效注释,移除无用的无效注释。有时候,在修改代码的时候也会将以前的代码进行注释,防止下次修改的时候再拿来参考或者使用。这个也不推荐,因为时间长了,这类的僵尸代码也比较难清除,并且影响代码的阅读
l Sonar
This block of commented-out lines of code should be removed. |
l 错误示例
略
l 处理方案
移除无用的注释
五、继承的方法需要添加@Override
l Sonar
Add the "@Override" annotation above this method signature |
l 错误示例
略
l 处理方案
在方法上方添加 @Override
六、嵌套不要超过三层
在写代码的时候,if/for/while/switch/try等代码的嵌套层数建议不要超过3层。当超过3层之后,代码的可读性和可维护性都会变的很差,同时如果做单测也会变的很艰难。
l Sonar
Refactor this code to not nest more than 3 if/for/while/switch/try statements. |
l 错误示例
if (condition1) { // Compliant - depth = 1 /* ... */ if (condition2) { // Compliant - depth = 2 /* ... */ for(int i = 0; i < 10; i++) { // Compliant - depth = 3, not exceeding the limit /* ... */ if (condition4) { // Noncompliant - depth = 4 if (condition5) { // Depth = 5, exceeding the limit, but issues are only reported on depth = 4 /* ... */ } return; } } } } |
l 处理方案
减少嵌套层数
七、方法块的圈复杂度过高
l Sonar
The Cyclomatic Complexity of this method xxx is 11 which is greater than 10 authorized. |
l 错误示例
略
l 处理方案
减少嵌套,减少代码行数,避免一个方法过于冗长和沉重,提高阅读性和可维护性。
八、应合并可折叠的if语句
l Sonar
Merge this if statement with the enclosing one. |
l 错误示例
if (file != null) { if (file.isFile() || file.isDirectory()) { /* ... */ } } |
l 处理方案
if (file != null && isFileOrDirectory(file)) { /* ... */ } private static boolean isFileOrDirectory(File file) { return file.isFile() || file.isDirectory(); } |
九、集合的是否为空集合的判断
在java的一些api中,提供了类似字符串、集合等常见对象的取值和判断的方法,我们要加以运用。
l Sonar
Use isEmpty() to check whether the collection is empty or not. |
l 错误示例
if (myCollection.size() == 0) { // Noncompliant /* ... */ } |
l 处理方案
if (myCollection.isEmpty()) { // Compliant /* ... */ } |
十、整形/长整形转字符串的方法
在java的一些api中,提供了类似字符串、集合等常见对象的取值和判断的方法,我们要加以运用。
l Sonar
Use "Long.toString" instead. |
l 错误示例
int myInt = 4; String myIntString = new Integer(myInt).toString(); // Noncompliant; creates & discards an Integer object myIntString = Integer.valueOf(myInt).toString(); // Noncompliant myIntString = 4 + ""; // Noncompliant |
l 处理方案
int myInt = 4; String myIntString = Integer.toString(myInt); |
十一、 BigDecimal的取值方法
在java的一些api中,提供了类似字符串、集合等常见对象的取值和判断的方法,我们要加以运用。
l Sonar
Use "BigDecimal.valueOf" instead. |
l 错误示例
double d = 1.1; BigDecimal bd1 = new BigDecimal(d); // Noncompliant; see comment above BigDecimal bd2 = new BigDecimal(1.1); // Noncompliant; same result |
l 处理方案
double d = 1.1; BigDecimal bd1 = BigDecimal.valueOf(d); BigDecimal bd2 = BigDecimal.valueOf(1.1); |
十二、 使用新的变量来代替传入的变量
方法中传入的变量,一般情况下,我们认为是不可修改的,一旦可修改的话,在测试的时候不容易发现问题是调用方发生的错误还是方法本身发生的错误,这也是有的方法的传入参数为什么要定义为final类型的原因。
l Sonar
Use isEmpty() to check whether the collection is empty or not. |
l 错误示例
class MyClass { public String name; public MyClass(String name) { name = name; // Noncompliant - useless identity assignment } public int add(int a, int b) { a = a + b; // Noncompliant /* additional logic */ return a; // Seems like the parameter is returned as is, what is the point? } public static void main(String[] args) { MyClass foo = new MyClass(); int a = 40; int b = 2; foo.add(a, b); // Variable "a" will still hold 40 after this call } } |
l 处理方案
class MyClass { public String name; public MyClass(String name) { this.name = name; // Compliant } public int add(int a, int b) { return a + b; // Compliant } public static void main(String[] args) { MyClass foo = new MyClass(); int a = 40; int b = 2; foo.add(a, b); } }} |
十三、 使用正确的类型转换方法
类型转换的方法有很多种方式,我们需要使用更合适的。
l Sonar
Use "Integer.parseInt" for this string-to-int conversion |
l 错误示例
String myNum = "12.2"; float f = new Float(myNum).floatValue(); // Noncompliant; creates & discards a Float |
l 处理方案
String myNum = "12.2"; float f = Float.parseFloat(myNum); |
十四、 不要使用System.out或者System.err
避免使用System.out和Sytem.err,而使用日志的方式输出,同时注意日志的级别,如果只是自己调试使用的话,不要使用Info以上的级别,使用debug就可以了。
l Sonar
Replace this usage of System.out or System.err by a logger. |
l 错误示例
System.out.println("My Message"); // Noncompliant |
l 处理方案
logger.info("My Message"); |
十五、 字符串比较的左边放常量,右边放变量
l Sonar
Move the "0" string literal on the left side of this string comparison. |
l 错误示例
String myString = null; System.out.println("Equal? " + myString.equals("foo")); // Noncompliant - will raise a NPE System.out.println("Equal? " + (myString != null && myString.equals("foo"))); // Noncompliant - null check could be removed |
l 处理方案
System.out.println("Equal?" + "foo".equals(myString)); // Compliant - properly deals with the null case |
sonar常见扫描问题总结相关推荐
- Sonar代码扫描常见规则总结
Sonar代码扫描常见规则 最近公司项目交付,交付前集成,功能,性能,安全种种测试工作就来了,由于测试离职,被抓壮丁,兼职起测试修改工作.小公司,平时敲(ctrl+c)代码(ctrl+v) 时,同事也 ...
- Sonar 质量扫描的输出日志--对应源码的跟踪(二){sonar engine源码}
一个project下面module完整的sonar分析日志: [INFO] --- sonar-maven-plugin:3.2:sonar (default-cli) @ pcaic-parent ...
- 看这里,全网最详细的Sonar代码扫描平台搭建教程
01 Sonar安装 官网:https://www.sonarqube.org/ 1.sonar简介 sonar是一款静态代码质量分析工具,支持Java.Python.PHP.JavaScript.C ...
- java代码扫描项目,sonar代码扫描实现(基于java-maven)
Sonar介绍 Sonar是一个用于代码扫描检测的开放平台.可以实现对不同语言(java.python.php.C++.C#等等)的项目代码进行分析,同时可以集成不同的测试工具.代码分析工具,以及持续 ...
- Sonar安全扫描代码规则
Sonar安全扫描代码规则 blocker级别 序号 分类 规则英语描述 翻译解读 1 Bug Loops should not be infinite 循环必须有一个终止条件 2 Bug Overr ...
- 设置sonar 排除扫描文件及问题
首先要设置 语言:sonar.language=java /**** 然后要设置 扫描源文件和编译文件 sonar.sources=./ sonar.java.binaries=./ 在设置 排 ...
- jenkins:集成sonar代码扫描+发送邮件
前提: Jenkins JDK 目录: 1.安装sonar插件:SonarQube Scanner for Jenkins 2.安装SonarQube 3.安装sonar-scanner ++++++ ...
- jenkins配置sonar并扫描C#代码
背景:我的jenkins搭在linux上 1.下载插件 下载插件sonarqube scanner,用来集成sonarqube,在配置任务的时候才有sonarscanner的选项. 2.全局工具配置 ...
- sonar本地扫描,没有胡里花哨的操作,通俗易懂~
首先java环境(jdk).数据库要先准备好: 1.jdk(需要设置环境变量) 2.安装sonarqube:http://www.sonarqube.org/downloads/ 这里拿社区版 ...
最新文章
- UltraTextEditor
- 创业路上的这点事之 从无到有,从有到......
- 大数据技术:分布式系统和分布式事务
- 课时 28:理解容器运行时接口 CRI(知谨)
- VC++中 PostMessage和SendMessage的区别
- IOS开发基础之音频工具类封装AVAudioPlayer
- jsf集成spring_Spring和JSF集成:分页
- 多媒体视频知识入门贴zt(二)
- 斯坦福 CS183 YC 创业课系列中文笔记
- Linux Socket C语言网络编程:TCP Socket
- xp系统windows 组件向导无iis安装选项解决办法
- 【Hadoop】Hadoop1.X版本与Hadoop2.X的区别
- C#生成Code39条形码【非条形码字体】
- Word处理控件Aspose.Words功能演示:在C#中将DOC或DOCX转换为HTML
- 北美周末票房榜TOP10 (06.21-23)
- 网易游戏互娱 笔试题2021.8.7 Java版
- BFS 寻找矩阵中两点之间的最短距离
- matlab计算器设计流程图_基于MATLAB计算器设计与开发
- oralc UPDATE关联表的思路总结
- unity室内渲染(白模渲染篇)
热门文章
- 阿里云mysql事件启动_mysql 启动事件-阿里云开发者社区
- linux 系统部署raid 5,CentOS 7.4搭建RAID5及测试实例
- C/C++——字符串分割(strtok, strtok_s)
- Intel CET 安全防御机制深度解析
- 四川传媒学院加入ACA世界大赛!发展博学笃行,德艺双馨人才
- js实现的模拟弹性网格布拉扯仿真动画可拉伸可切割
- 七牛云文件上传接口的使用
- C++ switch语句详解
- 【调剂】景德镇陶瓷大学关于招收2020年攻读硕士学位研究生调剂公告
- 服务器错误码显示,常见错误码说明