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.

See refactorings…

Use a method instead of if/else to conditionally assign to a variable

I’ve written code that assigns to a variable in each branch an if/else statement, but it always felt awkward. Here’s an example:

public Stuff getStuff() {
    // Other code...
    Thing foo;
    if (someValue == 5) {
        foo = getFooForFive();
    } else {
        foo = getOtherFoo();
    // Other code...

How to make this code better…