简体   繁体   English

最佳做法-多个退货声明

[英]Best Practices - Multiple Return Statements

I've seen a similar question, but I wanted some additional clarity on when people feel it is not OK to use multiple return statements. 我已经看到了类似的问题,但是我想进一步澄清一下何时人们觉得使用多个return语句不合适。 (I believe this is a language agnostic question) (我相信这是一个与语言无关的问题)

I've always tried really hard to make sure there is always just one exit statement for a method. 我一直非常努力地确保一种方法始终只有一个exit语句。

Example: 例:

doSomething: function() {
  for (var i = 0; i < 10; i++) {
    if (objCars[i].color == "red") {
      this.carIndex = i;
      return true;
    }
  }
  return false;
}

Previously I would have re-written something like this: 以前,我会重新编写如下内容:

doSomething: function() {
  var bResult = false;
  for (var i = 0; i < 10; i++) {
    if (objCars[i].color == "red") {
      this.carIndex = i;
      bResult = true;
      break;
    }
  }

  return bResult;
}

Now I know there are cases where I might skip over the whole function/method entirely by putting a check and return right at the beginning.. but in cases like this.. is it better to stick to the single exit statement, or does it really not improve the readability of the code even in this scenario? 现在我知道在某些情况下,我可能会通过在开始时进行检查并直接返回来完全跳过整个函数/方法。.但是在这种情况下。即使在这种情况下,也真的不能提高代码的可读性吗?

Thanks, -Stephen 谢谢,-斯蒂芬

In that particular example, the first option is more efficient because it exits the loop the first time it finds a red car. 在该特定示例中,第一个选项效率更高,因为它在第一次找到红色汽车时退出循环。 I personally prefer a single return per function, especially if it's a long method. 我个人更喜欢每个函数一次返回,特别是如果这是一个长方法。

For this case, I would have done this: 对于这种情况,我会这样做:

doSomething: function() {
  var i = 0;
  while (i < 10 && objCars[i].color != "red") {
    this.carIndex = i;  
    i++
  }  
  return i < 10;  
}

Hope it helps 希望能帮助到你

I find the single return statement a lot clearer in this case. 在这种情况下,我发现单个return语句更加清晰。

You know right away that, because the function returns within the if block, it will only give out a single value. 您马上就知道,因为该函数在if块内返回,所以它只会给出一个值。 At a glance, it is hard to tell in the second example if the function is building a list of values to return or not. 乍一看,在第二个示例中很难判断该函数是否正在构建要返回的值列表。 This is specially true in untyped languages. 在非类型化语言中尤其如此。

You also have an increase in performance as you prevent your code from looping as soon as you find a valid value. 您还可以提高性能,因为一旦找到有效值,就可以防止代码循环。 In the first example, you might run through the for loop for a single iteration, while the single one will always run for all 10 iterations. 在第一个示例中,您可能会在for循环中运行一次迭代,而单个循环将始终在所有10次迭代中运行。 You could prevent this by changing the loop's condition, but this makes the code even more convulsed by accessing a variable in many unrelated places. 您可以通过更改循环的条件来防止这种情况,但是通过在许多不相关的地方访问变量,这会使代码更加混乱。

On an historical note, the reason why a lot of people insist on single return statement is to help with debugging. 从历史上看,许多人坚持使用单个return语句的原因是为了帮助调试。 Since the function only has one exit point, it is easy to add a breakpoint there to see the returned result. 由于该函数只有一个出口点,因此很容易在该出口处添加断点以查看返回的结果。 This is greatly mitigated in modern debuggers with the ability to see the return value of functions as well as simply storing the return value inside a variable before using it when calling the function. 在现代调试器中,可以看到函数的返回值,并在调用函数时在使用返回值之前将其简单地存储在变量中,从而大大缓解了这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM