Start learning free

LESSON 74 OF 414 · CONDITIONAL STATEMENTS

Redundant Else Statement in Scala: Cleaner if Code

Lesson Read the full explanation

We will learn a simple trick to improve our code quality: removing redundant else statements.

Video Lesson:

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · CONDITIONAL STATEMENTS

    Redundant Else Statement in Scala, explained

    The idea

    An else block is redundant when the if branch before it always ends the method with return. Once the method has returned, nothing after the if can run for that input, so the code after the if already runs only when the condition was false. Wrapping it in else adds braces and an indentation level without changing what the program does.

    The starting code

    The method negative returns n when n is negative and -n otherwise, so its result is never positive:

    class Solution {
        def negative(n: Int): Int = {
            if (n < 0) {
                return n
            } else {
                return -n
            }
        }
    }
    

    This is correct. The task is to keep the behaviour and remove what isn't needed.

    Step by step

    1. Look at the if branch. It runs return n, which leaves the method on the spot. For a negative n the method never reaches the code after it.
    2. Ask when the code after the if runs. Only when the condition was false, because a true condition has already returned. That is exactly the situation the else was describing.
    3. Remove the else and its braces. Keep the closing } of the if block, and move return -n to the line after it, at the same indentation as the if.

    The solution in Scala

    class Solution {
        def negative(n: Int): Int = {
            if (n < 0) {
                return n
            }
            return -n
        }
    }
    

    Both versions return the same value for every input: -4 for 4, -4 for -4 and 0 for 0. The second is shorter, with one less level of nesting.

    Scala details

    • The compiler needs a value on every path. After the change, the last line, return -n, supplies the value for inputs that skip the if. If you removed the else and also forgot that last line, the compiler would report a type mismatch: found Unit, required Int.
    • This only works because of the return. In Scala, an if without return that sits before another expression doesn't end the method. If the first branch were just n, the value would be thrown away and the method would continue to the next line.
    • The expression form needs the else. Scala code that avoids return writes if (n < 0) n else -n. There the else is not redundant: the whole if-else is one expression, and both branches provide its value. The idea of dropping else applies to code that returns early.

    When to remove an else

    Remove it when the if block always exits the method. It is most useful for guard checks at the top of a method, where early returns handle special cases and the main logic follows without nesting:

    def describe(n: Int): String = {
        if (n < 0) {
            return "negative"
        }
        if (n == 0) {
            return "zero"
        }
        return "positive"
    }
    

    Keep the else when both branches have equal weight and reading them side by side makes the logic clearer, or when the if block doesn't return.

    Common mistakes

    • Removing the else when the if block does not return. If the block only prints, the code after it runs in both cases, which changes the behaviour.
    • Deleting the wrong brace. The if block keeps its own closing }. Only the else { and its matching } go.
    • Leaving the method without a final value. Every path must end in a value for the declared return type.

    Key takeaways

    • If an if block ends with return, the else after it adds nothing.
    • The code after such an if runs only when the condition was false.
    • Removing the redundant else keeps the behaviour and cuts nesting.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free