jav*_*avs 3 language-agnostic control-flow
我最近遇到过这段代码:
do {
if ( ! checkSomething() )
break;
// some code
if ( ! checkSomeOtherThing() )
break;
// some other code
} while(false);
// some final code
Run Code Online (Sandbox Code Playgroud)
编写它的程序员写了一条评论"cleaner control flow".
在我看来,如果原始代码重构为其他东西,它看起来会更好.但是这句话有什么道理吗?这个构造有什么用吗?
我发现这更容易阅读,并产生相同的结果:
if ( checkSomething() )
{
// some code
if ( checkSomeOtherThing() )
{
// some other code
}
}
// some final code
Run Code Online (Sandbox Code Playgroud)
我认为do ... while通常很难遵循,但是将它用于循环之外的其他东西充其量只是误导.