1

Possible Duplicate:
Order of condition checking?

When i read some source codes, the if statement is coded in this way,

if (1 == a) {
   ...
}

instead of

if (a == 1){
    ...
}

I read a programming book about the advantage of this way, but cannot remember exactly what it is about. Any one know about this ?

(Sorry if this question disturbs you :-) )

Community
  • 1
  • 1
Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307#2430307 – Matt Ball Dec 21 '11 at 03:53
  • possible duplicate of [Order of condition checking?](http://stackoverflow.com/questions/776478/order-of-condition-checking) and [Which is more effective: if (null == variable) or if (variable == null)?](http://stackoverflow.com/questions/3021195/which-is-more-effective-if-null-variable-or-if-variable-null) and [many](http://stackoverflow.com/search?q=if+condition+order) [others](http://stackoverflow.com/search?q=if+yoda) – Matt Ball Dec 21 '11 at 03:55
  • @bubuzzz - Actually in case of java the "Yoda condition" is not relevant as javac will produce an error regardless of `1=a` vs `a=1` in an if statement. For C/C++ I know it is relevant. – CoolBeans Dec 21 '11 at 04:03

3 Answers3

3

The advantage is compiler will tell you that error right away. For example, a = 1 will compile but will produce an error at run time whereas 1 = a will produce an error at compile time since 1 is not a valid lvalue.

Example: this will produce an error right away:

       int a = 0;
       if(1 = a) {
        print("test");
       }

Whereas this will compile (warnings may be produced depending on the compiler) but it will cause issues at run time.

       int a = 0;
       if(a = 1) {
           print("test");
       }

The main idea here is to make certain that you are using == in a condition instead of =.

That being said every modern compiler (ie. Eclipse) will treat the above as an error now. So it's not as big of a deal as it used to back in the notepad and vi days (in my opinion). I personally prefer the a == 1 since that seems more readable to me.

Ry-
  • 218,210
  • 55
  • 464
  • 476
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • @John - Not it's not unless 1 is defined as a variable. Read my post. – CoolBeans Dec 21 '11 at 03:51
  • a=1 will give you a big fat warning at least :) –  Dec 21 '11 at 03:52
  • @Vlad - yes true depending on the compiler ... old gcc compilers didn't even warn on that back in the days. Got beat by fat fingering in vi a lot :P – CoolBeans Dec 21 '11 at 03:53
  • No i just tried it, it works, I've always thought you couldn't do that, lesson learned – John Dec 21 '11 at 03:54
  • @John - Cool, thank you for taking the down vote back :) – CoolBeans Dec 21 '11 at 03:58
  • no i upvoted, its been an up vote from the beginning, i up voted because i was wrong and i learned something that I'm going to use from now on, good answer! – John Dec 21 '11 at 04:01
1

In classic C, where a condition is an integral expression, it's very easy to write

if (a = 1)

by mistake. The problem is that if you do it, the compiler won't complain, since the assignment evaluates to an integer, too. Writing the expression backward makes it such that if you make this typo, the code won't compile. It's not a bad idea to do this in C; it makes much less sense in other languages.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

If you leave out an =, 1 = a is a compiler error, whereas a = 1 is a subtle mistake.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964