在代码审查中,一位资深开发人员评论了我代码中的一些嵌套问题。他建议我设置一个bool值,以便我永远不会有超过一层的嵌套。我认为我的代码更容易阅读,但希望听听其他开发人员的意见。哪种风格更好?他对嵌套的反感是否合理?
以下是一些简化的代码示例。
嵌套:
If(condition1)
{
If(condition2)
{
if(condition3)
{
return true;
}
else
{
log("condition3 failed");
}
else
{
log("condition2 failed")
}
}
else
{
log("condition1 failed")
}
return false;
或者
基于布尔值:
bool bRC = false;
bRC = (condition1);
if(brc)
{
bRC = (condition2);
}
else
{
log("condition1 failed");
return false;
}
if(bRC)
{
bRC = (condition3);
}
else
{
log("condition2 failed");
return false;
}
if(bRC)
{
return true;
}
else
{
log("condition3 failed");
return false;
}