Refactoring Notes-Refactoring Methods (2)

xiaoxiao2021-03-06  19

2.Inline MethodUser Inline Method when someone is using too much indirection and it seems that every method does simple delegation to another mehtod.Before: int getRating () {return (moreThanFiveLateDeliveries ()) 2: 1;?}

Boolean MorethanfivelatedEndeliveries () {Return NumberOflateDeliveries> 5;

After: int getrating () {return (NumberOflatedLiveries> 5)? 2: 1;}

3 Inline Tempbefore: double baseprice = anorder.baseprice (); return (baseprice> 1000);

After: return (anORDER.BASEPRICE ()> 1000);

4: Replace Temp with QueryReplace Temp with Query often is a vital step before Extract Method.Mechanics: (1) Look for temporary variables that is assigned to once; (2) Extract the right-hand side of the assignment into a method (. 3) Use Replace Temp with query on the temp.Before: double getPrice () {int basePrice = quantity * itemPrice; double discountFactor; if (basePrice> 1000) discountFactor = 0.95; else discountFactor = 0.98; return basePrice * discountFactor;} After : Double getprice () {Return Baseprice () * discountfactor ();} int baseprice () {return quantity * itemprice;}

Double discountfactor () {if (baseprice ()> 1000) Return 0.95; Else Return 0.98;}

转载请注明原文地址:https://www.9cbs.com/read-43987.html

New Post(0)