引入解释性变量

开门见山

发现:你有一个复杂的表达式。

解决:将该复杂的表达式(或其中的部分)的结果放进一个临时变量,并以此变量名称来解释表达式用途。


//重构前
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
}

动机

在某些情况下,表达式可能非常的复杂以至于难以阅读。这样,临时变量可以帮助你将表达式分解为比较容易管理的形式。

在条件逻辑中,引入解释性变量就显得比较有价值:你可以用这项重构将每个子句提炼出来,以一个良好命名的临时变量来解释对应条件子句的意义。另一种可能的情况是,对于那些比较长的算法,可以运用临时变量来解释每一步运算的意义。

本文的重构手法是比较常见的手法之一,但是对其的使用又不是那么的多。因为一般情况下,我们都可以使用提炼函数来解释一段代码的意义。毕竟临时变量只有在它所处的那个函数中才有意义,局限性较大,函数则可以在对象的整个生命周期中都有用,并且可被其它对象使用。但是,当局部变量使用提炼函数难以进行时,就可以尝试使用引入解释性变量。

 

 

做法

(1)声明一个final型的临时变量,将待分解之复杂表达式中的一部分动作的运算结果赋值给它。 (2)将表达式中的“运算结果”这一部分,替换为上述的临时变量。(如果被替换的这一部分在代码中重复出现,可以每次一个,逐一进行替换) (3)编译,测试。 (4)重复上述过程,处理其它类似部分。

示例

我们从一个简单计算开始:

//重构前
double price(){
    // 价格 = basePrice - quantity discount + shipping
    return _quantity * _itemPrice -
            Math.max(0, _quantity - 800) * _itemPrice * 0.15 +
            Math.min(_quantity * _itemPrice * 0.25, 100);
}

这段代码还是比较简单,不过现在要让其更加容易理解一些。 首先发现底价(basePrice)等于数量(quantity)乘以单价(item price)。于是可以把这一部分的计算结果放进一个临时变量中,同时将Math.min()函数中参数进行同样替换。

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;
}

本文主要介绍了重构手法——引入解释性变量。该重构方法主要是在提炼函数需要花费更大工作量时才使用。比如你有一个拥有大量局部变量的算法,那么使用提炼函数绝非易事。这时候就可以使用本文的方法来整理代码,然后再考虑下一步怎么办;一旦搞清楚代码逻辑后,就可以运用以查询取代临时变量把中间引入的那些临时变量去掉。
我想你会比较喜欢提炼函数,因为对于同一对象的任何部分,都可以根据自己的需要取用这些提炼出来的函数。一开始会把这些新函数声明为private;如果其它对象也需要它们,就可以轻易释放这些函数的访问控制。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值