首页 > 代码库 > if-else的优化举例

if-else的优化举例

共有部分:

 1             String bookFrom = null; 2             String sheetFrom = null; 3             String bookTo = null; 4             String sheetTo = null; 5             String matchColumnFrom = null; 6             String updateValueFrom = null; 7             String matchColumnTo = null; 8             String updateValueTo = null; 9 10             String[] splitTwoSheets = parameters.split(";");11             boolean flag1 = splitTwoSheets[0].contains("\"");12             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;

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