您的位置:首页 > 其它

重构-重新组织函数

2014-08-15 17:40 260 查看

Inline Method (内联函数)

Before

int getRating() {
return (moreThanFive()) ? 2: 1;
}
boolean moreThanFive() {
return NUMBER > 5;
}


After

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



Inline Temp (内部临时变量)

Before

double basePrice = getBasePrice();
return (basePrice > 1000);

After

return getBasePrice() > 1000;



Replace Temp with Query (以查询取代临时变量)

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 getBasePrice() * getDiscountFactor();
}

private int getBasePrice(){
return quantity * itemPrice;
}

private double getDiscountFactor () {
return getBasePrice() > 1000 ? 0.95 : 0.98;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: