【发布时间】:2021-01-30 21:47:05
【问题描述】:
我是 Java 新手。我不知道如何使这段代码更简单。
if (word == null || word.length() < 3 || !Character.isLetter(word.charAt(0)) || !Character.isDigit(word.charAt(word.length()-1)) || word.matches("[\\s\\S]*\\s[\\s\\S]*")) {
return false;
} else {
return true;
}
sonarqube 说“用一个 return 语句替换这个 if-then-else 语句。”我不知道如何修改它。
【问题讨论】:
-
return !(word == null... -
或
return word != null && word.length() >= 3 && Character.isLetter(word.charAt(0)) && Character.isDigit(word.charAt(word.length()-1)) && !word.matches("[\\s\\S]*\\s[\\s\\S]*"); -
这是可怕的代码。强烈考虑重做这个。
标签: java coding-style