重构函数-Introduce Explaining Variable引入解释性变量五
创始人
2025-05-28 19:51:53
0次

重构函数-Introduce Explaining Variable引入解释性变量五

1.引入解释性变量

1.1.使用场景

在条件判断或者算法中出现表达式比较复杂不易阅读时,将表达式结果赋值给一个临时变量,以此变量名称来解释表达式用途。

引入解释性变量手法通常不会单独使用,它最大的价值在于能够为其他重构手法提供简化代码帮助他们能够在复杂的场景下顺利进行。
下面举一个应用场景

  • 在 提炼函数 需要花费更大工作量时。如果我要处理的是一个拥有大量局部变量的算法,那么使用 Extract Method 绝非易事。
  • 这种情况下我会使用引入解释性变量 帮助我理清代码,然后再考虑下一步该怎么办。
  • 搞清楚代码逻辑之后,我总是可以运用 查询取代临时变量把被我引入的那些解释性临时变量去掉。
  • 如果我最终使用 查询取代临时变量,那么被我引入的那些解释性临时变量也有其价值就是简化代码让业务逻辑更加清楚,降低重构难度。

1.2.局限性

引入解释性变量重构手法虽然能提升代码阅读性,但同时也引入了临时变量,毕竟临时变量只在它所处的那个函数中才有意义,局限性较大,函数则可以在对象的整个生命中都有用,并且可被其他对象使用。

但是当提炼函数重构方法难以进行时,就会使用引入解释性变量。因此这个重构手法使用的优先级较低,只有在不得已的情况下才会使用。

1.3.如何使用

  • 声明一个final临时变量,将待分解复杂表达式中的一部分动作的运算结果赋值给它。
  • 将表达式中的「运算结果」这一部分,替换为上述临时变量。
  • 如果被替换的这一部分在代码中重复出现,你可以每次一个,逐一替换。
  • 编译,测试。
  • 重复上述过程,处理表达式的其他部分。

1.4.应用示例

使用两种重构手法在同一个代码示例中进行重构,对比提炼函数 和 引入解释性变量 两种方式重构的效果。

1.引入解释性变量重构代码

   double price() {// price is base price - quantity discount + shippingreturn _quantity * _itemPrice -Math.max(0, _quantity - 500) * _itemPrice * 0.05 +Math.min(_quantity * _itemPrice * 0.1, 100.0);}

这段代码还算简单,不过我可以让它变得更容易理解。
首先我发现,底价(base price)等于数量(quantity)乘以单价(item price)。于是我把这一部分计算的结果放进 一个临时变量中

   double price() {// price is base price - quantity discount + shipping// 定义一个变量,将底价运算结果赋值给该变量。final double basePrice = _quantity * _itemPrice;// 将底价表达式替换为底价变量return basePrice -Math.max(0, _quantity - 500) * _itemPrice * 0.05 +Math.min(_quantity * _itemPrice * 0.1, 100.0);}

稍后也用上了「数量乘以单价」运算结果,所以我同样将它替换为basePrice临时变量

   double price() {// price is base price - quantity discount + shippingfinal double basePrice = _quantity * _itemPrice;return basePrice -Math.max(0, _quantity - 500) * _itemPrice * 0.05 +// 将底价表达式替换为底价变量Math.min(basePrice * 0.1, 100.0);}

然后,我将批发折扣(quantity discount)的计算提炼出来,将结果赋予临时变量quantityDiscount

  double price() {// price is base price - quantity discount + shippingfinal double basePrice = _quantity * _itemPrice;// 创建变量接收折扣表达式计算结果final double quantityDiscount = Math.max(0, _quantity - 500) * _itemPrice * 0.05;// 折扣表达式替换为折扣表达式return basePrice - quantityDiscount +Math.min(basePrice * 0.1, 100.0);}

最后,我再把运费(shipping)计算提炼出来,将运算结果赋予临时变量shipping。 同时我还可以删掉代码中的注释,因为现在代码已经可以完美表达自己的意义了

  double price() {final double basePrice = _quantity * _itemPrice;final double quantityDiscount = Math.max(0, _quantity - 500) * _itemPrice * 0.05;// 定义一个变量,接收运费表达式运算结果final double shipping = Math.min(basePrice * 0.1, 100.0);// 运费表达式替换为运费变量return basePrice - quantityDiscount + shipping;}

2.提炼函数重构代码

面对上述代码,我通常不会以临时变量来解释其动作意图,我更喜欢使用 Extract Method。让我们回到起点:

  double price() {// price is base price - quantity discount + shippingreturn _quantity * _itemPrice -Math.max(0, _quantity - 500) * _itemPrice * 0.05 +Math.min(_quantity * _itemPrice * 0.1, 100.0);}

这一次我把底价计算提炼到一个独立函数中

  double price() {// price is base price - quantity discount + shippingreturn basePrice() -Math.max(0, _quantity - 500) * _itemPrice * 0.05 +Math.min(basePrice() * 0.1, 100.0);}// 底价表达式提炼到函数private double basePrice() {return _quantity * _itemPrice;}

我继续我的提炼,每次提炼出一个新函数。最后得到下列代码:

   double price() {// 将所有表达式替换为函数return basePrice() - quantityDiscount() + shipping();}private double quantityDiscount() {return Math.max(0, _quantity - 500) * _itemPrice * 0.05;}private double shipping() {return Math.min(basePrice() * 0.1, 100.0);}private double basePrice() {return _quantity * _itemPrice;}

相关内容

热门资讯

WPK辅助妙计!wepoker... WPK辅助妙计!wepoker透视app下载,德州微扑克果然有辅助方法1、任何wepoker透视ap...
WPK四人学习!wepoker... WPK四人学习!wepoker有没有挂,德州微扑克真是真的是有透视挂技巧1、该软件可以轻松地帮助玩家...
WPK插件手筋!we-poke... WPK插件手筋!we-poker是什么软件,德州微扑克一贯存在有辅助器脚本1、这是跨平台的we-po...
WPK插件法门!wepoker... WPK插件法门!wepoker私人局辅助,德州微扑克都是是真的有透视教程1、上手简单,内置详细流程视...
WPK德州局指南!wepoke... WPK德州局指南!wepoker辅助工具,德州微扑克总是有透视脚本1、wepoker辅助工具有没有辅...
WPK辅助项目!wepoker... WPK辅助项目!wepoker有辅助工具吗,德州微扑克好像存在有辅助器方法1、wepoker有辅助工...
WPK透视步骤!wepoker... WPK透视步骤!wepoker如何设置透视,德州微扑克一直是真的有透视攻略wepoker如何设置透视...
WPK工具机巧!hhpoker... WPK工具机巧!hhpoker开辅助软件,德州微扑克确实真的有透视挂脚本1、不需要AI权限,帮助你快...
WPK脚本指引!wepoker... WPK脚本指引!wepoker脚本下载,德州微扑克确实是有透视挂方法1、wepoker脚本下载有没有...
WPK四人妙计!wepoker... WPK四人妙计!wepoker透视底牌脚本,德州微扑克确实真的是有开挂方法一、wepoker透视底牌...