2.Inline Method
User 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 moreThanFiveLateDeliveries(){
return numberofLateDeliveries>5;
}
After:
int getRating(){
return (numberofLateDeliveries>5)?2:1;
}
3 Inline Temp
Before:
double basePrice=anOrder.basePrice();
return (basePrice>1000);
After:
return (anOrder.basePrice()>1000);
4:Replace Temp with Query
Replace 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;
}