简体   繁体   English

减少表达式中使用的条件运算符(4)的数量(允许的最大值3)

[英]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();
}

However, 然而,

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.

相关问题 减少 SONAR 中表达式中使用的条件运算符 (4) 的数量(最多允许 3 个) - Reduce the number of conditional operators (4) used in the expression (maximum allowed 3) in SONAR java:S1067 - 减少表达式中使用的条件运算符 (5) 的数量(最多允许 3 个) - java:S1067 - Reduce the number of conditional operators (5) used in the expression (maximum allowed 3) 如何解决减少 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 减少条件运算符的数量 - Reduce number of conditional operators 减少条件运算符的数量 - Reduce the number of conditional operators java表达式太复杂,减少了conditionl运算符的数量 - java Expression too complex reduce the number of conditionl operators 如何设置电话号码的正则表达式中允许的最小和最大位数 - How to set the minimum and maximum number of digit allowed in a regex expression for telephone number 超过 ActiveMQ 中允许的最大客户端连接数 - Exceeded the maximum number of allowed client connections in ActiveMQ Activemq - 超过了允许的最大客户端连接数 - Activemq - Exceeded the maximum number of allowed client connections 减少二叉表达式树的括号数量 - Reduce number of parentheses for a binary expression tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM