Code Bad Smell
Bad Code Smell
來自重構這本書

散發異味的程式碼並不會立刻的造成功能上的損壞,但會造成系統的慢性病。
重覆的程式碼 - Duplicated Code

太長的函式 - Long Method
DiningApplyBOImpl - queryOrderSummary
過大的類別 - Large Class

過長的參數列
Long Parameter List
- 通常比較少發生這種狀況,大部分都被 JsonString 包住了。
// 寫法一
public String queryBiweeklyBellSetting(String jsonString) throws Exception {
JSONObject bellObj=JSONObject.fromObject(jsonString);
Personal person=settingFeeEBO.queryPersonalByEmpid(bellObj.getString("empid"));
List<SettingFeeSummary> SettingFeeSummaryList =
this.querySettingBellByPeriod(person,
bellObj.getString("startDate"),bellObj.getString("endDate"));
return CommonUtils.getBeansJson(SettingFeeSummaryList, SettingFeeSummaryList.size());
}
SettingFeeBOImpl.java
// 寫法二
public List<SettingFeeSummary> queryBiweeklyBellSetting(String empId,
String startDate, String endDate) throws Exception {
Personal person=settingFeeEBO.queryPersonalByEmpid(empId);
List<SettingFeeSummary> SettingFeeSummaryList =
this.querySettingBellByPeriod(person, startDate, endDate);
return SettingFeeSummaryList;
}SettingFeeBOImpl.java
發散式變化
Divergent Chnage
-
多個變動會改在同一支檔案或是 Class

霰彈式修改
Shotgun Surgery
-
一個變動改多個地方

if (ouLocation == "TW")
$("#source").html(sourceContent);
else
$("#source").html(sourceContentS1);資料泥團
Data Clumps
-
總是綁在一起出現的資料
Transfer getTransfer(String empid, String sDate, String eDate) throws Exception;
List<Mealfee> queryMealfeeByStartEnd(String empid, String startDate, String endDate) throws Exception;
Integer queryMealFeeType(String empid, String startDate, String endDate) throws Exception;
List<Transfer> getTransfer_new(String applicant ,String sDate, String eDate) throws Exception;
List<BtripView> getBtripByWorkingDate(String empid, String startDate,
String endDate,String lampStart) throws Exception;String queryShiftType(String empid, String date) throws Exception;
String queryGuardShiftType(String empid, String date) throws Exception;
List<DayOfMealfee> queryDayOfMealfeeByWorkingDate (String empid,String workingDate) throws Exception ;
Double getOvertimeShift(String empid,String workDate) throws Exception;
Double getGuardShift(String empid,String workDate) throws Exception;基本型別偏執
Primitive Obession
-
勇敢的建立小型 Class
String startDate,String endDate
class Period
注釋
Comments

Gin 式注解

常常被註解掉的 Transactional

名人堂式註解
結論
- 來試試 IDE 提供的 Refactor 功能吧 !
- Extract Method..
- Extract Constants...
- Rename variable and method..
- 記得 Java 是一門 Compile Language
有些事就像路上
看到垃圾
不撿也不會死
但撿了會比較愛地球
Code Bad Smell
By Balicanta Yao
Code Bad Smell
- 135