Refactoring: Inline Method in IntelliJ IDEA

The idea of Inline Method Refactoring is simple – replace a method call with its contents. Still, it is really powerful. But to appreciate its power, you need to know its use cases.

Let’s understand why, when, and how to apply the Inline Method refactoring and how IntelliJ IDEA can help you get started.

Disruption in the flow of a method

When a method call disrupts the flow in your method rather than simplifying it, you can consider applying Inline Method. In the following code, the call to the method compareRatingWithMethodParameter() is inserting unnecessary indirection in the method scheduleSession().

The preceding code could be simplified by inlining the method compareRatingWithMethodParameter():

However, you don’t have to inline every method that defines just one line of code. The decision should depend on whether it is helping you to understand the flow in your method.

Code Migration, or using the latest Java language features

When migrating your code to a later Java version, you may want to inline a couple of methods so that you can apply an operation to code from multiple methods, collectively.
Though the code in the method getSortedListOfNames() looks readable, by inlining the methods extractNamesFromSpeakerList() and sortSpeakerNames(), you can refactor it to use Java streams instead of using the for loop and Collections.sort methods separately. Here’s the original code:

Here’s how you can inline the methods and refactor the resulting code:

Here’s the refactored code for you to compare with the original code:

Group of badly refactored methods

Often programmers end up applying refactoring practices poorly. For example, in the following code, the programmer seems to have applied ‘Extract Method Refactoring’ to each individual line of code.

Though it still looks readable, inlining a few of these methods and combining the code of one or more methods into another method, followed by (sensible) refactoring, can improve the intent of the code:

Let’s see how we can reorganize the code. Let’s inline a couple of methods and then extract code to methods, to make the code concise and readable.

Again, here’s the final code for you to compare with the initial code:

Inline Method improvements

(New in IntelliJ IDEA 2019.2)

IntelliJ IDEA 2019.2 includes significant improvements to the Inline Method refactoring.
A method might include multiple exit points by defining multiple return statements. When you inline such a method in IntelliJ IDEA 2019.2, it can be modified to define just one exit point in the form of a single return statement, to exit the method. IntelliJ IDEA refers to this as the ‘transforms to single exit point’ feature.

In the following code, when you inline the method isBreakRequiredBetweenSession(), the process introduces a variable result. The multiple return statements are replaced with an assignment to the variable result and just one return statement at the end:

Let’s inline the method isBreakRequiredBetweenSession():

As you can see, the modified code is simpler to follow – since it doesn’t have multiple exit points. Here’s the final code for you to compare with the initial code:

It is usual for developers to define multiple return statements in a method that return values from various control statements like if, for, and others. Usually such a method also includes a return statement at the end. Here’s an example, which defines multiple exit points in the method acceptSpeaker() highlighted using end-of-line comments:

When you inline the method acceptSession() in submitTalk(), IntelliJ IDEA will detect its multiple exit statements and modify the code accordingly:

Here’s the final code for you to compare (end-of-line comments added for your convenience):

Inline method with negation

(New in IntelliJ IDEA 2019.2)

With the enhancements in IntelliJ IDEA 2019.2, the Inline Method refactoring also supports negation at call site. In the following code, when you inline the method check() with negation in the method isValidName(), it won’t be modified to a ‘single exit point’:

Here’s how the inlined code is negated – notice the reversal of return values and condition (name.length() % 2 == 0) to (name.length() % 2 != 0):

Here’s the modified code for you to compare with the original code:

Summary

As developers, we refactor our code often. To refactor our code efficiently, we must know the use cases – why, when, and how to refactor it. IntelliJ IDEA plays a crucial role by automating the various refactoring options for you.

Happy coding and sensible refactoring!

This entry was posted in New Features and tagged , , , , . Bookmark the permalink.

4 Responses to Refactoring: Inline Method in IntelliJ IDEA

  1. Lukas M says:

    The example of single return statement refactoring it’s not good choice. For me, the code before refactoring where are 2 methods: acceptSession, submitTalk are more readable and also they are relatively flat (I mean there are only a few levels of indentation). But after refactoring to one method submitTalk there are 4 levels of indentation (else > if > for > if) and it doesn’t look good but more complex to me.

    But I understand, the purpose of this example is demonstration of given refactorings.
    However great job! I appreciate such articles about refactorings and I’m looking forward to upcoming posts.

  2. Evan Kellogg says:

    Although a lot of the later examples do remove multiple return statements, it instead replaces it multiple reassignments of a single variable.

    I would argue that this isn’t anymore readable than the original examples but only a slightly different way of representing the logic.

  3. Siddharth Dhawan says:

    Awesome

  4. Hans says:

    Hmm.. seems there’s something wrong with the HTML encoding. For example, in the first example:

    return rating < param

    instead of <

Leave a Reply

Your email address will not be published. Required fields are marked *