风格问题:(5 == x)或(x == 5) [英] Style question: (5 == x) or (x == 5)

查看:81
本文介绍了风格问题:(5 == x)或(x == 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个宗教问题,我希望这个帖子不会退化,但是我很好奇交换==的操作数的优点和缺点。


唯一一个将常数放在我知道的LHS上的是,它可以防止意外分配,其中一个平等的测试是

打算。


但上述优势仅适用于其中一个操作数是常数的情况。

情况肯定不是这样,我怀疑它通常会更难以诊断意外分配到另一个变量而不是

常数。有些编译器在任何情况下都有足够的帮助发出警告。

最后,作为母语为英语的人,我发现(5 == x)的可读性低于(x

= = 5)(我认为对于许多其他语言也是如此)。


如果使用(5 == x)样式,你是否必须追踪错误您的代码

是由于意外分配,其中两个操作数都是变量?并且

你还把LHS中的常数(如果有的话)放在一个关系的

表达式中?


Alex

I know this is a religious issue, and I hope this thread doesn''t degenerate,
but I am curious about the pros and cons of swapping the operands of ==.

The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.

But the advantage above only applies if one of the operands is a constant.
This is certainly not always the case, and I suspect it would often be
harder to diagnose accidental assignment to another variable than to a
constant. Some compilers are helpful enough to issue a warning in any case.
Finally, as a native English speaker I find (5 == x) less readable than (x
== 5) (and I imagine the same goes for many if not most other languages).

If you use (5 == x) style, do you have to track down bugs in your code which
are due to accidental assignment where both operands are variables? And do
you also put the constant (if there is one) on the LHS in a relational
expression?

Alex

推荐答案

2004年5月6日星期四,Alex写道:
On Thu, 6 May 2004, Alex wrote:
我知道这是宗教问题,我希望这个帖子不会退化,但我很好奇交换==操作数的优点和缺点。

将常量放在我所知道的LHS是它可以防止意外分配,其中有平等的测试。


这是我唯一一次听说过(5 == x)。如果还有另外一个优势,我记不起它了。

但上述优势仅适用于其中一个操作数是常量的情况。


很正确。

情况肯定不是这样,我怀疑通常会更难以诊断意外分配到另一个变量而不是一个
常数。有些编译器非常有用,可以在任何情况下发出警告。


由于这是一个常见错误,我认为它是

编译的必要功能,以警告这一点。

最后,作为一个以英语为母语的人,我发现(5 == x)的可读性低于(x
== 5)(我想,对于许多其他语言而言,情况也是如此)。


我不确定它是否仅限于母语为英语的人。我从数学中拿出了(b == 5)格式的
。我曾经和那些我有过工作的人一起工作过很多困难因为他们的英语很差而我的掌握很好但他们的语言也很差,但当我们谈论的时候那里的数学

通常很少有误解。对于基础数学我认为

(变量=常数)是一种标准格式,无论语言如何。


所以如果你能做(var = const)但是仍然保持警觉足以使用(const

== var)然后你可能不会错误地使用赋值

当你意味着比较时。

如果您使用(5 == x)样式,您是否必须追踪代码中的错误,这些错误是由于意外分配导致两个操作数都是变量?并且你还把LHS中的常数(如果有的话)放在关系表达式中吗?
I know this is a religious issue, and I hope this thread doesn''t degenerate,
but I am curious about the pros and cons of swapping the operands of ==.

The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.
This is the only time I have heard of (5 == x) being used. If there is
another advantage, I do not remember heard of it.
But the advantage above only applies if one of the operands is a constant.
Quite true.
This is certainly not always the case, and I suspect it would often be
harder to diagnose accidental assignment to another variable than to a
constant. Some compilers are helpful enough to issue a warning in any case.
Since it a common mistake I would consider it a necessary feature for a
compile to warn about this.
Finally, as a native English speaker I find (5 == x) less readable than (x
== 5) (and I imagine the same goes for many if not most other languages).
I''m not sure if it is restricted to native English speakers. I picked up
the (x == 5) format from mathematics. I''ve worked with people who I have
great difficulty talking with because their English is poor and my grasp
of their language is just as bad but when we talk about mathematics there
is usually little misunderstanding. For basic mathematics I think
(variable = constant) is a standard format, regardless of language.

So if you can do (var = const) but still remain alert enough to use (const
== var) then you probably would not make the mistake of using assignment
when you meant comparison.
If you use (5 == x) style, do you have to track down bugs in your code which
are due to accidental assignment where both operands are variables? And do
you also put the constant (if there is one) on the LHS in a relational
expression?




我正在做数学运算在我学会用C编程之前的几年。它只是

抛出我写(5 == x)所以我仍然使用(x == 5)并信任编译器

如果我应该滑倒并使用(x = 5)警告我。如果编译器没有这样做

我会使用lint。


-

发送电子邮件to:darrell at cs dot toronto dot edu

不要发电子邮件给 vi ************ @ whitehouse.gov


" Alex" < me@privacy.net>在新闻中写道:2f ************ @ uni-berlin.de:
"Alex" <me@privacy.net> wrote in news:2f************@uni-berlin.de:
我知道这是一个宗教问题,我希望这个帖子没有'Ÿ退化,但我很好奇交换==的操作数的优点和缺点。

唯一一个将常数放在LHS上的专家我是知道的是,它可以防止意外分配,其中有平等的测试。


它只是一个免费的安全,你不必使用它。你可以

就像投射var一样。也是const类型,例如


int foo = getSomeValue();


if((const int)foo == 5)

无论如何。最后,作为英语母语人士,我发现(5 == x)可读性比(x> == 5)
I know this is a religious issue, and I hope this thread doesn''t
degenerate, but I am curious about the pros and cons of swapping the
operands of ==.

The only pro of putting the constant on the LHS that I know of is that
it protects against an accidental assignment where a test for equality
was intended.
It just a safety that comes for free, you don''t have to use it. You could
just as well cast the var. to a const type too, e.g.

int foo = getSomeValue();

if ((const int) foo == 5)
in any case. Finally, as a native English speaker I find (5 == x) less
readable than (x >== 5)




为什么?你找到了:


if(foo == bar)



更可读

if(bar == foo)





如果(5 == value)应该没有什么不同。


-

- 马克 - >

-



Why? Do you find:

if (foo == bar)

more readable than

if (bar == foo)

?

if (5 == value) should be no different.

--
- Mark ->
--




" Darrell Grainger" <哒***** @ NOMORESPAMcs.utoronto.ca.com>在消息中写道

新闻:Pi ******************************* @ drj.pf。 ..

"Darrell Grainger" <da*****@NOMORESPAMcs.utoronto.ca.com> wrote in message
news:Pi*******************************@drj.pf...
2004年5月6日星期四,亚历克斯写道:
我不确定它是否仅限于母语为英语的人。我从数学中选择了(x == 5)格式。我曾经和那些我很难交谈的人一起工作,因为他们的英语很差,而且我对他们语言的把握也同样糟糕,但是当我们谈论数学时,通常是一点点误会。对于基础数学,我认为
(变量=常数)是一种标准格式,无论语言如何。
On Thu, 6 May 2004, Alex wrote:
I''m not sure if it is restricted to native English speakers. I picked up
the (x == 5) format from mathematics. I''ve worked with people who I have
great difficulty talking with because their English is poor and my grasp
of their language is just as bad but when we talk about mathematics there
is usually little misunderstanding. For basic mathematics I think
(variable = constant) is a standard format, regardless of language.




有了这个我同意,我是不是原生的Endglish扬声器加上

当你解决一个等分时你总是写x = sometnihg甚至

如果结果是在结束时sometnig = x。

-

Frane Roje


祝你有愉快的一天


删除(* dele * te)从电子邮件到回复



With this I would agree, and I''m not a native Endglish speaker plus
when you solve an equotation you always write x = sometnihg even
if the result comes at the end sometnig = x.
--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply


这篇关于风格问题:(5 == x)或(x == 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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