如何降低运算符(operator)的复杂性?我有一个有条件的问题。
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";
}
最佳答案
itemId 是否为原始类型? 我猜它不是原始的,为了减少代码,你可以将这个 null 或空检查移动到一个单独的方法并返回一个 boolean 值..你也可以减少一些这样的代码:`
boolean isNotInstanceOfABC = !(someObject instanceof A) && !(someObject instanceof B) && !(someObject instanceof C);
if (isNotInstanceOfABC && isEmptyOrNull(container.getChildren(itemId))) {
return "something";
}`
关于java - 减少表达式中使用的条件运算符 (4) 的数量(最多允许 3 个),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43248432/