Wal*_*uch 6 java pmd sonarqube
这部分代码被声纳中的pmd拒绝:
public String getFoo() {
String foo = System.getProperty("foo");
if (foo == null) {
foo = System.getenv("foo");
} else if (foo == null) {
foo = "defaultFoo";
}
return foo;
}
Run Code Online (Sandbox Code Playgroud)
它说"在条件下避免文字".有人能告诉我这个或这个规则试图产生什么问题吗?
你为什么不用:
public String getFoo() {
String foo = System.getProperty("foo", "defaultFoo");
return foo;
}
Run Code Online (Sandbox Code Playgroud)
"defaultFoo"如果没有找到属性,它将返回.
Sonar 试图说的是,您应该避免null在条件中使用硬编码文字(例如 )if。
假设以下示例:
假设我们有这样的if语句,Sonar 对此语句发出避免在 If Condition 中使用文字的警告:
if (i == 5) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
通过将硬编码文字声明为final具有描述性名称的 ( ) 变量,可维护性得到增强:
final int FIVE = 5;
if (i == FIVE) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
声纳不再发出警告。
| 归档时间: |
|
| 查看次数: |
6301 次 |
| 最近记录: |