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
- Look at the
ifbranch. It runsreturn n, which leaves the method on the spot. For a negativenthe method never reaches the code after it. - Ask when the code after the
ifruns. Only when the condition was false, because a true condition has already returned. That is exactly the situation theelsewas describing. - Remove the
elseand its braces. Keep the closing}of theifblock, and movereturn -nto the line after it, at the same indentation as theif.
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 theif. If you removed theelseand also forgot that last line, the compiler would report a type mismatch: foundUnit, requiredInt. - This only works because of the
return. In Scala, anifwithoutreturnthat sits before another expression doesn't end the method. If the first branch were justn, the value would be thrown away and the method would continue to the next line. - The expression form needs the
else. Scala code that avoidsreturnwritesif (n < 0) n else -n. There theelseis not redundant: the wholeif-elseis one expression, and both branches provide its value. The idea of droppingelseapplies 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
elsewhen theifblock 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
ifblock keeps its own closing}. Only theelse {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
ifblock ends withreturn, theelseafter it adds nothing. - The code after such an
ifruns only when the condition was false. - Removing the redundant
elsekeeps the behaviour and cuts nesting.
