我是靠谱客的博主 仁爱招牌,这篇文章主要介绍重构-重新组织函数Inline Method (内联函数) Inline Temp (内部临时变量) Replace Temp with Query (以查询取代临时变量),现在分享给大家,希望可以做个参考。
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;
}
最后
以上就是仁爱招牌最近收集整理的关于重构-重新组织函数Inline Method (内联函数) Inline Temp (内部临时变量) Replace Temp with Query (以查询取代临时变量)的全部内容,更多相关重构-重新组织函数Inline内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复