How to Perform Inline Method Refactoring Like a Pro


The opportunity to improve the code can come in many forms.

Split the method into smaller pieces.

Remove nested if statements.

But one of the small improvements you can make is to remove small methods where the inside code already communicates well what it does. To do that, use the Inline Method refactoring.

The Inline Method is a refactoring technique that replaces the small, usually one-line method with its body and removes the original method declaration. This refactoring reduces excess layers of abstraction and reduces call-stack complexity.

This article will show you how to use the Inline Method refactoring in C# to a cleaner code that contains fewer lines. And when you have fewer lines to maintain, your job becomes much easier.

What is Inline Method refactoring?

(Quick tip: you can also watch the following video on the Inline Method refactoring.)

Refactoring is an iterative technique used to improve the structure of application code without changing the observable behavior. You can also call it code restructuring.

When writing code, it’s not uncommon to create poorly structured code. Sometimes you need to do this to meet a deadline, but the resulting code is not ideal. You can often improve your software’s design by breaking it into smaller parts.

For example, rather than having a giant method with 300+ lines of code, it is often possible to extract that code into its own method using the Extract Method refactoring. Or, in some cases, a separate Method object.

But, sometimes, you end up with a short method in which the body has a clear intention. In other words, it’s easy to understand what the method does just by looking inside it.

When that happens, you should eliminate the method and use the method body instead of it. And that’s the purpose of the Inline Method refactoring.

How to perform Inline Method?

To perform the Inline Method refactoring, follow the next steps:

  1. Check that the subclasses don’t override the method. In that case, it might not be possible to perform this refactoring.
  2. Identify all method calls.
  3. Replace each call with the body of the method.
  4. Run tests after making the changes.
  5. Remove the method completely.

If you follow the above steps, you can remove the unneeded methods. But, if the method body is not straightforward, meaning it has multiple return points or some conditional logic where the method name explains the logic, you should probably not perform this refactoring.

Let’s see a C# code example and how to use this refactoring. It’s a code snippet from the Extract Method article, so check out how we ended up with this code. One new addition is the PrintGoodbyeMessage method.

public void PerformSearch()
{
    bool continueSearch = true;

    while (continueSearch)
    {
        SearchForSmartphones();

        continueSearch = ShouldContinueWithSearch(continueSearch);
    }

    PrintGoodbyeMessage();
}

private static void PrintGoodbyeMessage()
{
    Console.Write("Thanks for searching!");
}

As you can see, the PrintGoodbyeMessage is straightforward. It contains one line of code. Therefore, you can replace its method call with the method body.

public void PerformSearch()
{
    bool continueSearch = true;

    while (continueSearch)
    {
        SearchForSmartphones();

        continueSearch = ShouldContinueWithSearch(continueSearch);
    }

    Console.Write("Thanks for searching!");
}

private static void PrintGoodbyeMessage()
{
    Console.Write("Thanks for searching!");
}

After that, you can remove the method entirely. If you use Visual Studio, it can remove the method for you.

When to refactor using Inline Method?

Use the Inline Method in the following cases:

  1. The method body is simple – if the method body is straightforward, like in the above example, use this refactoring to remove it.
  2. Remove delegation – Sometimes, you move the method body to another method or another class. And you end up with the delegation that adds unnecessary indirection in the code. To simplify the code, eliminate the method.
  3. The base for further refactorings – Refactoring is not a linear process and you can refactor the same code in multiple ways. For example, use the Extract Method refactoring to extract different code blocks. However, if the refactoring path you took doesn’t provide you with better code as a result, you can use the Inline Method to move the code you previously extracted back to its original method and start again.

Conclusion

Refactoring is the process of modifying a piece of code to improve it, and it is an essential skill to hone throughout the course of your career.

The Inline Method, even if it’s less popular and exciting, has its place in the refactoring process. It can simplify the code and remove indirections in the code.

The resulting code will be simpler. And there will be less code to maintain.

Less code, fewer bugs.

Recent Posts

Here, take this before you go!

There is a lot of dangerous and dirty code out there.

Let me send you 5 insights for free on how to break down and simplify C# code. So you can make it more efficient and easier to write and maintain.

Whether you are a new or experienced developer, with these few tricks, you will confidently improve your code quality.

We respect your privacy. Unsubscribe at any time.