Single return statement vs multiple? [closed]

一个人想着一个人 提交于 2020-01-03 12:44:11

问题


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?


回答1:


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.




回答2:


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.




回答3:


Compilers these days are smart enough to compile this kind of code in an efficient way and it might be the case that both of these translate to same set of instruction , personally i think it is easier to understand since the return is at the end of the function.




回答4:


There should not be a performance issue with the first method, because once your function returns something, rest of the lines in that function are not executed anymore. It should be readability more than anything else.

Edited: In fact, the second iteration should technically use more resource because it has to assign the value to the variable, and then return that variable,but that difference is simply negligible!




回答5:


I would prefer the approach with one return point any day. The reason for that is that it is much easier to understand the code. This may seem trivial, but I am sure we have all tried reading someone else's spaghetti code. This makes it easier to understand what is happening throughout the method, especially in more advanced cases where the code paths can be more challenging to understand. Multiple return points is also harder to debug I would say. However, for very simple methods, I think it can be OK to have two return points, but for more complicated scenarios, I would do my best to avoid it.

Regarding performance, I think the difference is minimal in most cases (although it depends how much code has to be executed after what would have been a return point). I think the advantages with better readability would almost always outweigh the performance aspect since there are so many other areas where performance could be enhanced by a much greater margin than little tweaks like these. If, for example, you have database, web service or I/O calls after what could have been a return point, then the performance impact would be greater. In this case, returning "early" would improve performance significantly, but then you could always make sure that these calls are never made in given scenarios, thus avoiding the multiple return points. However, if you really want to optimize low level, then you will end up with more instructions with a single return point. But as I said, there are more important things to optimize I would say.



来源:https://stackoverflow.com/questions/12768399/single-return-statement-vs-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!