单一收益表与多重收益表? [英] Single return statement vs multiple?

查看:82
本文介绍了单一收益表与多重收益表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经常有人告诉我,我不应该使用多个返回点,而应该只使用一个:

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

以该功能为例;

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?

我想第二个可以给IDE一些更好的机会来提示返回值吗?但是这里也存在性能问题吗?

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?

推荐答案

有什么理由不是优先选择一个优先事项吗?

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

有时,但这取决于具体代码.

Sometimes but that depends on concrete code.

我想第二个可以给IDE一个更好的机会来提示输入返回值?

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.

如今,良好的编码准则通常并不严格地如此,在较早的语言中,如果语言不够灵活,则采用严格的方法可能是有意义的(函数的最后一行必须是单个return命令).

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.

这篇关于单一收益表与多重收益表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆