java 表达式太复杂减少条件运算符的数量
java Expression too complex reduce the number of conditionl operators
我们有一个程序根据我们的代码运行以遵守一些编码标准。
程序说:
Expression should not be too complex, reduce the number of conditional operators used int he expression Min allowed 3.
如何减少条件运算符的数量?也许将关键事件放在一个数组中?
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;
}
您可以这样做的一种方法是构造一个 HashSet<Character> keys
包含您要测试的所有字符,然后使用 keys.contains(c)
来测试它是否是其中之一。
或者,您可以使用 switch
语句,让所有这些字符落入同一代码块。
但我个人最喜欢忽略警告。代码非常清楚(modulo ajb 关于括号的评论)。
这是一种方法。可以通过使用数组来改进。
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;
}
前五个和最后三个作业可以在各自的分组中压缩为两个总作业,但这将归结为您的编码标准。
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;
}
您的示例的严格重构如下所示:
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;
}
这凸显了其他人对您放置括号的关注
我们有一个程序根据我们的代码运行以遵守一些编码标准。
程序说:
Expression should not be too complex, reduce the number of conditional operators used int he expression Min allowed 3.
如何减少条件运算符的数量?也许将关键事件放在一个数组中?
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;
}
您可以这样做的一种方法是构造一个 HashSet<Character> keys
包含您要测试的所有字符,然后使用 keys.contains(c)
来测试它是否是其中之一。
或者,您可以使用 switch
语句,让所有这些字符落入同一代码块。
但我个人最喜欢忽略警告。代码非常清楚(modulo ajb 关于括号的评论)。
这是一种方法。可以通过使用数组来改进。
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;
}
前五个和最后三个作业可以在各自的分组中压缩为两个总作业,但这将归结为您的编码标准。
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;
}
您的示例的严格重构如下所示:
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;
}
这凸显了其他人对您放置括号的关注