测试值在条件表达式中的位置 [英] Position of test values in conditional expressions

查看:68
本文介绍了测试值在条件表达式中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我注意到,在许多C和C ++代码中,许多程序员似乎都希望将测试值放在第一位条件表达式。

即,他们宁愿写这个:


if(-1 == foobar())


比这个:


if(foobar()== -1)


第二种形式看起来更自然,更容易阅读恕我直言,是我一直使用的

。但是,鉴于第一个

的高发生率,我想知道使用这种方式的含义。


谢谢,


-

NeyAndrédeMello Zunino

Hello.

I have noticed, in a lot of C and C++ code, that many programmers seem
to prefer putting the test values first in conditional expressions.
I.e., they would rather write this:

if (-1 == foobar())

than this:

if (foobar() == -1)

The second form looks more natural and easier to read IMHO and is the
one I have always used. However, given the high ocurrence of the first
one, I would like to know the implications of using one way or the other.

Thank you,

--
Ney André de Mello Zunino

推荐答案

On Sun, 2004年6月20日00:40:25 -0300,NeyAndrédeMello Zunino

< zu **** @ inf.ufsc.br>在comp.lang.c ++中写道:
On Sun, 20 Jun 2004 00:40:25 -0300, Ney André de Mello Zunino
<zu****@inf.ufsc.br> wrote in comp.lang.c++:
你好。

我注意到,在许多C和C ++代码中,许多程序员似乎都是
更喜欢将测试值放在条件表达式中。
即,他们宁愿写下这个:

if(-1 == foobar())

比这个:

如果(foobar()== -1)

第二种形式看起来更自然,更容易阅读恕我直言,是我的一个总是用的。但是,鉴于第一个版本的高发生率,我想知道使用这种方式的含义。

谢谢,
Hello.

I have noticed, in a lot of C and C++ code, that many programmers seem
to prefer putting the test values first in conditional expressions.
I.e., they would rather write this:

if (-1 == foobar())

than this:

if (foobar() == -1)

The second form looks more natural and easier to read IMHO and is the
one I have always used. However, given the high ocurrence of the first
one, I would like to know the implications of using one way or the other.

Thank you,




编译器无论如何都不关心。除了一个C ++函数返回对非const对象的引用的

情况之外,

没有收益。


但是当对常量表达式进行相等测试时,请考虑

这个:


int x;

//分配一些值到x

if(x == 3)

//做点什么


现在考虑如果程序员提交的话会发生什么并非罕见

如果打算''=='',则只输入''=''错误。结果代码:


if(x = 3)

//做点什么


....是完全合法的,并具有评估分配给x的常数

值的效果。如果常量值为0,则测试始终为

false,并且始终跳过条件代码。如果常量是
而不是0,则测试始终为真且条件代码始终执行




另一方面,如果你总是写:


如果(3 == x)


....那么如果你不小心离开了第二个''=''结果是一个

约束违规,要求编译器发出诊断信息。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
h ttp://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



The compiler does not care, one way or the other. And except for the
case of a C++ function returning a reference to a non-const object,
there is no gain.

But when doing equality tests against constant expressions, consider
this:

int x;
// assign some value to x
if (x == 3)
// do something

Now consider what happens if the programmer commits the not uncommon
error of typing only ''='' when ''=='' was intended. The resulting code:

if (x = 3)
// do something

....is perfectly legal, and has the effect of evaluating the constant
value assigned to x. If the constant value is 0, the test is always
false and the conditional code is always skipped. If the constant is
not 0, the test is always true and the conditional code is always
executed.

On the other hand, if you always write:

if (3 == x)

....then if you accidentally leave off the second ''='' the result is a
constraint violation requiring the compiler to issue a diagnostic.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


NeyAndrédeMello Zunino写道:
Ney André de Mello Zunino wrote:
我注意到,在许多C和C ++代码中,许多程序员似乎更喜欢将测试值放在条件表达式中。
如果(-1 == foobar())
比这个:

if(foobar) ()== -1)

第二种形式看起来更自然,更容易阅读IMHO
并且是我一直使用的那种。
然而,鉴于高发生率第一个,
我想知道使用这种或那种方式的含义。


你注意到的是*习惯*一些程序员采用

来帮助他们避免一个常见的错误:


int x = 0;

//。 。 。

如果(x = -1)//错误!应该是(x == -1)


C和C ++的一个问题是

operator ==太接近*对于operator =

和程序员可能会错误地输入赋值运算符

当比较运算符打算。

这个印刷错误很难发现

在调试代码时,一些程序员写道:


if(-1 == x)//。 。 。


如果他们输入错误

cat main.cc
int main(int argc,char * argv []){

int x = 0;

//。 。 。

if(-1 = x);

返回0;

}

g ++ -Wall -ansi -pedantic -o main main.cc
main.cc:在函数`int main(int,char **)''中:

main.cc:4:错误:非左值在任务中


从上面的编译器获取诊断消息。

在你的例子中:

cat main。 cc
int foobar(void){

返回-1;

}


int main(int argc, char * argv []){

if(foobar()= -1);

返回0;

}

g ++ -Wall -ansi -pedantic -o main main.cc
I have noticed, in a lot of C and C++ code,
that many programmers seem to prefer
putting the test values first in conditional expressions.
I.e., they would rather write this:

if (-1 == foobar())

than this:

if (foobar() == -1)

The second form looks more natural and easier to read IMHO
and is the one I have always used.
However, given the high ocurrence of the first one,
I would like to know the implications of using one way or the other.
What you have noticed is a *habit* that some programmers adopt
to help them avoid a common mistake:

int x = 0;
// . . .
if (x = -1) // Error! Should have been (x == -1)

One problem with C and C++ is that
operator== is too *close* to operator=
and the programmer might mistype the assignment operator
when the comparison operator was intended.
This typographical error is difficult to spot
when debugging your code so some programmers write:

if (-1 == x) // . . .

so that if they mistype
cat main.cc int main(int argc, char* argv[]) {
int x = 0;
// . . .
if (-1 = x);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc main.cc: In function `int main(int, char**)'':
main.cc:4: error: non-lvalue in assignment

The get a diagnostic message from the compiler like the one above.
In your example:
cat main.cc int foobar(void) {
return -1;
}

int main(int argc, char* argv[]) {
if (foobar() = -1);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc



main.cc:在函数`int main(int,char **)''中:

main.cc:6:错误:分配时非左值


如果输入赋值运算符,则会得到诊断

而不是比较运算符因为foobar(void)

确实*不*返回左值。

这可能只是* force-of-habit *的一个例子。

使用t的程序员他的习惯习惯于在比较的左侧看到常数

,感觉更舒服

写*所有*比较表达式

即使它们不包含左值。


main.cc: In function `int main(int, char**)'':
main.cc:6: error: non-lvalue in assignment

You get a diagnostic if you type the assignment operator
instead of the comparison operator because foobar(void)
does *not* return an lvalue.
This is probably just an example of *force-of-habit*.
Programmers who use this trick get used to seeing the constant
on the left-hand side of a comparison and feel more comfortable
writing *all* comparison expressions that way
even if they don''t contain an lvalue.


Jack Klein发布:
Jack Klein posted:
另一方面,如果你总是写:

如果(3 == x)

...那么如果你不小心离开第二个''=''结果是一个
约束违规要求编译器发出诊断。
On the other hand, if you always write:

if (3 == x)

...then if you accidentally leave off the second ''='' the result is a
constraint violation requiring the compiler to issue a diagnostic.



非常聪明!

-JKop


Very clever indeed!
-JKop


这篇关于测试值在条件表达式中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆