Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single return statement vs multiple? [closed]

I have often been told that I should not use multiple return points, instead I should just use one:

take for example this function;

function data($item){
    switch($item){
        case 'one':
            return 1;
        case 'two':
            return 2;
        case 'three':
            return 3;
        case 'different_type':
            return 'Something Different';
        default:
            return false;
    }
}

Apparently a better way of writing this would be;

function data($item){
    $value = false;
    switch($item){
        case 'one':
            $value = 1;
            break;
        case 'two':
            $value = 2;
            break;
        case 'three':
            $value = 3;
            break;
        case 'different_type':
            $value =  'Something Different';
            break;
        default:
            $value = false;
    }
    return $value;
}

Is there any reason that is not a matter of preference for one over the other?

I imagine that the second one gives some IDE's a better chance at type hinting the return value? but are there performance issues here as well?

like image 481
Hailwood Avatar asked Oct 07 '12 11:10

Hailwood


People also ask

Should we have multiple return statements in a return statement?

All arguments in favor of multiple return statements go against the very idea of object-oriented programming. The code above has two return statements, and it is shorter than this one with a single return:

How many return statements can a method have in Java?

The answer may surprise you: In a pure object-oriented world, a method must have a single return statement and nothing else. Yes, just a return statement and that’s it. No other operators or statements.

Can you return from more than one place in a function?

In most cases “returning” from more than one place in a function or method is considered bad form and a potential source of errors. In a few cases it makes code cleaner and easier to follow. Generally we want a single point of entry into a function and likewise a single point of exit. The more complex the function the more this is desired.

What is the solution to multiple return statements in C++?

The solution to multiple return statements is to replace them with polymorphism have a single return after resolving the required implementation object. Moving from multiple returns to setting the return value in multiple places doesn't eliminate cyclomatic complexity, it only unifies the exit location.


2 Answers

It's just for readability. The IDE will do fine, and it won't affect performance so much you should worry about it. It's just that code with multiple return points is usually harder to read and debug.

But then again, it's a matter of taste as well, and depends very much on what you're used to.

like image 24
GolezTrol Avatar answered Sep 23 '22 11:09

GolezTrol


Is there any reason that is not a matter of preference for one over the other?

Sometimes but that depends on concrete code.

I imagine that the second one gives some IDE's a better chance at type hinting the return value?

No, that is normally not the case.

But are there performance issues here as well?

Early returns can shortcut longer paths in the code so can have a benefit.

A good coding guideline does normally not govern this strictly nowadays, in earlier times with languages not that flexible it might have made sense to keep a strict approach (last line of a function must be the single return command).

Nowadays it is known that it is more important to reduce Cyclomatic Complexity which is often the case with returning early. However, take this with a grain of salt, it's not that if you return early ever, that this is automatically the case.


As you're speaking about code, the first example should be in my eyes:

function data($item) {

    static $map = [
        'one'   => 1,
        'two'   => 2,
        'three' => 3,
        'different_type'
                => 'Something Different',
    ];

    # return @$map[$item] ?: false;
    return isset($map[$item])
        ? $map[$item] 
        : false
        ; 
}

But this would also run counter your example.

like image 115
hakre Avatar answered Sep 21 '22 11:09

hakre