我更喜欢选项A
bool a, b, c;
if( a && b && c )
{
//This is neat & readable
}
如果确实有特别长的变量/方法条件,则可以换行
if( VeryLongConditionMethod(a) &&
VeryLongConditionMethod(b) &&
VeryLongConditionMethod(c))
{
//This is still readable
}
如果它们更加复杂,那么我将考虑在if语句之外分别执行条件方法
bool aa = FirstVeryLongConditionMethod(a) && SecondVeryLongConditionMethod(a);
bool bb = FirstVeryLongConditionMethod(b) && SecondVeryLongConditionMethod(b);
bool cc = FirstVeryLongConditionMethod(c) && SecondVeryLongConditionMethod(c);
if( aa && bb && cc)
{
//This is again neat & readable
//although you probably need to sanity check your method names ;)
}
恕我直言,选择“ B”的唯一原因是,如果您else
要针对每种情况运行单独的功能。
例如
if( a )
{
if( b )
{
}
else
{
//Do Something Else B
}
}
else
{
//Do Something Else A
}