日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

重构手法--对象内

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 重构手法--对象内 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

重構手法–對象內

Extract Method(提煉函數)

將一段需要加注釋的函數放進一個獨立函數中,并讓函數名稱解釋該函數的用途,因為函數約簡短被復用的機會就越大。
重構前的代碼:

void printOwing(){Enumeration e = _order.elements();double outstanding = 0.0;//print bannerSystem.out.println("*************************");System.out.println("***** Customer Owes *****");System.out.println("*************************");//calculate outstandingwhile(e.hasMoreElements()){Order each = (Order)e.nextElement();outstanding += each.getAmount();}//print detailsSystem.out.println("name:"+_name);System.out.println("amount"+outstanding); }

重構后的代碼:

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 (moreThanFiveLateDeliveries()) ? 2 : 1; }boolean moreThanFiveLateDeliveries(){return _numberOfLateDeliveries > 5; }

重構后的代碼:

int getRating(){return (_numberOfLateDeliveries > 5) ? 2 : 1; }

Inline Temp(內聯臨時變量)

只被一個簡單表達式賦值一次的臨時變量,不如把它替換為賦值的那個表達式本身
重構前的代碼:

double basePrice = anOrder.basePrice(); return (basePrice > 1000)

重構后的代碼:

return (anOrder.basePrice() > 1000);

Replace Temp with Query(以查詢取代臨時變量)

將表達式提煉到一個獨立函數中,去掉保存該表達式結果的臨時變量,為了該表達式可以被復用。
重構前的代碼:

double basePrice = _quantity * _itemPrice; if(basePrice > 1000)return basePrice * 0.95; elsereturn basePrice * 0.98;

重構后的代碼:

if(basePrice() > 1000)return basePrice() * 0.95; elsereturn basePrice() * 0.98;... double basePrice(){return _quantity * _itemPrice;; }

Introduce Explaining Variable(引入解釋性變量)

當表達式比較復雜時,把表達式的結果放進臨時變量里,個人理解這個重構手法沒啥用
重構前的代碼:

if((platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) &&wasInitialized() && resize > 0){//do something }

利用該重構手法重構后的代碼:

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(分解臨時變量)

一個臨時變量只承擔一種意義的變量賦值,如果被多種含義的變量賦值多次就會影響代碼閱讀
重構前的代碼:

double temp = 2 * (_height + _width); System.out.println(temp); temp = _height * _width; System.out.println(temp);

重構后的代碼:

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){if(inputVal > 50)inputVal -= 2;return inputVal; }

重構后的代碼:

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 Account{int gamma(int inputVal, int quantity, int yearToDate){int importantValue1 = (inputVal * quantity) + delta();int importantValue2 = (inputVal * yearToDate) + 100;if((yearToDate - importantValue) > 100){importantValue2 -= 20;}int importantValue3 = importantValue2 * 7;return importantValue3 - 2 * importantValue1;} }

重構后的代碼:

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){for(int i = 0;i < people.lenght; i++){if(people[i].equals("Don")){return "Don";}if(people[i].equals("John")){return "John";}if(people[i].equals("Kent")){return "Kent";}}return ""; }

重構后的代碼:

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 ""; }

總結

以上是生活随笔為你收集整理的重构手法--对象内的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。