11
回答
替代if else语句的方案?
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
觉得自己写代码,if else语句拉得很长很长,不使用滚动条都读不完,哪位有比较好的解决方案?
<无标签>
举报
共有11个答案 最后回答: 2年前

原来的语句:

private Map versionCheck(Map mapParam) {
    Map msg = new HashMap();
    if(!"".equals(PMSUtil.isNull(mapParam.get("IS_INTERNET")))){
      String checkVersion =SystemGlobals.getProperty("IS_CHECK");
      if("1".equals(checkVersion)){
          String versions =SystemGlobals.getProperty("VERSION");
          String version =PMSUtil.isNull(mapParam.get("VERSION"));
          String taxRegisterNo=PMSUtil.isNull(mapParam.get(""));
          String whiteList =SystemGlobals.getProperty("NO_CHECK_TAX_REGISTER_NO");
          if((","+whiteList+",").indexOf((","+taxRegisterNo+","))==-1){
              if((","+versions+",").indexOf(","+version+",")==-1){//找不到
                  msg.put("RESULT", "0001");
                  msg.put("MSG", "FAILD");
              }else{
                 msg.put("RESULT", ""0000"");
              }
          }else{
            msg.put("RESULT", ""0000"");
          }
      }else{
          msg.put("RESULT", ""0000"");
      }
    }else{
        msg.put("RESULT", ""0000"");
    }
    return msg;
}



修改后的代码:

private Map versionCheck(Map mapParam) {
        Map msg = new HashMap();
        msg.put("RESULT", "0000");

        String isInternet = (String) mapParam.get("IS_INTERNET");
        if (StringUtils.isBlank(isInternet)) {
            return msg;
        }

        String isCheck = SystemGlobals.getProperty("IS_CHECK");
        if (!StringUtils.equals(isCheck, "1")) {
            return msg;
        }

        String globalVersions = SystemGlobals.getProperty("VERSION");
        String currentVersion = (String) mapParam.get("VERSION");
        if (!StringUtils.contains("," + globalVersions + ",", "," + currentVersion + ",")) {
            return msg;
        }

        String whiteList = SystemGlobals.getProperty("NO_CHECK_TAX_REGISTER_NO");
        String taxRegisterNo = (String) mapParam.get("");
        if (!StringUtils.contains("," + whiteList + ",", "," + taxRegisterNo + ",")) {
            msg.put("RESULT", "0001");
            msg.put("MSG", "FAILD");
            return msg;
        }

        return Collections.emptyMap();
    }



这样的话代码可读性大大的提高,望采纳。

引用来自“浮躁的码农”的评论

原来的语句:

private Map versionCheck(Map mapParam) {
    Map msg = new HashMap();
    if(!"".equals(PMSUtil.isNull(mapParam.get("IS_INTERNET")))){
      String checkVersion =SystemGlobals.getProperty("IS_CHECK");
      if("1".equals(checkVersion)){
          String versions =SystemGlobals.getProperty("VERSION");
          String version =PMSUtil.isNull(mapParam.get("VERSION"));
          String taxRegisterNo=PMSUtil.isNull(mapParam.get(""));
          String whiteList =SystemGlobals.getProperty("NO_CHECK_TAX_REGISTER_NO");
          if((","+whiteList+",").indexOf((","+taxRegisterNo+","))==-1){
              if((","+versions+",").indexOf(","+version+",")==-1){//找不到
                  msg.put("RESULT", "0001");
                  msg.put("MSG", "FAILD");
              }else{
                 msg.put("RESULT", ""0000"");
              }
          }else{
            msg.put("RESULT", ""0000"");
          }
      }else{
          msg.put("RESULT", ""0000"");
      }
    }else{
        msg.put("RESULT", ""0000"");
    }
    return msg;
}



修改后的代码:

private Map versionCheck(Map mapParam) {
        Map msg = new HashMap();
        msg.put("RESULT", "0000");

        String isInternet = (String) mapParam.get("IS_INTERNET");
        if (StringUtils.isBlank(isInternet)) {
            return msg;
        }

        String isCheck = SystemGlobals.getProperty("IS_CHECK");
        if (!StringUtils.equals(isCheck, "1")) {
            return msg;
        }

        String globalVersions = SystemGlobals.getProperty("VERSION");
        String currentVersion = (String) mapParam.get("VERSION");
        if (!StringUtils.contains("," + globalVersions + ",", "," + currentVersion + ",")) {
            return msg;
        }

        String whiteList = SystemGlobals.getProperty("NO_CHECK_TAX_REGISTER_NO");
        String taxRegisterNo = (String) mapParam.get("");
        if (!StringUtils.contains("," + whiteList + ",", "," + taxRegisterNo + ",")) {
            msg.put("RESULT", "0001");
            msg.put("MSG", "FAILD");
            return msg;
        }

        return Collections.emptyMap();
    }



这样的话代码可读性大大的提高,望采纳。

很不错,可能不是最好的解决方案,但是最起码比第一种好多了

拆分成多个函数

使用&&短路表达

使用indexOf

使用状态机

使用call 函数名+array的手法

<?php
$checkList=array('id','name',……);
$errorList=array('id不存在','用户名小于4位');

//遍历$checkList, 执行  checkId,checkName……
foreach($checkList as $v){

  $checkFuncName='check'.$v;
  //@todo: 如果函数$checkFuncName存在,调用之
  //@todo: 如果结果false,则调用errorFuncName处理
}

//上面是并列的,类似的,如果if else 有层级关系,给$checkList更丰富含义并解析即可

?>



非常感谢大家的回答,大家推荐的几本书让我觉得自己还太嫩,这几本书都没听过,一定抽空看
顶部