使用Eclipse重构代码——Replace Temp with Query
2013-04-10 23:31
495 查看
Motivation
局部变量会使代码难以被提炼,所以应该尽可能将他们替换为查询式。
另外,ReplaceTemp with Query也是Extract Method的必要步骤。
Mechanics
1 如果临时变量被赋值多次,则应用Split Temporary Variable,将他分割成多个变量,每个新的变量只被赋值一次。
2 对每一个临时变量,提取查询函数,然后替换。(如果查询函数有副作用,应再应用重构Separate Query from Modifler)
Eclipse refactor菜单中没有直接的对应项,
不过仍可以简单的组合[ExtractMethod] + [Inline] 完成对应的重构
// 选中_quantity * _itemPrice,应用[Extract Method]
double getPrice() {
final
int basePrice = basePrice();
final
double discountFactor;
if (basePrice > 1000)discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice *discountFactor;
}
private
int basePrice() {
return
_quantity * _itemPrice;
}
// 选中basePrice, 应用[Inline]
double getPrice() {
final
int basePrice =
basePrice();
final
double discountFactor;
if (basePrice() > 1000) discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice() * discountFactor;
}
private
int basePrice() {
return
_quantity * _itemPrice;
}
类似的,可以继续对discountFactor应用上面方法,仍旧只是鼠标右键+左键的问题。
相关代码包可以从 Here获取(上传的资源还未刷新,等刷新后更新地址)
局部变量会使代码难以被提炼,所以应该尽可能将他们替换为查询式。
另外,ReplaceTemp with Query也是Extract Method的必要步骤。
Mechanics
1 如果临时变量被赋值多次,则应用Split Temporary Variable,将他分割成多个变量,每个新的变量只被赋值一次。
2 对每一个临时变量,提取查询函数,然后替换。(如果查询函数有副作用,应再应用重构Separate Query from Modifler)
Eclipse refactor菜单中没有直接的对应项,
不过仍可以简单的组合[ExtractMethod] + [Inline] 完成对应的重构
// 原始代码,其中临时变量basePrice和discountFactor明显是可以应用ReplaceTemp with Query double getPrice() { final int basePrice = _quantity * _itemPrice; final double discountFactor; if (basePrice > 1000)discountFactor = 0.95; else discountFactor = 0.98; return basePrice *discountFactor; }
// 选中_quantity * _itemPrice,应用[Extract Method]
double getPrice() {
final
int basePrice = basePrice();
final
double discountFactor;
if (basePrice > 1000)discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice *discountFactor;
}
private
int basePrice() {
return
_quantity * _itemPrice;
}
// 选中basePrice, 应用[Inline]
double getPrice() {
final
int basePrice =
basePrice();
final
double discountFactor;
if (basePrice() > 1000) discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice() * discountFactor;
}
private
int basePrice() {
return
_quantity * _itemPrice;
}
类似的,可以继续对discountFactor应用上面方法,仍旧只是鼠标右键+左键的问题。
相关代码包可以从 Here获取(上传的资源还未刷新,等刷新后更新地址)
相关文章推荐
- 『重构--改善既有代码的设计』读书笔记----Replace Temp with Query
- refactoring笔记——代码重构的方法:Replace Temp with Query
- 重构改善既有代码设计--重构手法04:Replace Temp with Query (以查询取代临时变量)
- 重构手法04:Replace Temp with Query (以查询取代临时变量)
- 读书笔记-重构-Replace Temp with Query
- 重构:Replace Temp with Query
- 《重构》读书笔记 --Replace Temp with Query
- Replace Temp with Query 以查询取代临时变量
- Replace Temp with Query
- 重构改善既有代码设计--重构手法08:Replace Method with Method Object (以函数对象取代函数)
- 6.4replace temp with query
- 代码重构(一)-用函数对象代替函数(replace method with method object)
- 『重构--改善既有代码的设计』读书笔记----Replace Data Value with Object
- 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)
- 重构第8天:使用委托代替继承(Replace Inheritance with Delegation)
- Replace Temp With Query(以查询取代临时变量)
- Inline Temp & Replace Temp with Query
- 重构-代替类型代码几中方式的比较(Replace type code with ...)
- 重构代码 —— 函数即变量(Replace temp with Query)
- 6.4 Replace Temp with Query(以查询取代临时变量)