Javascript if..else vs if(){return}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6931072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:49:44  来源:igfitidea点击:

if..else vs if(){return}

javascript

提问by knex

In the following example - given that the return value isn't of any importance - is there a reason to prefer either method over the other?

在下面的例子中——假设返回值并不重要——是否有理由偏爱其中一种方法而不是另一种方法?

// Method 1
function (a, b) {
  if (a == b){
    // I'm just interested in
    // the stuff happening here
  } else {
    // or here
  }
return true;
}

// Method 2
function (a, b) {
  if (a == b){
    // I'm just interested in
    // the stuff happening here
    return true;
  }
  // or here
  return true;
}

回答by Todd Moses

It seems that best practices (mostly by places I've worked for) is to set default values at the top of a method or function and only change those values if some condition occurs. Thus, the use of else is not needed so Method 2 is preferred.

似乎最佳实践(主要是我工作过的地方)是在方法或函数的顶部设置默认值,并且仅在发生某些情况时才更改这些值。因此,不需要使用 else,因此首选方法 2。

Since the example is JavaScript, special attention needs to be paid in regards to code size. So Method 2 would create less code for the same functionality, furthering its argument as the preferred.

由于示例是 JavaScript,因此需要特别注意代码大小。因此,方法 2 将为相同的功能创建更少的代码,进一步将其参数作为首选。

However, if you have more than 2 possible conditions, an else or else if cannot be avoided. However, most places I've worked prefer a Switch Case in these situations.

但是,如果您有 2 个以上的可能条件,则无法避免 else 或 else if。但是,在这些情况下,我工作过的大多数地方都喜欢使用 Switch Case。

回答by hspain

I would prefer Method 1 because it is less confusing to read. Also, less duplicate code.

我更喜欢方法 1,因为它不那么容易阅读。此外,减少重复代码。

回答by Abel

I would base my decision on clarity of code and readability, i.e.:

我将根据代码的清晰度和可读性做出决定,即:

  • Choose method 1 when you need to do more stuff in the block after the if-block.
  • Choose method 2 when you only need two blocks of code, it's then clearer to read
  • Choose method 1 again in cases where you explicitly think your readers wouldn't understand your cryptic code without the word "else"; this is common when the blocks become larger than a few lines.
  • 当您需要在 if 块之后的块中执行更多操作时,请选择方法 1。
  • 当你只需要两块代码时选择方法2,这样就更清晰了
  • 如果您明确认为您的读者在没有“else”一词的情况下无法理解您的神秘代码,请再次选择方法 1;当块变得大于几行时,这很常见。

Many of today's programmers consider less indentation easier to read and I agree. In which case general preference should go to using the second method.

今天的许多程序员认为更少的缩进更容易阅读,我同意。在这种情况下,一般首选使用第二种方法。

回答by adu

Readability here really depends on the role of the function.

这里的可读性实际上取决于函数的作用。

If this function will ALWAYS return true, then I would prefer the Method 1It is clear because it only returns in one place, and it is easy to see it will always be true.

如果这个函数总是返回true,那么我更喜欢方法1很清楚,因为它只在一个地方返回,而且很容易看到它总是为true。

In the above case, Method 2 is more confusing. It returns in multiple places, and is more confusing thusly. Consider a developer unnecessarily traversing possible branches and then seeing how they affect the return value. In this simple case, it is not as big of a deal, but when you get more elaborate conditionals, I would really avoid this approach.

在上述情况下,方法 2 更令人困惑。它在多个地方返回,因此更加混乱。考虑开发人员不必要地遍历可能的分支,然后查看它们如何影响返回值。在这种简单的情况下,这没什么大不了的,但是当你得到更复杂的条件时,我真的会避免这种方法。

I would only use Method 2 if you have very little code in the if block. Such as something that would deal with an edge case.

如果 if 块中的代码很少,我只会使用方法 2。例如可以处理边缘情况的东西。

Hope that helps.

希望有帮助。

回答by Samir Adel

I would recommend method 1 as it is more readable and self documented.

我会推荐方法 1,因为它更具可读性和自我记录。

回答by Ed Staub

Any modern browser's interpreter should eliminate any performance advantage in either direction.

任何现代浏览器的解释器都应该消除任何一个方向的性能优势。

There are a couple of reasons method 1 is preferable that haven't been mentioned yet. Having a single point of exit makes any future modifications that require an action common to both branches easier and less likely to be buggy (because the author missed the early return. Similarly, in some cases it makes debugging easier, by providing a common place to put a breakpoint or alert().

有几个原因尚未提及方法 1 更可取。有一个单一的退出点使得任何需要两个分支共同的操作的未来修改更容易并且不太可能出错(因为作者错过了早期返回。类似地,在某些情况下,通过提供一个共同的地方来使调试更容易放置断点或警报()。