重构代码
最近超级忙,之前有一部分写的代码写的和屎一样,结果是修改啊,下定决心的是因为写junit的时候报exceed的异常,没办法就只能修改咯
下面贴一下对比代码
超级不好的代码
1 if (headerExport.get(i) instanceof ColumnHeaderItem) { 2 columnHeaderItemAddList = new ArrayList<>(); 3 for (int j = 1; j < 11; j++) { 4 // ColumnHeaderItem型のある"obj"オブジェクトの初期化 5 ColumnHeaderItem obj1 = new ColumnHeaderItem(); 6 ColumnHeaderItem obj2 = new ColumnHeaderItem(); 7 ColumnHeaderItem obj3 = new ColumnHeaderItem(); 8 ColumnHeaderItem obj4 = new ColumnHeaderItem(); 9 ColumnHeaderItem obj5 = new ColumnHeaderItem(); 10 ColumnHeaderItem obj6 = new ColumnHeaderItem(); 11 ColumnHeaderItem obj7 = new ColumnHeaderItem(); 12 ColumnHeaderItem obj8 = new ColumnHeaderItem(); 13 ColumnHeaderItem obj9 = new ColumnHeaderItem(); 14 ColumnHeaderItem obj10 = new ColumnHeaderItem(); 15 16 try { 17 // headerExport.get(i)から"obj"へ各属性をコピーする。 18 BeanUtils.copyProperties(obj1, headerExport.get(i)); 19 BeanUtils.copyProperties(obj2, headerExport.get(i)); 20 BeanUtils.copyProperties(obj3, headerExport.get(i)); 21 BeanUtils.copyProperties(obj4, headerExport.get(i)); 22 BeanUtils.copyProperties(obj5, headerExport.get(i)); 23 BeanUtils.copyProperties(obj6, headerExport.get(i)); 24 BeanUtils.copyProperties(obj7, headerExport.get(i)); 25 BeanUtils.copyProperties(obj8, headerExport.get(i)); 26 BeanUtils.copyProperties(obj9, headerExport.get(i)); 27 BeanUtils.copyProperties(obj10, headerExport.get(i)); 28 29 } catch (IllegalAccessException e) { 30 throw new SystemException("プログラム不良", e); 31 } catch (InvocationTargetException e) { 32 throw new SystemException("プログラム不良", e); 33 } 34 // 項目名とVoPropertyの設定 35 StringBuffer locationStartAddressCode = new StringBuffer(); 36 locationStartAddressCode.append("施工場所起点コード"); 37 locationStartAddressCode.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 38 obj1.setItemName(locationStartAddressCode.toString()); 39 obj1.setVoProperty("locationStartAddressCode" + j); 40 columnHeaderItemAddList.add(obj1); 41 StringBuffer locationStartAddress = new StringBuffer(); 42 locationStartAddress.append("施工場所起点地域名"); 43 locationStartAddress.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 44 obj2.setItemName(locationStartAddress.toString()); 45 obj2.setVoProperty("locationStartAddress" + j); 46 columnHeaderItemAddList.add(obj2); 47 StringBuffer locationStartAddressTo = new StringBuffer(); 48 locationStartAddressTo.append("施工場所起点地先名"); 49 locationStartAddressTo.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 50 obj3.setItemName(locationStartAddressTo.toString()); 51 obj3.setVoProperty("locationStartAddressTo" + j); 52 columnHeaderItemAddList.add(obj3); 53 StringBuffer locationStartLatitude = new StringBuffer(); 54 locationStartLatitude.append("施工場所起点緯度"); 55 locationStartLatitude.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 56 obj4.setItemName(locationStartLatitude.toString()); 57 obj4.setVoProperty("locationStartLatitude" + j); 58 columnHeaderItemAddList.add(obj4); 59 StringBuffer locationStartLongitude = new StringBuffer(); 60 locationStartLongitude.append("施工場所起点経度"); 61 locationStartLongitude.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 62 obj5.setItemName(locationStartLongitude.toString()); 63 obj5.setVoProperty("locationStartLongitude" + j); 64 columnHeaderItemAddList.add(obj5); 65 StringBuffer locationEndAddressCode = new StringBuffer(); 66 locationEndAddressCode.append("施工場所終点コード"); 67 locationEndAddressCode.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 68 obj6.setItemName(locationEndAddressCode.toString()); 69 obj6.setVoProperty("locationEndAddressCode" + j); 70 columnHeaderItemAddList.add(obj6); 71 StringBuffer locationEndAddress = new StringBuffer(); 72 locationEndAddress.append("施工場所終点地域名"); 73 locationEndAddress.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 74 obj7.setItemName(locationEndAddress.toString()); 75 obj7.setVoProperty("locationEndAddress" + j); 76 columnHeaderItemAddList.add(obj7); 77 StringBuffer locationEndAddressTo = new StringBuffer(); 78 locationEndAddressTo.append("施工場所終点地先名"); 79 locationEndAddressTo.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 80 obj8.setItemName(locationEndAddressTo.toString()); 81 obj8.setVoProperty("locationEndAddressTo" + j); 82 columnHeaderItemAddList.add(obj8); 83 StringBuffer locationEndLatitude = new StringBuffer(); 84 locationEndLatitude.append("施工場所終点緯度"); 85 locationEndLatitude.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 86 obj9.setItemName(locationEndLatitude.toString()); 87 obj9.setVoProperty("locationEndLatitude" + j); 88 columnHeaderItemAddList.add(obj9); 89 StringBuffer locationEndLongitude = new StringBuffer(); 90 locationEndLongitude.append("施工場所終点経度"); 91 locationEndLongitude.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 92 obj10.setItemName(locationEndLongitude.toString()); 93 obj10.setVoProperty("locationEndLongitude" + j); 94 columnHeaderItemAddList.add(obj10); 95 } 96 itemAddLists.add(columnHeaderItemAddList); 97 continue; 98 }
修改之后的代码
1 if (headerExport.get(i) instanceof ColumnHeaderItem) { 2 columnHeaderItemAddList = new ArrayList<>(); 3 for (int j = 1; j < 11; j++) { 4 String title[] = {"起点コード", "起点地域名", "起点地先名", 5 "起点緯度", "起点経度", "終点コード", "終点地域名", "終点地先名", 6 "終点緯度", "終点経度"}; 7 String titleId[] = {"locationStartAddressCode", "locationStartAddress", 8 "locationStartAddressTo", "locationStartLatitude", "locationStartLongitude", 9 "locationEndAddressCode", "locationEndAddress", "locationEndAddressTo", 10 "locationEndLatitude", "locationEndLongitude"}; 11 for(int k = 0; k < title.length; k++){ 12 ColumnHeaderItem obj = new ColumnHeaderItem(); 13 BeanUtils.copyProperties(obj, headerExport.get(i)); 14 StringBuffer tempItemName = new StringBuffer(); 15 tempItemName.append(location); 16 tempItemName.append(title[k]); 17 tempItemName.append(CommonUtils.convertFullWidthNumber(String.valueOf(j))); 18 obj.setItemName(tempItemName.toString()); 19 obj.setVoProperty(titleId[k] + j); 20 columnHeaderItemAddList.add(obj); 21 } 22 } 23 itemAddLists.add(columnHeaderItemAddList); 24 continue; 25 }