Skip to content

Instantly share code, notes, and snippets.

@RyanMarcus
Last active November 24, 2018 19:00
Show Gist options
  • Save RyanMarcus/0ac233b2243d049c67fc to your computer and use it in GitHub Desktop.
Save RyanMarcus/0ac233b2243d049c67fc to your computer and use it in GitHub Desktop.

The if collapse refactor

Whenever you have code like this:

if (condition) {
    if (confition2) {
        doSomeStuff();
    }
}

You can rewrite it to:

if (condition && condition2) {
    doSomeStuff();
}

The if/else refactor

Whenever you have code like this:

if (condition) {
    doSomeStuff();
    return value1;
} else {
    doSomeOtherStuff();
    return value2;
}

You can rewrite it to:

if (condition) {
    doSomeStuff();
    return value1;
}

doSomeOtherStuff();
return value2;

Combine the two

You can thus combine these two techniques. Whenever you have this:

if (condition) {
    if (condition2) {
        return value2;
    }
}

return value1;

You can rewrite as:

if (condition && condition2)
    return value2;

return value1;

#Use case

Notice how this complex code:

if (condition1) {
    if (condition2) {
        if (condition3) {
            return value2;
        } else {
            return value1;
        }
    } else {
        return value1;
    }
} else {
    return value1;
}

Can be greatly simplified:

if (condition1 && condition2 && condition3)
    return value2;

return value1;

if and for/while refactor

You can rewrite this:

for (int i = 0; i <= 100; i++) {
    if (i % 2 == 0) {
        System.out.println("Even: " + i);
    } else {
        System.out.println("Odd: " + i);
    }
}

as...

for (int i = 0; i <= 100; i++) {
    if (i % 2 == 0) {
        System.out.println("Even: " + i);
        continue;
    }

    System.out.println("Odd: " + i);
}

This is very useful for getting ride of "skip" clauses. For example, you can rewrite this:

for (int i = 0; i < 100; i++) {
    if (condition1) {
        if (condition2) {
            if (condition3) {
                doSomething();
            }
        }
    }
}

As:

for (int i = 0; i < 100; i++) {
    if (!(condition1 && condition2 && condition3))
        continue;

    doSomething();
}

The "bail" case

Often times in programming, we find ourselves checking a number of "bail" cases before running a method. For example, we might check if a string is non-empty, and that it starts with a "(". Using these techniques can make such code much more readable.

Consider this difference between this:

if (str.length() != 0) {
    if (str.charAt(0) == '(') {
        // do
        // a lot
        // of stuff
        // here.
        // do
        // a lot
        // of stuff
        // here.
        // do
        // a lot
        // of stuff
        // here.
        // do
        // a lot
        // of stuff
        // here.
        // do
        // a lot
        // of stuff
        // here.
        // do
        // a lot
        // of stuff
        // here.
        return something;
    }
}
return false;

And this:

if (str.length() == 0)
    return false;

if (str.charAt(0) != '(')
    return false;

// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
return something;

Now, when you are reading through the method, the first thing you see is that we "bail" on two different cases. Once these are "out of the way", you can think about the long complicated task ahead of you. When you reach the bottom of the complex task, you no longer have to think "huh, which if statement does this } close?"

@pitosalas
Copy link

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment