"如果"与“切换”相对 [英] "if" versus "switch"

查看:148
本文介绍了"如果"与“切换”相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

是否则如果”快于'switch()案例” ?

我最近遇到很多情况我很简单条件和需要分支应用程序流程。完成我正在做的事情的最简单的方法只是一个普通的,如果 / elseif 声明:

I've encountered a lot of situations lately where I have very simple conditionals and need to branch application flow. The "simplest" means of accomplishing what I'm doing is just a plain old if/elseif statement:

if($value == "foo") {
    // ...
} elseif($value == "bar") {
    // ...
} elseif($value == "asdf" || $value == "qwerty") {
    // ...
}

...但我也在考虑这样的事情:

...but I'm also considering something like:

switch($value) {
    case "foo":
        // ...
        break;
    case "bar":
        // ...
        break;
    case "qwer":
    case "asdf":
        // ...
}

这似乎有点不太可读,但也许它的性能更高?但是,当条件中有越来越多的或表达式时,似乎switch语句更具可读性和实用性:

This seems a little less readable, but perhaps it's more performant? However, when there are more and more "or" expressions in the conditional, it seems that the switch statement is much more readable and useful:

switch($value) {
    case "foo":
        // ...
        break;
    case "bar":
    case "baz":
    case "sup":
        // ...
        break;
    case "abc":
    case "def":
    case "ghi":
        // ...
        break;
    case "qwer":
    case "asdf":
        // ...
}

我还看到了使用数组和函数分支代码流的选项:

I've also seen options where code flow is branched using arrays and functions:

function branch_xyz() {/* ... */}
function branch_abc() {/* ... */}
function branch_def() {/* ... */}

$branches = array(
    "xyz"=>"branch_xyz",
    "abc"=>"branch_abc",
    "def"=>"branch_def"
);
if(isset($branches[$value])) {
    $fname = $branches[$value];
    $fname();
}

这最后一个选项也可能具有可跨多个文件分发的好处,尽管它非常难看。

This last option also presumably has the benefit of being distributable across multiple files, though it is pretty ugly.

在性能,可读性和易用性方面,你认为哪一方面最有利可图?

Which do you feel has the most advantages with the fewest tradeoffs in terms of performance, readability, and ease of use?

推荐答案

就个人而言,我觉得这个开关更具可读性。原因如下:

Personally, I find the switch a lot more readable. Here's the reason:

if ($foo == 'bar') {
} elseif ($foo == 'baz') {
} elseif ($foo == 'buz') {
} elseif ($fou == 'haz') {
}

简明扼要,你可以很容易地看到旅行(无论是拼写错误还是诚实的差异)。但是通过一个开关,你可以隐含地知道它是什么意思:

Condensed like that, you can easily see the trip up (be it a typo, or a honest difference). But with a switch, you know implicitly what was meant:

switch ($foo) {
    case 'bar': 
        break;
    case 'baz': 
        break;
    case 'buz': 
        break;
    case 'haz': 
        break;
}

Plus,更容易阅读:

Plus, which is easier to read:

if ($foo == 'bar' || $foo == 'baz' || $foo == 'bat' || $foo == 'buz') {
}

case 'bar':
case 'baz':
case 'bat':
case 'buz':
    break;

从性能角度来看......好吧,不要担心性能问题。除非你在一个紧密的循环中做了几千个,否则你甚至无法区分(差异可能在微秒范围内,如果不是更低)。

From a performance standpoint... Well, don't worry about the performance. Unless you're doing a few thousand of them inside of a tight loop, you won't even be able to tell the difference (the difference will likely be in the micro-second range, if not lower).

选择您认为最具可读性的方法。这是重要的部分。不要尝试微观优化。请记住,过早优化是所有邪恶的根源 ...

Go for the method that you find the most readable. That's the important part. Don't try to micro-optimize. Remember, Premature Optimization Is The Root Of All Evil...

这篇关于"如果"与“切换”相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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