关系运算符表达顺序 [英] relational operator expression order
问题描述
这可能是一个愚蠢的问题,但是好奇心使我变得更好.我最近看到的代码似乎颠倒"了关系运算符的表达式顺序,例如:
This is probably a silly question, but curiosity has gotten the better of me. I've been seeing code lately that seems to "reverse" the order of expressions for relational operators e.g.:
if (0 == someVariable)
与我通常看到的/写的相反:
As opposed to what I normally see/write:
if (someVariable == 0)
在我看来,第二种方法似乎更具可读性和直观性,因此我想知道是否有某些原因让我看到第一种方法?从逻辑上讲,两个语句得出的结果都相同,所以编写方式仅仅是个人喜好吗?
To me, the second method seems more readable and intuitive, so I'm wondering if there's some reason I'm seeing the first method? Logically, both statements evaluate to the same result, so is it just a matter of personal preference how they're written?
推荐答案
我了解这是个人喜好.尽管通过将变量放在第二位可以确保您不会意外地将常量分配给用于隐藏c开发人员的变量.这可能就是为什么在开发人员切换语言时会在c#中看到它的原因.
I understand that this is personal preference. Although by putting the variable second you can ensure that you don't accidentally assign the constant to the variable which used to concearn c developers. This is probably why you are seeing it in c# as developers switch language.
这篇关于关系运算符表达顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!