重构-重新组织函数
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; }
相关文章推荐
- 《重构》读书笔记(六)——第六章 重新组织函数
- 重构—改善既有代码的设计006:重新组织你的函数(Composing Methods)
- PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数
- 代码重构-重新组织函数
- 重构系列3.重新组织函数
- 重构手法-重新组织函数
- 《重构改善既有代码的设计》之重构列表--重新组织函数(三)
- 重构-改善既有代码的设计:重新组织函数的九种方法(四)
- 重构-改善既有代码的设计:重新组织函数的九种方法(四)
- 重构-改善既有代码的设计:重新组织函数的九种方法(四)
- 重构手法(一)之重新组织函数
- 重新组织你的函数(读书摘要——重构改善既有代码的设计)
- 重构摘要6_重新组织函数
- PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数
- 《重构改善既有代码的设计》之重构列表--重新组织函数(一)
- PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数
- 【重构笔记02】重新组织函数
- PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数
- 重构方法之重新组织函数
- 重构——重新组织你的函数