为什么一些经验丰富的程序员用变量前的值写比较? [英] Why do some experienced programmers write comparisons with the value before the variable?

查看:63
本文介绍了为什么一些经验丰富的程序员用变量前的值写比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复项:

如何检查是否等于? (0 == i)或(i == 0)

为什么经常会看到 null!= variable而不是变量!=空在C#中?


我一直在这里看一个奇怪的教程以及一些DirectX代码,并注意到许多经验丰富的C ++程序员以以下方式编写表达式:

 (< constant> ; ==< variable>)

而不是我传统的智慧似乎更喜欢:

 (< variable> ==< constant>)

例如 if(NULL == ptr),而不是 if(ptr == NULL)。我更喜欢第二种选择,如果没有其他理由选择前一种,我的理由是该变量似乎是表达式的接收端。



但是我怀疑前者是用来避免通过使用 = 来无意中将常量的值赋给变量的而不是 ==

解决方案

是的,这是正确的。这是为了检测 = 的错字,而不是 == 的错字。

Possible Duplicates:
How to check for equals? (0 == i) or (i == 0)
Why does one often see “null != variable” instead of “variable != null” in C#?

I've been having a look at an odd tutorial here and there as well as some DirectX code and noticed that many experienced C++ programmers write expressions in the following way:

(<constant> == <variable>)

rather than what my conventional wisdom seems to prefer:

(<variable> == <constant>)

E.g. if (NULL == ptr) rather than if (ptr == NULL). I prefer the second alternative, if there are no other reasons for choosing the former, my reason being that the variable seems to be the "receiving" end of the expression.

But I suspect the former is used to avoid inadvertently assigning the value of the constant to the variable by using = rather than ==. Would that be correct?

解决方案

Yes, that's correct. It's to detect the typo of = instead of ==.

这篇关于为什么一些经验丰富的程序员用变量前的值写比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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