当我在我的代码上运行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".
在下面的例子中 - 假设返回值没有任何重要性 - 是否有理由更喜欢任何一种方法而不是另一种方法?
// 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)