What is the difference between if(CONST==variable) or if(variable==CONST)?

前端 未结 5 1077
星月不相逢
星月不相逢 2021-01-11 14:01

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

#define CONST_VALUE 5

int variable;

...

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

O         


        
5条回答
  •  借酒劲吻你
    2021-01-11 14:49

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

    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).

提交回复
热议问题