拆分阶段

什么时候需要进行拆分阶段的重构?

我们希望一段代码只执行一件事,当一段代码执行多件事时,我们就需要对代码进行阶段的拆分

 

例1:

重构前:

 

1 const orderData = orderString.split(/\s+/);
2 const productPrice = priceList[orderData[0].split("-")[1]]; 
3 const orderPrice = parseInt(orderData[1]) * productPrice;

 

重构后:

 1 const orderRecord = parseOrder(order);
 2 const orderPrice = price(orderRecord, priceList);
 3 
 4 // 解析字符串
 5 function parseOrder(aString) {
 6  const values = aString.split(/\s+/); 
 7  return ({
 8   productID: values[0].split("-")[1], 
 9   quantity: parseInt(values[1]),
10  });
11 }
12 
13 // 计算价格
14 function price(order, priceList) {
15  return order.quantity * priceList[order.productID];
16 }

 

例2:

重构前

 1 function priceOrder(product, quantity, shippingMethod) {
 2   // 计算基础价格
 3   const basePrice = product.basePrice * quantity;
 4 
 5   // 计算折扣
 6   const discount = 
 7      Math.max(quantity - product.discountThreshold, 0) 
 8      * product.basePrice  
 9      * product.discountRate;
10 
11   // 运费
12   const shippingPerCase = basePrice > shippingMethod.discountThreshold
13                     ? shippingMethod.discountedFee
14                     : shippingMethod.feePerCase;
15   const shippingCost = quantity * shippingPerCase;
16   
17   const price = basePrice - discount + shippingCost;
18   return price;
19 }

 

重构步骤:

  1. 抽离函数
  2. 提取公共数据,减少抽离函数的参数数量
  3. 减少不必要的局部变量

重构后:

 1 function priceOrder(product, quantity, shippingMethod) {
 2   const priceData = calculatePricingData(product, quantity);
 3   return applyShipping(priceData, shippingMethod);
 4 }
 5 
 6 // 提取公共数据
 7 function calculatePricingData(product, quantity) {
 8   const basePrice = product.basePrice * quantity;
 9   const discount =
10     Math.max(quantity - product.discountThreshold, 0) *
11     product.basePrice *
12     product.discountRate;
13   return {
14     basePrice,
15     quantity,
16     discount
17   };
18 }
19 
20 
21 function applyShipping(priceData, shippingMethod) {
22   const shippingPerCase =
23     priceData.basePrice > shippingMethod.discountThreshold
24       ? shippingMethod.discountedFee
25       : shippingMethod.feePerCase;
26   const shippingCost = priceData.quantity * shippingPerCase;
27   return priceData.basePrice - priceData.discount + shippingCost;
28 }

 

 

 

🤔️

applyShipping 这个函数直接执行了基础价格 - 折扣 + 运费的功能, 是否应该单独抽离计算运费的功能? 

 1 function priceOrder(product, quantity, shippingMethod) {
 2     const priceData = calculatePricingData(product, quantity);
 3     return applyShipping(priceData, calculateShippingCost(priceData, shippingMethod));
 4   }
 5   
 6   // 提取公共数据
 7   function calculatePricingData(product, quantity) {
 8     const basePrice = product.basePrice * quantity;
 9     const discount =
10       Math.max(quantity - product.discountThreshold, 0) *
11       product.basePrice *
12       product.discountRate;
13     return {
14       basePrice,
15       quantity,
16       discount
17     };
18   }
19   
20   
21   function applyShipping(priceData, shippingCost) {
22     return priceData.basePrice - priceData.discount + shippingCost;
23   }
24 
25 
26   function calculateShippingCost(priceData, shippingMethod) {
27     const shippingPerCase =
28     priceData.basePrice > shippingMethod.discountThreshold
29       ? shippingMethod.discountedFee
30       : shippingMethod.feePerCase;
31      return priceData.quantity * shippingPerCase;
32   }
33  

 

 

摘录来自: 马丁·福勒(Martin Fowler). “重构:改善既有代码的设计(第2版)。

  

 

 

posted @ 2020-04-27 23:59  yoyoluxi  阅读(124)  评论(0)    收藏  举报