sonar问题描述
最近公司项目结合了jenkins以及sonar,排查出了之前已经离职同事,许多代码问题现在我就简单介绍下我解决的出现比较多的sonar问题1、Define a constant instead of duplicating this literal “djxhs” 3 times.定义常量去替代重复的字面值==>简单来说,就是一个"djxhs"重复太多了,要求你定义一个常量来代替它之前//N
最近公司项目结合了jenkins以及sonar,排查出了之前已经离职同事,许多代码问题
现在我就简单介绍下我解决的出现比较多的sonar问题
1、Define a constant instead of duplicating this literal “djxhs” 3 times.
定义常量去替代重复的字面值==>简单来说,就是一个"djxhs"重复太多了,要求你定义一个常量来代替它
之前
//Noncompliant Code Example
public void run() {
prepare("action1"); // Noncompliant - "action1" is duplicated 3 times
execute("action1");
release("action1");
}
@SuppressWarning("all") // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }
public String method3(String a) {
System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded
return ""; // Compliant - literal "" has less than 5 characters and is excluded
}
//Compliant Solution
之后
private static final String ACTION_1 = "action1"; // Compliant
public void run() {
prepare(ACTION_1); // Compliant
execute(ACTION_1);
release(ACTION_1);
}
2、Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
代码中圈复杂度不能太高==>大多是因为if,else过多
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
圈复杂度:
http://blog.csdn.net/lg707415323/article/details/7790660
3、Remove this unused import 'gov.gt3.iitms.refactor.basecode.util.CommUtil’
移除未使用到的包==》就需要删除掉那些个注释不用的代码
//Noncompliant Code Example
package my.company;
import java.lang.String; // Noncompliant; java.lang classes are always implicitly imported
import my.company.SomeClass; // Noncompliant; same-package files are always implicitly imported
import java.io.File; // Noncompliant; File is not used
import my.company2.SomeType;
import my.company2.SomeType; // Noncompliant; 'SomeType' is already imported
class ExampleClass {
public String someString;
public SomeType something;
}
4、Refactor this method to not always return the same value.
方法中不同分支不应该返回相同的值 ==》你判断了不同的条件,就要返回不同的结果
错误示范:
//Noncompliant Code Example
int foo(int a) {
int b = 12;
if (a == 1) {
return b;
}
return b; // Noncompliant
}
5、URIs should not be hardcoded 这个我好像没遇到。。。
Hard coding a URI makes it difficult to test a program: path literals are not always portable across operating systems, a given absolute path may not exist on a specific test environment, a specified Internet URL may not be available when executing the tests, production environment filesystems usually differ from the development environment, …etc. For all those reasons, a URI should never be hard coded. Instead, it should be replaced by customizable parameter.
Further even if the elements of a URI are obtained dynamically, portability can still be limited if the path-delimiters are hard-coded.
This rule raises an issue when URI’s or path delimiters are hard coded.
Url不要硬编码
public class Foo {
// Configuration is a class that returns customizable properties: it can be mocked to be injected during tests.
private Configuration config;
public Foo(Configuration myConfig) {
this.config = myConfig;
}
public Collection listUsers() {
// Find here the way to get the correct folder, in this case using the Configuration object
String listingFolder = config.getProperty(“myApplication.listingFolder”);
// and use this parameter instead of the hard coded path
File userList = new File(listingFolder, “users.txt”); // Compliant
Collection users = parse(userList);
return users;
}
}
6、Method names should comply with a naming convention
==》定义方法名未按照指定格式定义
hared naming conventions allow teams to collaborate efficiently. This rule checks that all method names match a provided regular expression.
With default provided regular expression 1[a-zA-Z0-9]*$;
方法命名要符合规范;
7、Comments should not be located at the end of lines of code : Move this trailing comment on the previous empty line.
注释不应位于代码行的结尾
8、Reduce the total number of break and continue statements in this loop to use at most one. 这个我好像也没改
Restricting the number of break and continue statements in a loop is done in the interest of good structured programming.
One break and continue statement is acceptable in a loop, since it facilitates optimal coding. If there is more than one, the code should be refactored to increase readability.
for (int i = 1; i <= 10; i++) { // Noncompliant - 2 continue - one might be tempted to add some logic in between
if (i % 2 == 0) {
continue;
}
if (i % 3 == 0) {
continue;
}
System.out.println("i = " + i);
}
9、Use try-with-resources or close this “FileInputStream” in a “finally” clause.
关闭流==》需要注意的是,需要等流用完了或者不用的才关闭,别你的流还在用,你就给人家关了
10、Remove this conditional structure or edit its code blocks so that they’re not all the same. 减少代码重用吧
Having all branches in a switch or if chain with the same implementation is an error. Either a copy-paste error was made and something different should be executed, or there shouldn’t be a switch/if chain at all. Note that this rule does not apply to if chains without else-s, or to switch-es without default clauses.
11、Merge this if statement with the enclosing one.
你的if语句能合并的,就合并,if语句往小的方向调整
Merging collapsible if statements increases the code’s readability.
//Noncompliant Code Example
if (file != null) {
if (file.isFile() || file.isDirectory()) {
/* … /
}
}
//Compliant Solution
if (file != null && isFileOrDirectory(file)) {
/ … */
}
private static boolean isFileOrDirectory(File file) {
return file.isFile() || file.isDirectory();
}
12、This block of commented-out lines of code should be removed.
移除注释掉的代码
13.结合泛型问题
这个介绍是啥忘了
反正就是sonar中允许以下
List<String> list = new ArrayList<>();
不允许下面这个
List<String> list = new ArrayList<String>();
我这边解决问题主要是参考以下这位兄弟的,你们也可以看下
http://blog.leanote.com/post/yuanquan.ni@qq.com/%E8%AE%B0%E5%BD%95%E6%88%91%E5%9C%A8%E7%A8%8B%E5%BA%8F%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%8F%E5%91%B3%E9%81%93
如果还有其他出现的,也可以在评论中回复,我后期也可以添加,谢谢了
a-z ↩︎
更多推荐
所有评论(0)