重构手法--对象内
重構手法–對象內
Extract Method(提煉函數)
將一段需要加注釋的函數放進一個獨立函數中,并讓函數名稱解釋該函數的用途,因為函數約簡短被復用的機會就越大。
重構前的代碼:
重構后的代碼:
void printOwing(){printBanner();printDetails(getOutstanding()) }void printBanner(){System.out.println("*************************");System.out.println("***** Customer Owes *****");System.out.println("*************************"); } double getOutstanding(){Enumeration e = _order.elements();double result = 0.0;while(e.hasMoreElements()){Order each = (Order)e.nextElement();result += each.getAmount();}return result; }void printDetails(double outstanding){System.out.println("name:"+_name);System.out.println("amount"+outstanding); }Inline Method(內聯函數)
過分提煉的函數,用函數體代替該函數更能讓代碼清晰
重構前的代碼:
重構后的代碼:
int getRating(){return (_numberOfLateDeliveries > 5) ? 2 : 1; }Inline Temp(內聯臨時變量)
只被一個簡單表達式賦值一次的臨時變量,不如把它替換為賦值的那個表達式本身
重構前的代碼:
重構后的代碼:
return (anOrder.basePrice() > 1000);Replace Temp with Query(以查詢取代臨時變量)
將表達式提煉到一個獨立函數中,去掉保存該表達式結果的臨時變量,為了該表達式可以被復用。
重構前的代碼:
重構后的代碼:
if(basePrice() > 1000)return basePrice() * 0.95; elsereturn basePrice() * 0.98;... double basePrice(){return _quantity * _itemPrice;; }Introduce Explaining Variable(引入解釋性變量)
當表達式比較復雜時,把表達式的結果放進臨時變量里,個人理解這個重構手法沒啥用
重構前的代碼:
利用該重構手法重構后的代碼:
final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1; final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1; final boolean wasResized = resize > 0;if(isMacOs && isIEBrowser && wasInitialized() && wasResized){//do something }個人感覺使用Extract Method(提煉函數)方法會更好:
if(isMacOs() && isIEBrowser() && wasInitialized() && wasResized()){//do something }boolean isMacOs(){return platform.toUpperCase().indexOf("MAC") > -1; } boolean isIEBrowser(){return browser.toUpperCase().indexOf("IE") > -1; } boolean wasResized(){return resize > 0; }Split Temporary Variable(分解臨時變量)
一個臨時變量只承擔一種意義的變量賦值,如果被多種含義的變量賦值多次就會影響代碼閱讀
重構前的代碼:
重構后的代碼:
final double temp = 2 * (_height + _width); System.out.println(temp); final double area = _height * _width; System.out.println(temp);Remove Assignments to Parameters(移除對參數的賦值)
用臨時變量取代直接對參數進行賦值的操作,為了代碼便于閱讀
重構前的代碼:
重構后的代碼:
int discount(int inputVal, int quantity, int yearToDate){int result = inputVal;if(inputVal > 50)result -= 2;return result; }Replace Method with Method Object(以函數對象取代函數)
如果一個比較大的函數里局部變量很多,導致無法直接對該函數進行Extract Method(提煉),這時候就可以把該函數放進一個單獨對象里再提煉
重構前的代碼:
重構后的代碼:
class Gamma{private final Account _account;private int inputVal;private int quantity;private int yearToDate;private int importantValue1;private int importantValue2;private int importantValue3;Gamma(Account source,int inputValArg, int quantityArg,int yearToDateArg){_account = source;inputVal = inputValArg;quantity = quantityArg;yearToDate = yearToDateArg;}int compute(){importantValue1 = (inputVal * quantity) + _account.delta();importantValue2 = (inputVal * yearToDate) + 100;importantThing();importantValue3 = importantValue2 * 7;return importantValue3 - 2 * importantValue1;}void importantThing(){if((yearToDate - importantValue) > 100){importantValue2 -= 20;}} }class Account{int gamma(int inputVal, int quantity, int yearToDate){return new Gamma(this, inputVal, quantity, yearToDate),compute();} }Substitute Algorithm(替換算法)
將某個算法替換為另一個更清晰的算法
重構前的代碼:
重構后的代碼:
String foundPerson(String[] people){List candidates = Arrays.asList(new String[] {"Don","John","Kent"});for(int i = 0;i < people.lenght; i++){if(candidates.contains(people[i])){return people[i];}}return ""; }總結
- 上一篇: ae效果英文版翻译对照表_AE自带特效中
- 下一篇: 帆软报表决策系统自定义登录界面 使用验证