过长参数列表 & 函数取代参数 & Inline Method

来源:互联网 发布:html页面嵌入php 编辑:程序博客网 时间:2024/06/05 07:42

例子:

源代码:

 public double getPrice() {      int basePrice = _quantity * _itemPrice;      int discountLevel;      if (_quantity > 100) discountLevel = 2;      else discountLevel = 1;      double finalPrice = discountedPrice (basePrice, discountLevel);      return finalPrice;  }   private double discountedPrice (int basePrice, int discountLevel) {      if (discountLevel == 2) return basePrice * 0.1;      else return basePrice * 0.05;  }
这里,参数discountLevel完全可以作处理。

首先,把所有计算discountLevel的代码提炼成一个独立的getDiscountLevel函数:因为从discountLevel的声明开始,到下面的循环语句,都是对这个变量作处理没有涉及到其它。

 public double getPrice() {int basePrice = _quantity * _itemPrice;int discountLevel = getDiscountLevel();double finalPrice = discountedPrice (basePrice, discountLevel);return finalPrice;}private int getDiscountLevel() {if (_quantity > 100) return 2;else return 1;}

然后把discountedPrice函数中,对于这个变量作为传入参数的引用点做替换:

private double discountedPrice (int basePrice, int discountLevel) {

if (getDiscountLevel() == 2) return basePrice * 0.1;

else return basePrice * 0.05;

}

然后去掉方法中的参数:

public double getPrice() {      int basePrice = _quantity * _itemPrice;      int discountLevel = getDiscountLevel();      double finalPrice = discountedPrice (basePrice);      return finalPrice;  }   private double discountedPrice (int basePrice) {      if (getDiscountLevel() == 2) return basePrice * 0.1;      else return basePrice * 0.05;  }

然后可以看到getPrice方法里没有对于discountlevel的引用了,可以直接去去掉。,也可以去掉其它临时变量basePrice:

public double getPrice() {      return discountedPrice ();  }   private double discountedPrice () {      if (getDiscountLevel() == 2) return getBasePrice() * 0.1;      else return getBasePrice() * 0.05;  }   private double getBasePrice() {      return _quantity * _itemPrice;  }

再往后,就是使用Inline Method,把中间的方法合并:

private double getPrice () {      if (getDiscountLevel() == 2) return getBasePrice() * 0.1;      else return getBasePrice() * 0.05;  }
这样就省略了中间的discountedPrice方法。


最后结果这样,但是中间步骤是必要的。


Inline Method:

简单例子:

 int getRating() {     return (moreThanFiveLateDeliveries()) ? 2 : 1; } boolean moreThanFiveLateDeliveries() {     return _numberOfLateDeliveries > 5; } int getRating() {     return (_numberOfLateDeliveries > 5) ? 2 : 1; }

本书经常以简短的函数表现动作意图,这样会使代码更清晰易读。但有时候你会遇到某些函数,其内部代码和函数名称同样清晰易读。也可能你重构了该函数,使得其内容和其名称变得同样清晰。果真如此,你就应该去掉这个函数,直接使用其中的代码。间接性可能带来帮助,但非必要的间接性总是让人不舒服。 另一种需要使用Inline Method 的情况是:你手上有一群组织不甚合理的函数。你可以将它们都inline到一个大型函数中,再从中提炼出组织合理的小型函数。Kent Beck发现,实施 Replace Method with Method Object 之前先这么做,往往可以获得不错的效果。你可以把你所要的函数(有着你要的行为)的所有调用对象的函数内容都inline到method object(函数对象)中。比起既要移动一个函数,又要移动它所调用的其他所有函数,「将大型函数作为单一整体来移动」会比较简单。 如果别人使用了太多间接层,使得系统中的所有函数都似乎只是对另一个函数的简单委托(delegation),造成我在这些委托动作之间晕头转向,那么我通常都会使用Inline Method。当然,间接层有其价值,但不是所有间接层都有价值。试着使用inlining,我可以找出那些有用的间接层,同时将那些无用的间接层去除。

对于内联函数的使用,看着简单,但是应该考验功力,比较常用的地方就是在提取函数时,去掉无用的间接层,就像上面那个最后的getPrice方法,最后的函数非常简洁。


看到这里,《重构》这本书其实并不是从头到尾都读了,因为用到的知识是相互穿插的。不过,阅读后才感觉到,如果说其它的书是注重java语言细节基础的话,这本书才真的是提升自己编写函数、类等的能力,使函数简洁优美。


怪不得《重构》是和《设计模式》并称的神书!!!!


原创粉丝点击