【发布时间】:2026-02-14 09:55:02
【问题描述】:
做{...} while(0);
在我的编码中使用 do{}while(0); 是因为,我不想使用 long if else 嵌套条件语句。我最终在失败时中断并退出循环,并保证我的函数至少会被遍历 1 次。
现在,问题出在代码警告工具上,我在使用 do{...}while(0);
时收到警告嵌套 if(){} else{} 的用法可读性差,复杂度高。并让代码有死代码。
如果我排除 nested if(){} else{} 和 do{} while(0); ,我们是否留下了一些其他方式来制作代码可读性强,逻辑清晰;
if(status_of_funcA_ok != funcA())
{ //failure}
else if (status_of_funcB_ok != funcB())
{//failure}
else if (status_of_funcC_ok != funcC())
else
{//Great}
do{
if(status_of_funcA_ok != funcA())
break;
if (status_of_funcB_ok != funcB())
break;
if (status_of_funcC_ok != funcC())
break;
}while(0);
【问题讨论】:
-
*.com/questions/10720465/… 对问题的解释不多,现在已经接近了。 :(
-
你能举一个你的代码的一部分的具体例子吗?也许可以用不同的方式重写它
-
编译器可以警告他们喜欢的任何东西;在所有情况下都避免所有警告是不切实际的。 大多数警告很重要,但如果您知道自己在做什么(即,在这种情况下,您实际上比编译器更了解),您可以忽略它们。
-
我举了个例子!希望它会很容易理解。