Java Too complex expression reduces conditionl statements

We have a program that we run against our code to adhere to some coding standards.

The program says:

The expression should not be too complex, reduce the number of conditional operators used in its expression. Min 3.

How to reduce the number of conditional statements? perhaps enter key events into 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;
}
+4
source share
4 answers
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;
}
+5
source

The strict refactor of your sample will look like this:

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 underlines the concerns that others have noted about placing your brackets.

+1
source

- HashSet<Character> keys, , , keys.contains(c), , .

switch, .

But my personal favorite would be to ignore the warning. The code is perfectly clear as it is (modulo ajb comment on parentheses).

0
source

Here is one way to do it. It can be improved with 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 three last jobs can be compressed in their respective groups to two general purposes, but this will come down to your coding standards.

0
source

Source: https://habr.com/ru/post/1570493/


All Articles