我个人并不反对提前退出,但我会提出第三种选择SingerOfTheFall供考虑.
好处:
>正常的代码流(即非错误)流畅地流过顶部
代码
>没有机会失败一个“东西”并传递另一个
“某事”无意中执行了一段代码
>您可以在代码块上强制执行范围;包括清除退出代码子块中使用的东西的范围
坏处:
>缩进可以加起来(虽然这可以通过打破来减轻
进入子功能)
>在编辑器中没有大括号匹配,可能很难将错误与失败的条件匹配
int foo()
{
int errorCode = 0;
if(!something) {
//100 lines of code
if(!something) {
//100 lines of code
if(!something) {
//100 lines of code
}
else {
errorCode = -1;
}
}
else {
errorCode = 11;
}
}
else {
errorCode = 1;
}
return errorCode;
}