简体   繁体   English

java表达式太复杂,减少了conditionl运算符的数量

[英]java Expression too complex reduce the number of conditionl operators

We have a program that we run against our code to adhere to some coding standards. 我们有一个针对我们的代码运行的程序,以遵守一些编码标准。

The program says: 该计划说:

Expression should not be too complex, reduce the number of conditional operators used int he expression Min allowed 3. 表达式不应该太复杂,减少使用的条件运算符的数量,他的表达式Min允许3。

How can I reduce the number of conditional operators? 如何减少条件运算符的数量? perhaps put the keyevents in an array? 也许把关键事件放在一个数组中?

public boolean onlyNumbers(KeyEvent evt) {
    char c = evt.getKeyChar();
    boolean returnValue = true;
    if (
        !(
            Character.isDigit(c) 
            || c == KeyEvent.VK_BACK_SPACE
            || c == KeyEvent.VK_DELETE 
            || c == KeyEvent.VK_END 
            || c == KeyEvent.VK_HOME
        )
        || c == KeyEvent.VK_PAGE_UP
        || c == KeyEvent.VK_PAGE_DOWN
        || c == KeyEvent.VK_INSERT
    ) {
        evt.consume();
        returnValue = false;
    }
    return returnValue;
}
final String junkChars = new String(new char[] {
    KeyEvent.VK_BACK_SPACE,
    KeyEvent.VK_DELETE,
    KeyEvent.VK_END,
    KeyEvent.VK_HOME
    /* The last three seem unused in the latest version
    KeyEvent.VK_PAGE_UP,
    KeyEvent.VK_PAGE_DOWN,
    KeyEvent.VK_INSERT */
});
if(!Character.isDigit(c) && junkChars.indexOf(c) == -1) {
   evt.consume();
   return false;
}  else {
    return true;
}

A strict refactor of your sample would look like: 您的样本的严格重构看起来像:

public boolean onlyNumbers(KeyEvent evt) {
    char c = evt.getKeyChar();
    boolean returnValue = true;
    boolean bad = Character.isDigit(c);
    bad |= (c == KeyEvent.VK_BACK_SPACE);
    bad |= (c == KeyEvent.VK_DELETE);
    bad |= (c == KeyEvent.VK_END);
    bad |= (c == KeyEvent.VK_HOME);
    boolean good = (c == KeyEvent.VK_PAGE_UP);
    good |= c == KeyEvent.VK_PAGE_DOWN;
    good |= c == KeyEvent.VK_INSERT;
    if (!bad || good) {
        evt.consume();
        returnValue = false;
    }
    return returnValue;
}

This highlights the concerns others have noted about the placing of your brackets 这突出了其他人关于放置支架的注意事项

One way you could do this is to construct a HashSet<Character> keys containing all the characters you want to test for, and then use keys.contains(c) to test whether it's one of them. 你可以这样做的一种方法是构造一个包含你要测试的所有字符的HashSet<Character> keys ,然后使用keys.contains(c)来测试它是否是其中之一。

Alternatively, you could use a switch statement, and let all those characters fall through to the same block of code. 或者,您可以使用switch语句,并让所有这些字符落入同一代码块。

But my personal favourite would be to ignore the warning. 但我个人最喜欢的是忽略警告。 The code is perfectly clear as it is (modulo ajb's comment about parentheses). 代码非常清晰(模数ajb关于括号的注释)。

Here is one way to do it. 这是一种方法。 Could be improved by using an array. 可以通过使用数组来改进。

public boolean onlyNumbers(KeyEvent evt) {
    char c = evt.getKeyChar();
    boolean returnValue;

    returnValue =  !(Character.isDigit(c));
    returnValue &= !(c == KeyEvent.VK_BACK_SPACE);
    returnValue &= !(c == KeyEvent.VK_DELETE);
    returnValue &= !(c == KeyEvent.VK_END);
    returnValue &= !(c == KeyEvent.VK_HOME);
    returnValue |= (c == KeyEvent.VK_PAGE_UP);
    returnValue |= (c == KeyEvent.VK_PAGE_DOWN);
    returnValue |= (c == KeyEvent.VK_INSERT);
    if(returnValue)
    {
        evt.consume();
        returnValue = !returnValue;
    }
    return returnValue;
}

The first five and last three assignments could be compacted in their respective groupings to two total assignments, but that will boil down to your coding standards. 前五个和后三个任务可以在各自的分组中压缩到两个总分配,但这将归结为您的编码标准。

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

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