簡體   English   中英

如何解決減少 java 中表達式中使用的條件運算符 (5) 的數量(最多允許 3 個)的聲納問題

[英]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 &= ba = a & b相同,出於實際目的,其結果與a = a && b相同(除了與a的值無關的性能外, b也將在 a 的情況下a & b評估a & b而在a && b的情況下,如果afalse則不處理b

在此基礎上,如果您以result = Objects.equals(this.exp, systemError.exp);開始您的新代碼,那么您的新代碼確實可以成為舊代碼的解決方案。 並以return result;

如果您在理解它時仍有任何問題,請隨時告訴我,我將嘗試進一步詳細說明我的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM