Q:如何在C系列语言(C,C++,Java,C#)中避免以下代码中的过度缩进?
void f()
{
f1();
if(condition1)
{
f2();
if(condition2)
{
f3();
if(condition3)
{
f4();
if(condition4)
{
f5();
if(condition5)
{
f6();
}
}
}
}
}
}
A:使用 do {} while (false) 技巧
void f()
{
do {
f1();
if(!condition1) break;
f2();
if(!condition2) break;
f3();
if(!condition3) break;
f4();
if(!condition4) break;
f5();
if(!condition5) break;
f6();
} while(false);
//} while(0); //if we are using the C language
}