重构手法--对象内
重構(gòu)手法–對象內(nèi)
Extract Method(提煉函數(shù))
將一段需要加注釋的函數(shù)放進(jìn)一個獨立函數(shù)中,并讓函數(shù)名稱解釋該函數(shù)的用途,因為函數(shù)約簡短被復(fù)用的機會就越大。
重構(gòu)前的代碼:
重構(gòu)后的代碼:
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(內(nèi)聯(lián)函數(shù))
過分提煉的函數(shù),用函數(shù)體代替該函數(shù)更能讓代碼清晰
重構(gòu)前的代碼:
重構(gòu)后的代碼:
int getRating(){return (_numberOfLateDeliveries > 5) ? 2 : 1; }Inline Temp(內(nèi)聯(lián)臨時變量)
只被一個簡單表達(dá)式賦值一次的臨時變量,不如把它替換為賦值的那個表達(dá)式本身
重構(gòu)前的代碼:
重構(gòu)后的代碼:
return (anOrder.basePrice() > 1000);Replace Temp with Query(以查詢?nèi)〈R時變量)
將表達(dá)式提煉到一個獨立函數(shù)中,去掉保存該表達(dá)式結(jié)果的臨時變量,為了該表達(dá)式可以被復(fù)用。
重構(gòu)前的代碼:
重構(gòu)后的代碼:
if(basePrice() > 1000)return basePrice() * 0.95; elsereturn basePrice() * 0.98;... double basePrice(){return _quantity * _itemPrice;; }Introduce Explaining Variable(引入解釋性變量)
當(dāng)表達(dá)式比較復(fù)雜時,把表達(dá)式的結(jié)果放進(jìn)臨時變量里,個人理解這個重構(gòu)手法沒啥用
重構(gòu)前的代碼:
利用該重構(gòu)手法重構(gòu)后的代碼:
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(提煉函數(shù))方法會更好:
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(分解臨時變量)
一個臨時變量只承擔(dān)一種意義的變量賦值,如果被多種含義的變量賦值多次就會影響代碼閱讀
重構(gòu)前的代碼:
重構(gòu)后的代碼:
final double temp = 2 * (_height + _width); System.out.println(temp); final double area = _height * _width; System.out.println(temp);Remove Assignments to Parameters(移除對參數(shù)的賦值)
用臨時變量取代直接對參數(shù)進(jìn)行賦值的操作,為了代碼便于閱讀
重構(gòu)前的代碼:
重構(gòu)后的代碼:
int discount(int inputVal, int quantity, int yearToDate){int result = inputVal;if(inputVal > 50)result -= 2;return result; }Replace Method with Method Object(以函數(shù)對象取代函數(shù))
如果一個比較大的函數(shù)里局部變量很多,導(dǎo)致無法直接對該函數(shù)進(jìn)行Extract Method(提煉),這時候就可以把該函數(shù)放進(jìn)一個單獨對象里再提煉
重構(gòu)前的代碼:
重構(gòu)后的代碼:
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(替換算法)
將某個算法替換為另一個更清晰的算法
重構(gòu)前的代碼:
重構(gòu)后的代碼:
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 ""; }總結(jié)
- 上一篇: ae效果英文版翻译对照表_AE自带特效中
- 下一篇: 帆软报表决策系统自定义登录界面 使用验证