“如果"与“开关"相比

2022-01-19 00:00:00 if-statement branch switch-statement php

可能重复:
是“else if”比 “switch() case” 更快?

我最近遇到很多情况,我的条件非常简单,需要分支应用程序流.完成我正在做的事情的最简单"方法只是一个普通的旧 if/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;
}

另外,更容易阅读:

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...

相关文章