Refactoring Day 13 : Extract Method Object

Today’s refactoring comes from Martin Fowlers list of refactorings. You can find his original article here with a brief description.

This is a more infrequent refactoring that I see myself using but it comes in handy at times. When trying to apply an Extract Method refactoring, and multiple methods are needing to be introduced, it is sometimes gets ugly because of multiple local variables that are being used within a method. Because of this reason, it is better to introduce an Extract Method Object refactoring and to segregate the logic required to perform the task.

   1: public class OrderLineItem
   2: {
   3:     public decimal Price { get; private set; }
   4: }
   5:  
   6: public class Order
   7: {
   8:     private IList<OrderLineItem> OrderLineItems { get; set; }
   9:     private IList<decimal> Discounts { get; set; }
  10:     private decimal Tax { get; set; }
  11:  
  12:     public decimal Calculate()
  13:     {
  14:         decimal subTotal = 0m;
  15:  
  16:         // Total up line items
  17:         foreach (OrderLineItem lineItem in OrderLineItems)
  18:         {
  19:             subTotal += lineItem.Price;
  20:         }
  21:  
  22:         // Subtract Discounts
  23:         foreach (decimal discount in Discounts)
  24:             subTotal -= discount;
  25:  
  26:         // Calculate Tax
  27:         decimal tax = subTotal * Tax;
  28:  
  29:         // Calculate GrandTotal
  30:         decimal grandTotal = subTotal + tax;
  31:  
  32:         return grandTotal;
  33:     }
  34: }

This entails passing a reference to the class that will be returning the computation to a new object that has the multiple methods via the constructor, or passing the individual parameters to the constructor of the method object. I will be showing the former here.

   1: public class OrderLineItem
   2: {
   3:     public decimal Price { get; private set;}
   4: }
   5:  
   6: public class Order
   7: {
   8:     public IEnumerable<OrderLineItem> OrderLineItems { get; private set;}
   9:     public IEnumerable<decimal> Discounts { get; private set; }
  10:     public decimal Tax { get; private set; }
  11:  
  12:     public decimal Calculate()
  13:     {
  14:         return new OrderCalculator(this).Calculate();
  15:     }
  16: }
  17:  
  18: public class OrderCalculator
  19: {
  20:     private decimal SubTotal { get; set;}
  21:     private IEnumerable<OrderLineItem> OrderLineItems { get; set; }
  22:     private IEnumerable<decimal> Discounts { get; set; }
  23:     private decimal Tax { get; set; }
  24:  
  25:     public OrderCalculator(Order order)
  26:     {
  27:         OrderLineItems = order.OrderLineItems;
  28:         Discounts = order.Discounts;
  29:         Tax = order.Tax;
  30:     }
  31:  
  32:     public decimal Calculate()
  33:     {
  34:         CalculateSubTotal();
  35:  
  36:         SubtractDiscounts();
  37:  
  38:         CalculateTax();
  39:  
  40:         return SubTotal;
  41:     }
  42:  
  43:     private void CalculateSubTotal()
  44:     {
  45:         // Total up line items
  46:         foreach (OrderLineItem lineItem in OrderLineItems)
  47:             SubTotal += lineItem.Price;
  48:     }
  49:  
  50:     private void SubtractDiscounts()
  51:     {
  52:         // Subtract Discounts
  53:         foreach (decimal discount in Discounts)
  54:             SubTotal -= discount;
  55:     }
  56:  
  57:     private void CalculateTax()
  58:     {
  59:         // Calculate Tax
  60:         SubTotal += SubTotal * Tax;
  61:     }
  62: }

This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Chambers

I am a Senior software developer from Palm Coast, Florida. An advocate of Domain Driven Design, Behavior Driven Development, creator of FluentMigrator and community activist. I am married to my beautiful wife Erin and am the proud father of two wonderful children. I currently reside at ACI, a local insurance industry/mortgage software company that excels in creating solutions using Agile methodologies.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.codebork.com/ Alastair Smith

    Good post. But you’ve introduced a circular dependency between Order and OrderCalculator. Would a static OrderCalculator class improve this?

  • http://www.adverseconditionals.com Harry M

    Are you going to do the “Remove useless comments with same name as one line method” refactoring? :)

  • http://www.lostechies.com/members/schambers/default.aspx schambers

    @Alastair

    Where is the circular dependency? An instance of Order is passed to the ctor of OrderCalculator.

  • http://ppetrov.wordpress.com/ Petar Petrov

    No not circular dependency but however I think Alastair Smith means that you don’t have something like IOrderCalculator to be injected in the order so the order is always dependent on the concrete class OrderCalculator. ( return new OrderCalculator(this).Calculate() )

  • http://www.lostechies.com/members/schambers/default.aspx schambers

    Oh ok. that makes more sense. Yeah, OrderCalculator would be dependent on Order. You could easily just introduce an interface however the OrderCalculator is naturally going to have pretty intimate knowledge of how an order works.

  • http://nmtloyzqpvww.com/ bcmfqqymif

    ApD6bf gijwqeouklwq, [url=http://iyjzoawykcjx.com/]iyjzoawykcjx[/url], [link=http://ddbxmnwseolm.com/]ddbxmnwseolm[/link], http://hdwrwakshhzh.com/

  • Maxi

    Please find a new way to style your code, it’s rendered really bad I have problems reading it at all