Skip to content
Home » Extract Method

Extract Method

One of the most common techniques used in refactoring code is the Extract Method. The Extract Method technique is a refactoring process that turns a fragment of source code into a method, a procedure, or a function, reducing code duplication and enhancing readability.

The Extract Method technique has a straightforward goal: if you have a section of code within a method that performs a specific task, it should be turned into its own method. This newly formed method then gets a name that describes the task it performs.

Let’s look at this technique with code examples in three popular programming languages: Java, Python, and JavaScript.

Extract Method Refactoring in Java

Let’s start with Java. Assume we have the following code snippet:

public void printOwing() {
    printBanner();

    // print details
    System.out.println("name: " + _name);
    System.out.println("amount: " + getOutstanding());
}
Java

If we want to extract the details printing section, we can refactor the code as follows:

public void printOwing() {
    printBanner();
    printDetails(getOutstanding());
}

private void printDetails(double outstanding) {
    System.out.println("name: " + _name);
    System.out.println("amount: " + outstanding);
}
Java

Here, the ‘printDetails’ method is created to encapsulate the code responsible for printing the details.

Extract Method Refactoring in Python

Now let’s move on to Python. Given the following function:

def print_owing(name, outstanding):
    print_banner()

    # print details
    print(f"name: {name}")
    print(f"amount: {outstanding}")
Python

We can refactor this using the Extract Method technique like this:

def print_owing(name, outstanding):
    print_banner()
    print_details(name, outstanding)

def print_details(name, outstanding):
    print(f"name: {name}")
    print(f"amount: {outstanding}")
Python

Here, we have created a new function, print_details, which takes care of printing the name and outstanding amount.

Extract Method Refactoring in JavaScript

Finally, let’s look at JavaScript. Given the following code:

function printOwing(name, outstanding) {
    printBanner();

    // print details
    console.log(`name: ${name}`);
    console.log(`amount: ${outstanding}`);
}
JavaScript

After applying the Extract Method refactoring technique, it can be refactored as follows:

function printOwing(name, outstanding) {
    printBanner();
    printDetails(name, outstanding);
}

function printDetails(name, outstanding) {
    console.log(`name: ${name}`);
    console.log(`amount: ${outstanding}`);
}
JavaScript

Here, printDetails is the newly created function that encapsulates the task of printing the name and outstanding amount.

Conclusion

Extract Method is a simple yet powerful refactoring technique that can significantly improve the maintainability and readability of your code. Regardless of the programming language, the concept remains the same: If a chunk of code performs a particular task, extract it into a separate function or method. Doing so will not only make your code more readable and maintainable, but it will also make it easier to debug, test, and understand.