重构函数调用-Replace Parameter with Method以函数取代参数八

重构函数调用-Replace Parameter with Method以函数取代参数八

1.以函数取代参数

1.1.使用场景

对象调用某个函数,并将所得结果作为参数,传递给另一个函数。而接受该参数的函数本身也能够调用前一个函数。
让参数接受者去除该项参数,并直接调用前一个函数。

如果函数可以通过其他途径获得参数值,那么它就不应该通过参数取得该值。过长的参数列会增加程序阅读者的理解难度,因此我们应该尽可能缩短参数列的长度。

缩减参数列的办法之一就是:看看参数接受端是否可以通过与调用端相同的计算来取得参数值。如果调用端通过其所属对象内部的另一个函数来计算参数,并在计算过程中未曾引用调用端的其他参数,那么你就应该可以将这个计算过程转移到被调用端内,从而去除该项参数。如果你所调用的函数隶属另一对象,而该对象拥有调用端所属对象的引用,前面所说的这些也同样适用。

1.2.如何做

  • 如果有必要,将参数的计算过程提炼到一个独立函数中。
  • 将函数本体内引用该参数的地方改为调用新建的函数。
  • 每次替换后,修改并测试。
  • 全部替换完成后,使用Remove Parameter (277)将该参数去掉。

1.3.示例

以下代码用于计算定单折扣价格。虽然这么低的折扣不大可能出现在现实生活中, 不过作为一个范例,我们暂不考虑这一点

  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)的代码提炼成为一个独立的 getDiscountLevel() 函数:

  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() 函数中对discountLevel 参数的所有引用点,替换为getDiscountLevel() 函数的调用:

  private double discountedPrice (int basePrice, int discountLevel) {
    
    
      if (getDiscountLevel() == 2) return basePrice * 0.1;
      else return basePrice * 0.05;
  }

此时我就可以使用Remove Parameter 去掉discountLevel 参数了

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

接下来可以将discountLevel 变量去除掉:

  public double getPrice() {
    
    
      int basePrice = _quantity * _itemPrice;
      double finalPrice = discountedPrice (basePrice);
      return finalPrice;
  }

现在,可以去掉其他非必要的参数和相应的临时变量。最后获得以下代码

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

最后我还可以针对discountedPrice() 函数使用Inline Method:

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

猜你喜欢

转载自blog.csdn.net/m0_38039437/article/details/129733696