在解决sonarQube问题时,我面临以下警告,是否有人告诉我如何克服此警告
方法:-
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Division other = (Division) obj;
if (divisionId != other.divisionId)
//getting warning for above if condition
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
警告 :
用单个return语句替换这个if-then-else语句.
描述:-
应该简化包含在if-then-else中的布尔文字语句的返回.
Era*_*ran 12
好吧,你可以替换:
if (divisionId != other.divisionId)
return false;
return true;
Run Code Online (Sandbox Code Playgroud)
相当于:
return divisionId == other.divisionId;
Run Code Online (Sandbox Code Playgroud)
false如果divisionId != other.divisionId或true其他原因,这将返回.
小智 5
我在使用 sonarlint 时收到了类似的警告消息“布尔表达式的返回不应包含在“if-then-else”语句中”这是我以前的代码,
if (val.isEmpty()) {
switchCompat.setChecked( false );
} else {
switchCompat.setChecked( true );
}
Run Code Online (Sandbox Code Playgroud)
现在我把它改成,
boolean checked = val.isEmpty();
switchCompat.setChecked( checked );
Run Code Online (Sandbox Code Playgroud)
根据这个问题,它类似于,
@Override
public boolean equals(Object obj) {
Division other = (Division) obj;
if (this == obj)
return true;
else if (obj == null)
return false;
else if (getClass() != obj.getClass())
return false;
else if (divisionId != other.divisionId)
return false;
else
return true;
}
Run Code Online (Sandbox Code Playgroud)
同样,它可以像这样解析,
@Override
public boolean equals(Object obj) {
boolean success;
Division other = (Division) obj;
if (this == obj)
success = true;
else if (obj == null)
success = false;
else if (getClass() != obj.getClass())
success = false;
else if (divisionId != other.divisionId)
success = false;
else
success = true;
return success;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8084 次 |
| 最近记录: |