本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42417535
在上一篇文章中介绍了“以查询取代临时变量“。本文将介绍“引入解释性变量”这种重构手法。
下面让我们来学习这种重构手法吧。
发现:你有一个复杂的表达式。
解决:将该复杂的表达式(或其中的部分)的结果放进一个临时变量,并以此变量名称来解释表达式用途。
//重构前
if((platform.toUpperCase().indexOf("MAC") > -1) &&
(browser.toUpperCase().indexOf("IE") > -1) &&
wasInitialized() && resize > 0)
{
//do something
} //重构后
final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
final boolean wasResize = resize > 0;
if(isMacOs && isIEBrowser && wasInitialized() && wasResize){
//do something
}在某些情况下,表达式可能非常的复杂以至于难以阅读。这样,临时变量可以帮助你将表达式分解为比较容易管理的形式。
在条件逻辑中,引入解释性变量就显得比较有价值:你可以用这项重构将每个子句提炼出来,以一个良好命名的临时变量来解释对应条件子句的意义。另一种可能的情况是,对于那些比较长的算法,可以运用临时变量来解释每一步运算的意义。
本文的重构手法是比较常见的手法之一,但是对其的使用又不是那么的多。因为一般情况下,我们都可以使用提炼函数来解释一段代码的意义。毕竟临时变量只有在它所处的那个函数中才有意义,局限性较大,函数则可以在对象的整个生命周期中都有用,并且可被其它对象使用。但是,当局部变量使用提炼函数难以进行时,就可以尝试使用引入解释性变量。
//重构前
double price(){
// 价格 = basePrice - quantity discount + shipping
return _quantity * _itemPrice -
Math.max(0, _quantity - 800) * _itemPrice * 0.15 +
Math.min(_quantity * _itemPrice * 0.25, 100);
} 这段代码还是比较简单,不过现在要让其更加容易理解一些。 double price(){
// 价格 = basePrice - quantity discount + shipping
final double basePrice = _quantity * _itemPrice;
return basePrice -
Math.max(0, _quantity - 800) * _itemPrice * 0.15 +
Math.min(basePrice * 0.25, 100);
} 然后,将批发折扣(quantity discount)的计算提炼出来,并将运算结果赋予临时变量。 double price(){
// 价格 = basePrice - quantity discount + shipping
final double basePrice = _quantity * _itemPrice;
final double quantityDiscount = Math.max(0, _quantity - 800) * _itemPrice * 0.15;
return basePrice -quantityDiscount+
Math.min(basePrice * 0.25, 100);
} 最后,再把搬运费(shipping)计算提炼出来,并将运算结果赋予临时变量。 //重构后
double price(){
// 价格 = basePrice - quantity discount + shipping
final double basePrice = _quantity * _itemPrice;
final double quantityDiscount = Math.max(0, _quantity - 800) * _itemPrice * 0.15;
final double shipping = Math.min(basePrice * 0.25, 100);
return basePrice - quantityDiscount + shipping;
}
//重构前
double price(){
// 价格 = basePrice - quantity discount + shipping
return _quantity * _itemPrice -
Math.max(0, _quantity - 800) * _itemPrice * 0.15 +
Math.min(_quantity * _itemPrice * 0.25, 100);
} 现在把底价计算提炼到一个独立的函数中。 double price(){
// 价格 = basePrice - quantity discount + shipping
return basePrice() -
Math.max(0, _quantity - 800) * _itemPrice * 0.15 +
Math.min(basePrice() * 0.25, 100);
}
private double basePrice(){
return _quantity * _itemPrice;
} 继续进行提炼,每次提炼一个新的函数。最后得到代码如下。 //重构后
double price(){
// 价格 = basePrice - quantity discount + shipping
return basePrice() - quantityDiscount() + shipping();
}
private double basePrice(){
return _quantity * _itemPrice;
}
private double shipping(){
return Math.min(basePrice() * 0.25, 100);
}
private double quantityDiscount(){
return Math.max(0, _quantity - 800) * _itemPrice * 0.15;
}原文地址:http://blog.csdn.net/pistolove/article/details/42417535