我写了一些代码,只是想知道是否有更简洁的方法来做以防万一我必须做额外的条件?我不想使用嵌套的ifs。
有没有办法让它更加面向对象?
if(!usernameExists)
{
if(!username.matches("^[\\pL\\pN]+$") ||
username.length() < 7 ||
username.length() > 25)
{
usernameValid=false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText(
"<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>");
}
else
{
Forms.Main.instructorForm.usernameField.setBackground(Color.white);
Forms.Main.instructorForm.usernameField.setToolTipText("");
}
}
else
{
usernameValid=false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText(
"This username already exists!");
}
答案 0 :(得分:3)
我没有假设你所呈现的逻辑的正确性,但是对于我所显示的内容,我建议你重构并将第一个布尔值提取到一个单独的方法中。
if(!usernameExists) {
usernameValid = validateUsernameAndSetBackground(username);
} else {
// rest of code
}
public boolean validateUsernameAndSetBackground(String username) {
boolean result = true;
if(!username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25) {
result = false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText("<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>");
} else {
Forms.Main.instructorForm.usernameField.setBackground(Color.white);
Forms.Main.instructorForm.usernameField.setToolTipText("");
}
return result;
}
还有其他重构机会 - 即背景颜色和字段的设置。但这解决了很多条件性条款深层嵌套的直接问题。
答案 1 :(得分:1)
我要做的是以下内容。 这摆脱了一些神奇的数字并稍微分散了责任。
public static final String USERNAME_KEY = "^[\\pL\\pN]+$";
public static final int USERNAME_MIN_LENGTH = 7;
public static final int USERNAME_MAX_LENGTH = 25;
void foo() {
if(!usernameExists) {
usernameValid = validateUsernameAndSetBackground(username);
} else {
usernameValid = false;
adjustFormSettings(Color.red, "This username already exists!");
}
}
private boolean validateUsernameAndSetBackground(String username) {
if(usernameIsValid(username)) {
adjustFormSettings(Color.white, "");
return true;
} else {
adjustFormSettings(Color.red, getInvalidUsernameErrorMsg());
return false;
}
}
private void adjustFormSettings(Color color, String msg) {
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText(getErrorMsg());
}
private boolean usernameIsValid(String username) {
return username.matches(USERNAME_KEY)
&& username.length() >= USERNAME_MIN_LENGTH
&& username.length() <= USERNAME_MAX_LENGTH;
}
private String getInvalidUsernameErrorMsg() {
return "<html>Only letters & numbers allowed!<br> Length must be between "
+ USERNAME_MIN_LENGTH + " and " + USERNAME_MAX_LENGTH + ".</html>";
}
答案 2 :(得分:0)
我会按如下方式重新格式化您的示例。 (注意我使用不常见的缩进来关闭'}'):
if(usernameExists) {
usernameValid=false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText("This username already exists!");
}
else if(!username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25) {
usernameValid=false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText("<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>");
}
else if(/* other validation conditions here...*/) {
}
else {
Forms.Main.instructorForm.usernameField.setBackground(Color.white);
Forms.Main.instructorForm.usernameField.setToolTipText("");
}
答案 3 :(得分:0)
第一步:使用默认值。
usernameValid=false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText("This username already exists!");
if (!usernameExists) {
usernameValid=true;
Forms.Main.instructorForm.usernameField.setBackground(Color.white);
Forms.Main.instructorForm.usernameField.setToolTipText("");
if(!username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25) {
usernameValid=false;
Forms.Main.instructorForm.usernameField.setBackground(Color.red);
Forms.Main.instructorForm.usernameField.setToolTipText("<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>");
}
}
第二步:消除冗余信息
Forms.Main.instructorForm.usernameField.setToolTipText("This username already exists!");
if (!usernameExists) {
Forms.Main.instructorForm.usernameField.setToolTipText("");
if(!username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25) {
Forms.Main.instructorForm.usernameField.setToolTipText("<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>");
}
}
boolean usernameValid = Forms.Main.instructorForm.usernameField.getToolTipText().length() > 0);
Forms.Main.instructorForm.usernameField.setBackground(usernameValid ? Color.white : Color.red);
第三步:替换技术信息
String error = "This username already exists!";
if (!usernameExists) {
error = "";
if(!username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25) {
error = "<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>";
}
}
Forms.Main.instructorForm.usernameField.setToolTipText(error);
boolean usernameValid = error.length() > 0;
Forms.Main.instructorForm.usernameField.setBackground(usernameValid ? Color.white : Color.red);
第四步:减少巢穴
String error = usernameExists ? "This username already exists!" : "";
if("".equals(error) && (!username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25)) {
error = "<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>";
}
Forms.Main.instructorForm.usernameField.setToolTipText(error);
boolean usernameValid = "".equals(error) == false;
Forms.Main.instructorForm.usernameField.setBackground(usernameValid ? Color.white : Color.red);
第五步:制定更有意义的方法
boolean isUsernameValid(String username) {
return !username.matches("^[\\pL\\pN]+$") || username.length() < 7 || username.length() > 25);
}
boolean noErrors(error) {
return "".equals(error);
}
String error = usernameExists ? "This username already exists!" : "";
if(noErrors(error) && isUsernameValid(username) == false) {
error = "<html>Only letters & numbers allowed!<br> Length must be between 6 and 26</html>";
}
boolean usernameValid = noErrors(error);
Forms.Main.instructorForm.usernameField.setToolTipText(error);
Forms.Main.instructorForm.usernameField.setBackground(usernameValid ? Color.white : Color.red);