[英]Omit “> 0” in conditional?
I have recently inherited an old project to make some optimization and add new features. 我最近继承了一个旧项目,以进行一些优化并添加新功能。 In this project I have seen this type of condition all over the code:
在这个项目中,我在代码中看到了这种情况:
if (int_variable)
instead of 代替
if (int_variable > 0)
I have used the first option only with boolean
type of variables. 我只对
boolean
类型的变量使用了第一个选项。
Do you think the first option is a "correct" way to check if a number is positive? 您是否认为第一种选择是检查数字是否为正的“正确”方法?
负数也计算为true
,因此您应该使用if (int_variable > 0)
来检查数字是否为正。
否。第一种选择是检查数字是否为非零的正确方法。
Any non-zero value will be considered true
, so your version with >
is a bit sketchy if the variable truly is int
, ie signed. 任何非零值都将被视为
true
,因此,如果变量确实是int
,即带符号的,则带有>
的版本会有些粗略。 For clarity I prefer to make that explicit in many cases, ie write 为了清楚起见,我倾向于在很多情况下都将其明确,例如
if (int_variable != 0)
This is perhaps a bit convoluted (basically computing a Boolean where one can be automatically inferred), I would never use if (bool_variable == true)
for instance but I think the integer case makes the test clearer. 也许这有点令人费解(基本上是计算一个可以自动推断一个布尔值的布尔值),例如,我永远不会使用
if (bool_variable == true)
,但我认为整数情况可以使测试更加清晰。 Reading the two cases out loud works way better for the integer. 大声朗读这两种情况对于整数更有效。
if (int_variable)
has the same meaning as if (int_variable != 0)
. if (int_variable)
与if (int_variable != 0)
具有相同的含义。
This is not the same as if (int_variable > 0)
unless you otherwise know that the value will never be negative. 除非您另外知道该值永远不会为负,否则这与
if (int_variable > 0)
不同。
When using an integer value as a truth value in C, zero yields false
while any non-zero value yields true
. 在C中将整数值用作真值时,零将生成
false
而任何非零值都将生成true
。
In your case, assuming int_variable is signed, (int_variable)
is not equivalent to (int_variable > 0)
since int_variable
being negative will yield true
and false
, respectively. 在您的情况下,假设对int_variable进行了签名,则
(int_variable)
不等于(int_variable > 0)
因为int_variable
为负将分别产生true
和false
。
So 所以
if (int_variable) { … }
is not a correct way of checking whether int_variable
is positive. 不是检查
int_variable
是否为正的正确方法。
In C any expression which evaluates to integer 0
or a null pointer is considered false. 在C语言中,任何计算结果为整数
0
或空指针的表达式都被视为false。 Anything else is true. 还有其他的话。
So basically something like 所以基本上像
int i = ...;
if ( i )
Test if i
is 0. However, if i
is negative, the condition will also be true, so this is no replacement for i > 0
. 测试
i
是否为0。但是,如果i
为负,则条件也为true,因此不能替代i > 0
。 Unless i
is unsigned
. 除非
i
没有unsigned
。 Simply because they cannot be negative. 仅仅因为它们不能是负面的。 But your compiler may warn about such constructs as they often show a wrong concept.
但是您的编译器可能会警告此类构造,因为它们经常显示错误的概念。
Coming to coding style, if ( i )
is bad style if you are really comparing to the value 0
. 谈到编码风格,
if ( i )
是真的与值0
进行比较,则它是不好的风格。 You better compare explicitly: if ( i != 0 )
. 您最好明确地进行比较:
if ( i != 0 )
。 However, if i
contains some boolean result, you should use a "speaking" name (eg isalpha('A')
to use a ctypes.h
function instead of a variable) and do not compare. 但是,如果
i
包含一些布尔结果,则应使用“说”名称(例如isalpha('A')
使用ctypes.h
函数而不是变量),并且不要进行比较。 Compare the readbility: 比较可读性:
char ch = ...;
if ( isalpha(ch) )
if ( isalpha(ch) != 0 )
For the latter you have to think a second about the comparison - unnecessarily. 对于后者,您必须考虑一下比较-不必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.