DEV Community

Theodore Karropoulos
Theodore Karropoulos

Posted on

3

Extract Method Refactoring

Extract Method

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)
        {
            // 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)
    {
        // Complex logic for applying discounts
        // ...

        return price;
    }

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

        return price;
    }
}
Enter fullscreen mode Exit fullscreen mode

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)
    {
        // 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)
    {
        // Complex logic for applying discounts
        // ...

        return price;
    }

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

        return price;
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay