Getting rid of if statements

In our application we needed to implement a different discount algorithm for each country.

  • By default - the discount was in it's $ amount.
  • For orders that are shipped to France it had to be by % of line.
  • For orders that are shipped to Germany there is no Discount.

Previously this was implemented by several complicated expressions with lot's of rules.

Visible Expression:

internal Bool Exp_5() => _parent.Orders1.ShipCountry != "Germany"; 

Format (Picture) Expression:

internal Text Exp_4() => u.If(_parent.Orders1.ShipCountry == "France", "3%", "5.2CZ +$;"); 

Total Expression:

internal Number Exp_3() => Order_Details.UnitPrice * Order_Details.Quantity - u.If(_parent.Orders1.ShipCountry == "France", (Order_Details.UnitPrice * Order_Details.Quantity * Order_Details.Discount / 100), u.If(_parent.Orders1.ShipCountry == "Germany", 0, Order_Details.Discount)); 

After the refactoring

We refactored this into a strategy object:

namespace Northwind.Orders
{
    public class DefaultDiscountStrategy
    {
        public virtual bool ShouldShowDiscountColumn()
        {
            return true;
        }
        public virtual Text DiscountColumnFormat()
        {
            return "5.2CZ +$;";
        }
        public virtual Number GetDiscount(Models.Order_Details Order_Details)
        {
            return Order_Details.Discount;
        }
    }
    public class PercentDiscountStrategy : DefaultDiscountStrategy
    {
        public override Text DiscountColumnFormat()
        {
            return "3%";
        }
        public override Number GetDiscount(Order_Details Order_Details)
        {
            return (Order_Details.UnitPrice * Order_Details.Quantity * Order_Details.Discount / 100);
        }
    }
    public class NoDiscountStrategy : DefaultDiscountStrategy
    {
        public override bool ShouldShowDiscountColumn()
        {
            return false;
        }
        public override Number GetDiscount(Order_Details Order_Details)
        {
            return 0;
        }
    }
} 

We implemented a GetStrategy method:

DefaultDiscountStrategy GetDiscountStrategy()
{
     if (_parent.Orders1.ShipCountry == "France")
         return new PercentDiscountStrategy();
     else if (_parent.Orders1.ShipCountry == "Germany")
         return new NoDiscountStrategy();
     return new DefaultDiscountStrategy();
} 

And replaced the expressions:

Visible Expression:

-internal Bool Exp_5() => _parent.Orders1.ShipCountry != "Germany";
internal Bool Exp_5() => GetDiscountStrategy().ShouldShowDiscountColumn(); 

Format (Picture) Expression:

-internal Text Exp_4() => u.If(_parent.Orders1.ShipCountry == "France", "3%", "5.2CZ +$;");
internal Text Exp_4() => GetDiscountStrategy().DiscountColumnFormat(); 

Total Expression:

-internal Number Exp_3() => Order_Details.UnitPrice * Order_Details.Quantity - u.If(_parent.Orders1.ShipCountry == "France", (Order_Details.UnitPrice * Order_Details.Quantity * Order_Details.Discount / 100), u.If(_parent.Orders1.ShipCountry == "Germany", 0, Order_Details.Discount));
internal Number Exp_3() => Order_Details.Quantity * Order_Details.UnitPrice - GetDiscountStrategy().GetDiscount(Order_Details); 

Help us improve, Edit this page on GitHub
or email us at info@fireflymigration.com