求助:一堆的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;
}
Originally, please don’t use your copycat
PMSUtil
, go directly toorg.apache.commons.lang.StringUtils
1. The whole can be simplified as:
2.
str.indexOf(s) == -1
can be simplified to!str.contains(s)
3. After using 2, the two innermost if statements:
can be written as:
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
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:
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
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