Most Useful IntelliJ Refactorings for Cleaner Code

IntelliJ’s refactorings are a quick way to improve your Java code. In this post I detail the refactoring actions I find most commonly useful. Even small refactorings can greatly improve readability and make larger refactorings easier and more obvious. Many of these actions have multiple options that are worth exploring. For a more in depth discussion of refactoring I recommend Martin Fowler’s Refactoring: Improving the Design of Existing Code.

Rename

I use rename frequently on variables and methods, and more occasionally on classes. It is a simple way to make your code more readable.

Before
String a = b.getUN();
After
String userName = userProfile.getUserName();

Move

Move is useful for organizing code into a more logical class structure and for moving classes around in packages. I find moving methods can be a great way to reduce the responsibilities of a class and simplify code.

Before
public class UserProfile {

    public static long getDaysSinceLastLogin(LoginData loginData) {
        ...
    }
}

public class LoginData {

    ...
}
After
public class UserProfile {

    ...
}

public class LoginData {

    public long getDaysSinceLastLogin() {
        ...
    }
}

Extract Variable

Extract Variable takes an expression and stores the result in a variable. I find this useful for eliminating duplicate code (such as calls to get() in maps) and also occasionally for breaking up a long unwieldy line of code. Extract Variable can also be used to give a name to an expression to make your intentions more clear (consider Extract Method in this case as well).

Before
String value = myMap.get(itemKey).getValue();
int otherValue = myMap.get(itemKey).getOtherValue();
After
Item item = myMap.get(itemKey);
String value = item.getValue();
int otherValue = item.getOtherValue();

Inline

Inline is the inverse of Extract Variable and Extract Method. I use inline when I’ve written a line of code that doesn’t do very much, such as a call to a getter method. It is often more readable to inline these calls so the reader doesn’t need to mentally keep track of an extra variable. Beware of inlining calls to methods that involve expensive computations or network communication.

Before
String value = item.getValue();
String lowerCaseValue = value.toLowerCase();
...
if (value.isEmpty()) {
    ...
}
After
String lowerCaseValue = item.getValue().toLowerCase();
...
if (item.getValue().isEmpty()) {
    ...
}

Extract Method

Extract Method is one of my favorite refactorings, it is simple to use and can work wonders on the clarity of your code. One useful application is to move behavior to another class by combining Extract Method with a Move Refactoring. Extract Method can also be used in place of Extract Variable, especially in cases where your motivation is to make intent clear by naming an expression.

Before
public void doCalculation() {
    if (size == 5 && (discount < 3 || day.equals("Tuesday"))) {
        ...
    }
}
After
public void doCalculation() {
    if (shouldAwardPoints()) {
        ...
    }
}

private boolean shouldAwardPoints() {
    return size == 5 && (discount < 3 || day.equals("Tuesday"))
}

Change Signature

Change Signature is great for updating method signatures and it is less work than changing the signature and manually fixing the resulting compilation errors. It can also be used to automatically create a new method overloading while preserving the old signature, although be wary of introducing too many overloadings as it can make code harder to follow.

Before
private long multiply(long input) {
    return input * 5;
}

public long get5x(long value) {
    return multiplyBy(value)
}
After
private long multiply(long input, long factor) {
    return input * factor;
}

public long get5x(long value) {
    return multiplyBy(value, 5)
}

These are the six IntelliJ refactorings that I use the most. Before I complete a code change I will look over my work and see if it can be quickly improved using these methods. This a great time to do refactoring since it is only time when code is not legacy code! IntelliJ has many other refactoring actions, see the documentation. To get better at cleaning up code in general I recommend the books below.




Leave a Reply

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