[英]java:S1067 - Reduce the number of conditional operators (5) used in the expression (maximum allowed 3)
How can I reduce the number of conditional operators?如何减少条件运算符的数量? Sonar showing Major issue like Reduce the number of conditional operators (5) used in the expression (maximum allowed 3) but those all condition mandatory to keep in this block:
声纳显示主要问题,例如减少表达式中使用的条件运算符 (5) 的数量(最多允许 3 个),但所有条件都必须保留在此块中:
private String processfromOrigin(Object value) {
if ((value instanceof A) || (value instanceof B)
|| (value instanceof C) || (value instanceof D)
|| (value instanceof E)
|| (value instanceof F)) {
return ((baseDto) processo).getProcess();
} else if (value instanceof G) {
return ((G) value ).getProcess();
} else if (value instanceof H) {
return ((H) value ).getProcess();
} else {
return (String) value ;
}
}
You can use code like below to simplify the first conditional in the if block:您可以使用如下代码来简化 if 块中的第一个条件:
import static java.util.Stream.of;
boolean checkABCDEF = of(A.class, B.class, C.class, D.class, E.class, F.class).
anyMatch(aClass -> aClass.isInstance(value))
of, you can encapsulate it in a local method: of,你可以把它封装在一个本地方法中:
private boolean isABCDEF(Object x) {
return Stream.of(A.class, B.class, C.class, D.class, E.class, F.class).
anyMatch(aClass -> aClass.isInstance(x));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.