DEV Community

Cover image for Code Tip: Avoid unnecessary else statements

Code Tip: Avoid unnecessary else statements

George Hanson on January 23, 2019

When writing code it is easy to think in a logical way. With conditionals, people often write it as they would think it. If this condition is true ...
Collapse
 
exacs profile image
Carlos Saito

The statement it removes a level of indentation which makes the code more readable is too generic. Not always "less indentation" means "more readability". In some cases, the code is losing balance which could lead to less readability.

In your example without the "else" part, the code looks like the "if" condition being an "exceptional path" (early return) and the rest of the code the "normal path". Was it your intention?

Collapse
 
georgehanson profile image
George Hanson

It is not always the case, that is correct. But there are a number of cases where this issue can prop up and it can lead to more readability. Personally I prefer that if code doesn't need to be there then it shouldn't be there. It often allows you to read what the code is doing at a quicker pace.

In this specific example, the "if" is actually an exceptional path. Considering the context of Mononymous people.

Collapse
 
aleksikauppila profile image
Aleksi Kauppila

Good basic tip. Else-block is unnecessary in most cases.

Collapse
 
ernestasdob profile image
Ernestas • Edited

I have to agree with others. I don't like the multiple returns, unless it's the early return because of an error.

I would prefer something like this(I don't know what language it is, so just guessing here):

public function getName()
{
    var $name= $this->firstName;

    if ($this->lastName) {
        $name = $name.' '.$this->lastName;
    }

    return $name;
}

This clearly reads top to bottom, GetName:
Name is firstName.
If lastName exists, name is firstName plus lastName.
Return name.

Collapse
 
georgehanson profile image
George Hanson

It's PHP.

It depends on the developer. I don't think early returns are much of an issue personally, depending on the use case. If there is an error, I would expect an exception to be thrown instead.

Collapse
 
dvddpl profile image
Davide de Paolis

totally agree. less code to read, less indentation, less complexity. I am not a fan of multiple returns because they could easily be overlooked - especially "if block has a fair amount of code" as stated in other comments. But, if method is big and has a fair amount of code maybe it makes sense to split it into smaller bits and have the main method just deal with the necessary logic and return as soon as possibile.

Returning as soon as possible also means easier debugging because you dont have to go through the entire method to be sure your temporary variable is being modified by some other if-else down the chain.

Collapse
 
willglyles profile image
willglyles • Edited

Sometimes, yes. But generally, I don't agree. Omitting the else statement can be confusing to readers, especially if the if block has a fair amount of code (including other branches with return statements). I usually include the else statement for readability, except in trivial cases such as your example.

Collapse
 
georgehanson profile image
George Hanson

Like anything it all depends. There are cases when it is useful and others where it is not. In a simple case like this, I think it reduces visual debt. But it's all up to the individual developer.