DEV Community

Discussion on: Code Tip: Avoid unnecessary else statements

ernestasdob profile image
Ernestas • Edited on

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.

georgehanson profile image
George Hanson Author

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.