if-else的优化举例

时间:2022-09-27 12:16:31

共有部分:

             String bookFrom = null;
String sheetFrom = null;
String bookTo = null;
String sheetTo = null;
String matchColumnFrom = null;
String updateValueFrom = null;
String matchColumnTo = null;
String updateValueTo = null; String[] splitTwoSheets = parameters.split(";");
boolean flag1 = splitTwoSheets[0].contains("\"");
boolean flag2 = splitTwoSheets[1].contains("\"");

优化前:

       if(flag1 && flag2){
int lastIndexOfQuotes0 = splitTwoSheets[0].lastIndexOf("\"");
String Wb1AndSheet1 = splitTwoSheets[0].substring(0,lastIndexOfQuotes0).replaceAll("\"","");
String[] splitWb1AndSheet1 = Wb1AndSheet1.split("->");
separatedParameters[1] = splitWb1AndSheet1[0].trim();
separatedParameters[2] = splitWb1AndSheet1[1].trim();
int length0 = splitTwoSheets[0].length();
String stringColumns0 = splitTwoSheets[0].substring(lastIndexOfQuotes0,length0).replaceAll("\"","");
stringColumns0 = deleteExtraBlanks(stringColumns0);
String[] splitColumns0 = stringColumns0.trim().split(" ");
separatedParameters[5] = splitColumns0[0];
separatedParameters[6] = splitColumns0[1];
int lastIndexOfQuotes1 = splitTwoSheets[1].lastIndexOf("\"");
String Wb2AndSheet2 = splitTwoSheets[1].substring(0,lastIndexOfQuotes1).replaceAll("\"","");
String[] splitWb2AndSheet2 = Wb2AndSheet2.split("->");
separatedParameters[3] = splitWb2AndSheet2[0].trim();
separatedParameters[4] = splitWb2AndSheet2[1].trim();
int length1 = splitTwoSheets[1].length();
String stringColumns1 = splitTwoSheets[1].substring(lastIndexOfQuotes1,length1).replaceAll("\"","");
stringColumns1 = deleteExtraBlanks(stringColumns1);
String[] splitColumns1 = stringColumns1.trim().split(" ");
separatedParameters[7] = splitColumns1[0];
separatedParameters[8] = splitColumns1[1]; } else if(flag1 && !flag2){
int lastIndexOfQuotes0 = splitTwoSheets[0].lastIndexOf("\"");
String Wb1AndSheet1 = splitTwoSheets[0].substring(0,lastIndexOfQuotes0).replaceAll("\"","");
String[] splitWb1AndSheet1 = Wb1AndSheet1.split("->");
separatedParameters[1] = splitWb1AndSheet1[0].trim();
separatedParameters[2] = splitWb1AndSheet1[1].trim();
int length0 = splitTwoSheets[0].length();
String stringColumns0 = splitTwoSheets[0].substring(lastIndexOfQuotes0,length0).replaceAll("\"","");
stringColumns0 = deleteExtraBlanks(stringColumns0);
String[] splitColumns0 = stringColumns0.trim().split(" ");
separatedParameters[5] = splitColumns0[0];
separatedParameters[6] = splitColumns0[1]; separatedParameters[3] = "";
splitTwoSheets[1] = deleteExtraBlanks(splitTwoSheets[1]);
String[] splitSheetAndColumns = splitTwoSheets[1].trim().split(" ");
separatedParameters[4] = splitSheetAndColumns[0];
separatedParameters[7] = splitSheetAndColumns[1];
separatedParameters[8] = splitSheetAndColumns[2]; } else if(!flag1 && flag2){
int lastIndexOfQuotes1 = splitTwoSheets[1].lastIndexOf("\"");
String Wb2AndSheet2 = splitTwoSheets[1].substring(0,lastIndexOfQuotes1).replaceAll("\"","");
String[] splitWb2AndSheet2 = Wb2AndSheet2.split("->");
separatedParameters[3] = splitWb2AndSheet2[0].trim();
separatedParameters[4] = splitWb2AndSheet2[1].trim();
int length1 = splitTwoSheets[1].length();
String stringColumns1 = splitTwoSheets[1].substring(lastIndexOfQuotes1,length1).replaceAll("\"","");
stringColumns1 = deleteExtraBlanks(stringColumns1);
String[] splitColumns1 = stringColumns1.trim().split(" ");
separatedParameters[7] = splitColumns1[0];
separatedParameters[8] = splitColumns1[1]; separatedParameters[1] = "";
splitTwoSheets[0] = deleteExtraBlanks(splitTwoSheets[0]);
String[] splitSheetAndColumns = splitTwoSheets[0].trim().split(" ");
separatedParameters[2] = splitSheetAndColumns[0];
separatedParameters[5] = splitSheetAndColumns[1];
separatedParameters[6] = splitSheetAndColumns[2]; } else{
separatedParameters[1] = "";
splitTwoSheets[0] = deleteExtraBlanks(splitTwoSheets[0]);
String[] splitSheetAndColumns0 = splitTwoSheets[0].trim().split(" ");
separatedParameters[2] = splitSheetAndColumns0[0];
separatedParameters[5] = splitSheetAndColumns0[1];
separatedParameters[6] = splitSheetAndColumns0[2]; separatedParameters[3] = "";
splitTwoSheets[1] = deleteExtraBlanks(splitTwoSheets[1]);
String[] splitSheetAndColumns1 = splitTwoSheets[1].trim().split(" ");
separatedParameters[4] = splitSheetAndColumns1[0];
separatedParameters[7] = splitSheetAndColumns1[1];
separatedParameters[8] = splitSheetAndColumns1[2];
}

优化后:

            if(flag1){
bookFrom = splitTwoSheets[0].split("->")[0].replace("\"","").trim();
sheetFrom = splitTwoSheets[0].split("->")[1].split("\"")[0].trim();
String columnFrom1 = splitTwoSheets[0].split("->")[1].split("\"")[1];
columnFrom1 = deleteExtraBlanks(columnFrom1);
matchColumnFrom = columnFrom1.trim().split(" ")[0];
updateValueFrom = columnFrom1.trim().split(" ")[1];
} else{
bookFrom = "";
splitTwoSheets[0] = deleteExtraBlanks(splitTwoSheets[0]);
sheetFrom = splitTwoSheets[0].trim().split(" ")[0];
matchColumnFrom = splitTwoSheets[0].trim().split(" ")[1];
updateValueFrom = splitTwoSheets[0].trim().split(" ")[2];
}
if(flag2){
bookTo = splitTwoSheets[1].split("->")[0].replace("\"","").trim();
sheetTo = splitTwoSheets[1].split("->")[1].split("\"")[0].trim();
String columnTo = splitTwoSheets[1].split("->")[1].split("\"")[1];
columnTo = deleteExtraBlanks(columnTo);
matchColumnTo = columnTo.trim().split(" ")[0];
updateValueTo = columnTo.trim().split(" ")[1]; } else{
bookTo = "";
splitTwoSheets[1] = deleteExtraBlanks(splitTwoSheets[1]);
sheetTo = splitTwoSheets[1].trim().split(" ")[0];
matchColumnTo = splitTwoSheets[1].trim().split(" ")[1];
updateValueTo = splitTwoSheets[1].trim().split(" ")[2]; }
separatedParameters[1] = bookFrom;
separatedParameters[2] = sheetFrom;
separatedParameters[3] = bookTo;
separatedParameters[4] = sheetTo;
separatedParameters[5] = matchColumnFrom;
separatedParameters[6] = updateValueFrom;
separatedParameters[7] = matchColumnTo;
separatedParameters[8] = updateValueTo;

优化后,既防止出错、对应不上的问题,而且使代码更加简洁