具有相同返回值的多个 IF
我见过一些如下所示的代码:
if (a) return false;
if (b) return false;
if (c) return false;
return true;
上述代码和
if (a || b || c) return false;
else return true;
一般情况下,处理此问题的首选情况是什么?也许在我的第二个例子中没有 else ?
编辑:似乎很多人都被我返回 true 或 false 并建议返回 !(a || b || c) 的事实误导了。这不是我想问的。想象一下,如果我不想返回 true 或 false,而是想返回“Yes”或“No”,或者 23423 或 3。
I've seen some code that looks like this:
if (a) return false;
if (b) return false;
if (c) return false;
return true;
Is there any difference in performance between the above and
if (a || b || c) return false;
else return true;
In general, what would be the preferred case to handle this? Maybe without the else in my second example?
EDIT: It seems a lot of people were mislead by the fact that I return true or false and suggest returning !(a || b || c). This wasn't what I wanted to ask. Imagine if instead of returning true or false I want to return "Yes" or "No", or 23423 or 3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两者的唯一优点是可读性,优化编译器为这两种情况生成几乎相同的代码是合理的。
如果 3 个测试很短,那么
return !(a||b||c);
是完全合理的,但是如果它们是长函数调用,那么您的第一个示例将更容易阅读。
The only advantage to either one would be readability, it would be reasonable for an optimizing compiler to generate nearly identical code for the two cases.
If the 3 tests are short then
return !(a||b||c);
is perfectly reasonableIf however they are long function calls then your first example would be easier to read.
这一切都取决于编译器如何编译代码。出于所有实际目的,它们是相同的。 (只要确保使用短路或“||”)
It all comes down to how the compiler compiles the code. For all practical purposes, they are identical. (As long as you make sure to use short-curcuited OR "||")
简而言之:代码没有显着差异,您可以通过将其编写为
return !(a || b || c);
If您的条件非常简单,例如
if (fata_is_invalid || login_failed)
那么您可以像您建议的那样将它们全部合并到一行中。有时您会看到非常庞大的条件,在这种情况下,最好将它们分成更小的块(通过多个 if 语句或通过重新格式化代码)。不管怎样,这只是一个可读性的事情 - 使用你喜欢的任何方法(或者你的“风格指南”提倡的任何方法)。
编译器非常友好,无论您在这些情况下编写什么,都会生成(几乎)相同的代码。
In short: there are no significant differences in the code, and you could further shorten your code by writing it as
return !(a || b || c);
If your conditions are really simple, for example
if (fata_is_invalid || login_failed)
then you could combine them all into one line like you suggested.Sometimes you will see conditions that are simply massive, and in that case it's preferable to split them up into smaller chunks (either through multiple if-statements or by re-formatting your code). Either way, it is just a readability thing - use whatever method you prefer (or whatever is advocated by your "style guide").
The compiler is super-sweet and will produce (almost) identical code for whatever you write in those cases.
&&, ||和 ?是 C++ 中的短路运算符,这意味着仅当满足以下条件时才计算第二个参数:首先是不确定表达式的值。
这意味着您可以简单地为示例代码编写:
&&, || and ? are short-circuit operators in C++, which means that the second argument is evaluated only if the first is not determining the value of the expression.
That means you could simply write, for your sample code:
分隔 if 并使用 ||运算符不一定相同!如果 a、b 或 c 中的任何一个是用户定义的类型并重载 ||或转换运算符到任何整数或指针类型,那么这两个构造可能会产生不同的结果!
Separate ifs and using || operator are not neccessarily identical! If any of a, b or c are user defined types and overload either || or conversion operator to any integer or pointer type then the two constructs may yield different results!