![](/img/trans.png)
[英]Reduce the number of conditional operators (4) used in the expression (maximum allowed 3) in SONAR
[英]How to fix sonar issues for Reduce the number of conditional operators (5) used in the expression (maximum allowed 3) in java
ObjectTest systemError = (ObjectTest ) o;
//New Code
result &= Objects.equals(this.exp1, systemError.exp1);
result &= Objects.equals(this.exp2, systemError.exp2) ;
result &= Objects.equals(this.exp3, systemError.exp3);
result &= Objects.equals(this.exp4, systemError.exp4);
result &= Objects.equals(this.exp5, systemError.exp5) ;
result &= Objects.equals(this.exp6, systemError.exp6);
return result;
//Old Code
return Objects.equals(this.exp, systemError.exp) &&
Objects.equals(this.exp1, systemError.exp1) &&
Objects.equals(this.exp2, systemError.exp2) &&
Objects.equals(this.exp3, systemError.exp3) &&
Objects.equals(this.exp4, systemError.exp4) &&
Objects.equals(this.exp5, systemError.exp5) &&
Objects.equals(this.exp6, systemError.exp6);
新代碼是舊代碼的解決方案嗎?任何人都可以對此進行確認。
請注意, a &= b
與a = a & b
相同,出於實際目的,其結果與a = a && b
相同(除了與a
的值無關的性能外, b
也將在 a 的情況下a & b
評估a & b
而在a && b
的情況下,如果a
為false
則不處理b
)
在此基礎上,如果您以result = Objects.equals(this.exp, systemError.exp);
開始您的新代碼,那么您的新代碼確實可以成為舊代碼的解決方案。 並以return result;
如果您在理解它時仍有任何問題,請隨時告訴我,我將嘗試進一步詳細說明我的解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.