java如何消除繁琐的if else 语句?
PHPz
PHPz 2017-04-17 11:29:22
0
9
810

求助:一堆的if else 嵌套

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;
}
PHPz
PHPz

学习是最好的投资!

reply all(9)
左手右手慢动作

Originally, please don’t use your copycat PMSUtil, go directly to org.apache.commons.lang.StringUtils

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

1. The whole can be simplified as:

if () {
    if () {
        if () {
            if () {
                msg.put("RESULT", "0001");
                msg.put("MSG", "FAILD");
                return msg;
            }
        }
    }
}

msg.put("RESULT", "0000");
return msg;

2. str.indexOf(s) == -1 can be simplified to !str.contains(s)
3. After using 2, the two innermost if statements:

if (c1) {
  if (c2) {
    ...
  }
}

can be written as:

if (c1 && c2) {
  ...
}
左手右手慢动作

IF simplification is actually a logical separation and integration of facts, and there is currently little room for language use. . .

Personal opinion, ignore the trolls

PHPzhong

I don’t know the specific logic of your program, but design patterns may be able to help you

大家讲道理

Integrate it logically. In this example, you can set the default value first:

msg.put("RESULT", ""0000"");

Then add the if statement, so that the rest of the else statements can be omitted.

迷茫

Analyze the changing part and the stable part of the code, and use the strategy pattern to separate the changing part of the code

左手右手慢动作

The poster can check the table driver method

黄舟

A few suggestions
1. Try not to use else but retrun as early as possible

void foo(){
    if(bad){
        //doSomthing
        return ;
    }
    //else <- 这个就可以不写了
    //real good logic;
}
  1. Use short circuit logic
if(s!= null && s.isValid() && s.then(doSomthing))
  1. Use functional expression
String result(String a){
    return (checkVersion(a) || isInternet(a)) ? "OK" :"000-default";
}
boolean checkVersion(String ver){
    return StringUtils.notBlank(ver) && ver.equals("...");
}
boolean isInternet(String ver){
    return StringUtils.notBlank(ver) && ver.equals("internet");
}

void main(){
//...
    someMap.put(result(String a));
//...
}


All logic should be split into as many and small functions as possible. These functions without side effects can be assembled into complex logic without losing readability. But this requires some experience.

小葫芦

Specific to the poster’s example, I think you should return as soon as possible instead of using else

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template