Extract Method Refactoring

The Extract Method refactoring technique involves breaking down a section of code into a separate, named method. This technique is beneficial for improving code readability, promoting code reuse, and making the codebase more maintainable.

Simple Explanation:

Imagine you have a block of code that performs a specific task within a method. Instead of keeping all that code in one place, you can extract it into a separate method with a meaningful name. This is the Extract Method refactoring.

Example of Extract Method (Before):

Now for us to better understand this technic lets consider a method that calculates the total price of items in a shopping cart. Initially, our code might look like this:

public class ShoppingCart
{
    private List<Item> items;

    // ... other methods ...

    public decimal CalculateTotalPrice()
    {
        decimal totalPrice = 0;

        foreach (var item in items)
        {
            // Super Complex logic for calculating item price
            decimal itemPrice = item.Price * item.Quantity;

            // Additional logic, perhaps applying discounts or taxes
            itemPrice = ApplyDiscounts(itemPrice);
            itemPrice = ApplyTaxes(itemPrice);

            totalPrice += itemPrice;
        }

        return totalPrice;
    }

    private decimal ApplyDiscounts(decimal price)
    {
        // Super Complex logic for applying discounts
        // ...

        return price;
    }

    private decimal ApplyTaxes(decimal price)
    {
        // Super Complex logic for applying taxes
        // ...

        return price;
    }
}

Refactored Code using "Extract Method" (After):

As you can easily observe, the logic within the foreach loop appears extensive, making it challenging for someone to quickly grasp its purpose. To enhance clarity, we will refactor this by extracting the logic responsible for calculating the price of an individual item into a distinct method.

public class ShoppingCart
{
    private List<Item> items;

    // ... other methods ...

    public decimal CalculateTotalPrice()
    {
        decimal totalPrice = 0;

        foreach (var item in items)
        {
            // Extracted method for calculating item price
            decimal itemPrice = CalculateItemPrice(item);

            totalPrice += itemPrice;
        }

        return totalPrice;
    }

    private decimal CalculateItemPrice(Item item)
    {
        // Super Complex logic for calculating item price
        decimal itemPrice = item.Price * item.Quantity;

        // Additional logic, perhaps applying discounts or taxes
        itemPrice = ApplyDiscounts(itemPrice);
        itemPrice = ApplyTaxes(itemPrice);

        return itemPrice;
    }

    private decimal ApplyDiscounts(decimal price)
    {
        // Super Complex logic for applying discounts
        // ...

        return price;
    }

    private decimal ApplyTaxes(decimal price)
    {
        // Super Complex logic for applying taxes
        // ...

        return price;
    }
}

In the refactored code, the logic for calculating the price of an individual item has been encapsulated within the CalculateItemPrice method. This adjustment enhances the readability of the CalculateTotalPrice method and ensures a clear separation of concerns. Additionally, it opens up the possibility for potential reuse of the CalculateItemPrice logic elsewhere in the codebase.

Did you find this article valuable?

Support Theodoros Karropoulos by becoming a sponsor. Any amount is appreciated!