简体   繁体   English

重构多个if-else

[英]Refactor multiple if-else

I have below code with lot of if else codition. 我在下面的代码中有很多其他代码。 Due to this if else code looking more complex and also sonar saying "Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed." 由于这个原因,否则代码看起来更复杂,并且声纳也说"Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed."

Is there any other way so that I can reduce the complexcity. 还有什么其他方法可以降低复杂性。

    if (CONDITION1) {
        validatorVO.setErrorCode("ERROR_CODE_1");
    } else if (CONDITION2) {
        validatorVO.setErrorCode("ERROR_CODE_2");
    } else if (CONDITION3) {
        validatorVO.setErrorCode("ERROR_CODE_3");
    } else if (CONDITION4) {
        if (CONDITION5) {
            validatorVO.setErrorCode("ERROR_CODE_4");
        } else if (CONDITION6) {
            validatorVO.setErrorCode("ERROR_CODE_5");
        } else if (CONDITION7) {
            validatorVO.setErrorCode("ERROR_CODE_6");
        } else {
            validatorVO.setErrorCode("ERROR_CODE_7");
        }
    } else if (CONDITION8) {
        validatorVO.setErrorCode("ERROR_CODE_8");
    } else if (CONDITION9) {
        validatorVO.setErrorCode("ERROR_CODE_9");
    } else if (CONDITION10) {
        validatorVO.setErrorCode("ERROR_CODE_10");
    } else if (CONDITION11) {
        validatorVO.setErrorCode("ERROR_CODE_11");
    }
}

Note : Based on the condition error code will be different 注意:根据条件错误代码会有所不同

With your amended question, one option would be to maintain a LinkedHashMap<Supplier<Boolean>, String> with the error codes (to preserve order). 对于您提出的问题,一种选择是维护带有错误代码的LinkedHashMap<Supplier<Boolean>, String> (以保留顺序)。 Note that the order matters (if you put CONDITION4 first, the combined conditions will never be checked). 请注意,顺序很重要(如果您首先放置CONDITION4 ,将永远不会检查合并条件)。

Map<Supplier<Boolean>, String> conditions = new LinkedHashMap<> ();
conditions.put(() -> CONDITION1, "ERROR_CODE_1");
conditions.put(() -> CONDITION2, "ERROR_CODE_2");
conditions.put(() -> CONDITION3, "ERROR_CODE_3");
conditions.put(() -> CONDITION4 && CONDITION5, "ERROR_CODE_4");
conditions.put(() -> CONDITION4 && CONDITION6, "ERROR_CODE_5");
conditions.put(() -> CONDITION4 && CONDITION7, "ERROR_CODE_6");
conditions.put(() -> CONDITION4, "ERROR_CODE_7");
conditions.put(() -> CONDITION8, "ERROR_CODE_8");
conditions.put(() -> CONDITION9, "ERROR_CODE_9");
conditions.put(() -> CONDITION10, "ERROR_CODE_10");
conditions.put(() -> CONDITION11, "ERROR_CODE_11");

Then iterate: 然后重复:

for (Entry<Supplier<Boolean>, String> e : conditions.entrySet()) {
  if (e.getKey().get()) {
    validatorVO.setErrorCode(e.getValue());
    break;
  }
}

OLD ANSWER 老答案

Without knowing what the conditions are, it's hard to refactor the code too much. 不知道条件是什么,很难过多地重构代码。

You can write: 你可以写:

if (CONDITION1 || CONDITION2 || ... CONDITION11) validatorVO.setErrorCode("FAILED");

Of you may put the conditions in an array: 您可以将条件放入数组中:

boolean[] conditions = {CONDITION1, CONDITION2, ...}
for (boolean b : conditions) {
  if (b) validatorVO.setErrorCode("FAILED");
}

Also note that you can exclude CONDITION5, CONDITION6 and CONDITION7 since they are ignored in your code (whether they are true or not makes not difference). 还要注意,您可以排除CONDITION5,CONDITION6和CONDITION7,因为它们在您的代码中将被忽略(无论正确与否都没有区别)。

If the same result, then you can do something like: 如果结果相同,则可以执行以下操作:

if (CONDITION1 || CONDITION2 ||CONDITION3
     || CONDITION4
     || CONDITION8 || CONDITION9 ||CONDITION10 || CONDITION11) {
        validatorVO.setErrorCode("FAILED");
}

Actually, your CONDITION4 always evaluate 实际上,您的CONDITION4总是会评估

validatorVO.setErrorCode("FAILED");

regardless of CONDITION5 , CONDITION6 , CONDITION7 (thanks to Peter Lawrey for the hint) 不管CONDITION5CONDITION6CONDITION7 (感谢Peter Lawrey的提示)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM