相关疑难解决方法(0)

反转"if"语句以减少嵌套

当我在我的代码上运行ReSharper时,例如:

    if (some condition)
    {
        Some code...            
    }
Run Code Online (Sandbox Code Playgroud)

ReSharper给了我上面的警告(反转"if"声明以减少嵌套),并提出了以下更正:

   if (!some condition) return;
   Some code...
Run Code Online (Sandbox Code Playgroud)

我想明白为什么那样更好.我一直认为在方法中间使用"返回"有问题,有点像"goto".

c# resharper

257
推荐指数
17
解决办法
11万
查看次数

if..else vs if(){return}

在下面的例子中 - 假设返回值没有任何重要性 - 是否有理由更喜欢任何一种方法而不是另一种方法?

// 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;
}
Run Code Online (Sandbox Code Playgroud)

javascript

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

javascript ×1

resharper ×1