[英]Reduce number of conditional operators(4) used in the expression (maximum allowed 3)
How can i reduce the complexitiy of my operators? 如何降低操作员的复杂性? i have a conditional issue.
我有条件的问题。
Object someObject = getSomeObject();
boolean isNotInstanceOfA = !(someObject instanceof A);
boolean isNotInstanceOfB = !(someObject instanceof B);
boolean isNotInstanceOfC = !(someObject instanceof C);
boolean isNotInstanceOfABC = isNotInstanceOfA && isNotInstanceOfB && isNotInstanceOfC;
if (isNotInstanceOfABC && (container.getChildren(itemId) == null || container.getChildren(itemId).isEmpty())) {
return "something";
}
You can actually compute the conditions prior to the if
block so that the code will be more readable as shown below: 实际上,您可以在
if
块之前计算条件,以使代码更具可读性 ,如下所示:
boolean isChildNull = (container.getChildren(itemId) == null);
boolean isChildEmpty = (isChildNull || container.getChildren(itemId).isEmpty());
if (isNotInstanceOfABC && isChildEmpty) {
return "something";
}
But, I infer from your code that you are doing something wrong because you are doing lots of instanceof
checks which is NOT at all a good practice , you might need to consider refactoring your code as explained here ( Replace conditionals with polymorphism ). 但是,我从您的代码推断出您做错了,因为您执行了很多
instanceof
检查,而这根本不是一个好习惯 ,您可能需要考虑按此处所述 重构代码( 用多态替换条件 )。
itemId is primitive type or not ? itemId是原始类型吗? i am guessing that it is not primitive and to reduce code you can move this null or empty checking to a separate method and return a boolean value.. and you can also reduce some code like this: `
我猜它不是原始的,要减少代码,您可以将此null或empty检查移到一个单独的方法并返回一个布尔值..您还可以减少一些这样的代码:
boolean isNotInstanceOfABC = !(someObject instanceof A) && !(someObject instanceof B) && !(someObject instanceof C);
if (isNotInstanceOfABC && isEmptyOrNull(container.getChildren(itemId))) {
return "something";
}`
Not sure if it is an option for you, but i would try to refactor code in this way: 不知道这是否是您的选择,但我会尝试以这种方式重构代码:
Object someObject = getSomeObject();
if (isInstanceOfABC(someObject)) {
log.debug("Instance of A, B or C");
return;
}
if (childrenIsEmptyOrNull( container.getChildren(itemId) ) {
log.debug("Children is null or empty");
return;
}
return "something";
private boolean isInstanceOfABC(Object someObject) {
return someObject instanceof A
|| someObject instanceof B
|| someObject instanceof C;
}
private boolean childrenIsEmptyOrNull(Children children) {
return children == null || children.isEmpty();
}
using instance of
is a code smell . 使用
instance of
是代码的味道 。 Whenever I see a construction like that, I'm sure that something went awry. 每当我看到这样的构造时,我肯定会出现问题。 You'd better stop now and get rid of all this checks with the help of polymorphysm.
您最好现在就停止,并在polymorphysm的帮助下摆脱所有这些检查。
Normalise your logical expression then utilise the method below 规范化您的逻辑表达式,然后使用以下方法
public boolean AND_Operator(boolean ...values) {
for (boolean b : values) {
if(!b)
return false;
}
return true;
}
public static boolean OR_Operator(boolean ...values) {
for (boolean b : values) {
if(b)
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.