是如果(CONST ==变量)或者(可变==常数)之间的区别? [英] What is the difference between if(CONST==variable) or if(variable==CONST)?

查看:99
本文介绍了是如果(CONST ==变量)或者(可变==常数)之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比较运营商的顺序不同?

Is there a difference in the order of the comparison operator?

#define CONST_VALUE 5

int variable;

...

if ( variable == CONST_VALUE )   // Method 1
...

OR

if ( CONST_VALUE == variable )   // Method 2
...

这是简单的preference的问题还是有一个令人信服的理由为特定的比较顺序?

Is this simply a matter of preference or is there a compelling reason for a particular comparison order?

推荐答案

之所以有人使用方法2是因为如果你在的地方==的敲错了=你会得到一个编译器错误。

The reason some people use method 2 is because you'll get a compiler error if you mistype a = in place of the ==.

不过,你得人(像我)谁仍然使用方法1,因为他们觉得更容易阅读,如果有错误,它将测试(或在某些情况下,静态分析过程中被检测code)。

However, you'll have people (like me) who will still use method 1 because they find it more readable and if there is an error, it will be detected during testing (or, in some cases, static analysis of the code).

这篇关于是如果(CONST ==变量)或者(可变==常数)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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