替代if else语句的方案?

码农的故事 发布于 2015/07/06 09:36
阅读 1K+
收藏 0
觉得自己写代码,if else语句拉得很长很长,不使用滚动条都读不完,哪位有比较好的解决方案?
加载中
1
浮躁的码农
浮躁的码农

原来的语句:

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();
    }



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

0
浮躁的码农
浮躁的码农
什么语言?
0
码农的故事
码农的故事

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

原来的语句:

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();
    }



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

很不错,可能不是最好的解决方案,但是最起码比第一种好多了
0
高山流水情
高山流水情
建议读一下《代码重构》,会学习到很多代码技巧的。
0
alexgaoyh
alexgaoyh
Enum 代替 if else  
0
读一不二
垃圾代码的第一奥义就是:远看一坨。 
0
梅开源
梅开源

拆分成多个函数

使用&&短路表达

使用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更丰富含义并解析即可

?>



0
记住一座城
记住一座城
可以看一下<代码简洁之道>和<重构-改善既有代码的设计>
0
码农的故事
码农的故事
非常感谢大家的回答,大家推荐的几本书让我觉得自己还太嫩,这几本书都没听过,一定抽空看
返回顶部
顶部