【问题标题】:Java: How to reduce the number of if-else statements where not possible to merge themJava:如何在无法合并的情况下减少 if-else 语句的数量
【发布时间】:2017-11-22 19:46:34
【问题描述】:

虽然我的代码可以正常运行,但是我的代码中有很多 if-else 语句,所以我想问一下是否可以减少这段代码中的 if-else 语句

if ( nightPost(timePicker1.getValue().toString(), timePicker4.getValue().toString()) ) {
    if ( !teToTime.isAfter(LocalTime.parse("08:30",format)) ) {
        textField4.setStyle("");
        if(getDifference(timeKA, timeKB)<121 && getDifference(timeKA, timeKB)>59) {
            timePicker3.setStyle("");
            if ( range(timePicker1.getValue().toString(), timePicker2.getValue().toString(),
                timePicker3.getValue().toString(), timePicker4.getValue().toString())) {
                timePicker2.setStyle("");
                timePicker3.setStyle("");
                if( increaseTxt.isBefore(LocalTime.parse("11:01",format)) )
                {
                    textField5.setStyle("");
                    return true;
                } else {
                    textField5.setStyle("-fx-border-color: red");
                    return false;
                }
            } else {
                timePicker2.setStyle("-fx-border-color: orange");
                return false;
            }                               
        }
        else {
            timePicker3.setStyle("-fx-border-color: red");
            return false;
        }
    } else {
        textField4.setStyle("-fx-border-color: red");
        return false;
    }
}
return false;

【问题讨论】:

  • 我想你应该在 codeReview.stackexchange 上问这个...
  • 我不太明白你的代码在做什么,但这里有一些通用的提示:提前失败,可能使用 switch/case 或映射(例如颜色),三元运算符,获取信息第一,...
  • 我的建议是不要在整个代码中混合使用英语和另一种语言。这可能是改进的第一步。这将有助于理解你的逻辑。
  • @tvelykyy 改变了它。
  • 我投票结束这个问题,因为它应该在 codereview.stackexchange.com 上。

标签: java if-statement switch-statement conditional


【解决方案1】:

将嵌套条件替换为 Guard Clauses

if (!nightPost(timePicker1.getValue().toString(), timePicker4.getValue().toString()) ) {
    return false;
}
if ( teToTime.isAfter(LocalTime.parse("08:30",format)) ) {
    textField4.setStyle("-fx-border-color: red");
    return false;
}
textField4.setStyle("");
if ( !(getDifference(timeKA, timeKB) < 121 && getDifference(timeKA, timeKB) > 59) ) {
    timePicker3.setStyle("-fx-border-color: red");
    return false;
}
timePicker3.setStyle("");
if ( !range(timePicker1.getValue().toString(), timePicker2.getValue().toString(), 
            timePicker3.getValue().toString(), timePicker4.getValue().toString()) ) 
{
    timePicker2.setStyle("-fx-border-color: orange");
    return false;
}
timePicker2.setStyle("");
timePicker3.setStyle("");
if ( !increaseTxt.isBefore(LocalTime.parse("11:01",format)) ) {
    textField5.setStyle("-fx-border-color: red");
    return false;
}
textField5.setStyle("");
return true;

【讨论】:

  • 感谢“保护条款”一词!我从来不知道合适的术语;在我上面的评论中,我将其称为“早期失败”。
  • 请不要简单地发布包含描述的链接。这些链接可能会使您的答案过时而死。有关答案中的链接,请参阅How to Answer
猜你喜欢
  • 2012-05-11
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多