1

I found that some programmers would like to code like this in the comparator operator. I found it is more difficult to read...

if (0 == foo()){
    ....
}

Is there any different between foo() == 0 in term of readability ? What's the advantage of using 0 ==foo()?

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • 3
    I am with you, I find this less readable (as it is not natural). But there was a push about a decade ago to put the 0 first as it prevents accidental assignment when you forget to use '==' and use '=' instead. Personally I find this argument very weak as the compiler actually warns you (and I compile with the flag that converts all warnings to errors so it would fail to compile for me so it is never a problem). – Martin York Jul 25 '11 at 16:45
  • 1
    @Martin: If I'm not mistaken, that argument is more applicable for C, where you don't get a warning for that, if I'm not mistaken. – JAB Jul 25 '11 at 16:49

5 Answers5

6

In that case there is no difference but when comparing strings it is a good idea to use the string constant first to avoid a null pointer exception.

i.e.

if ("somestring".equals(someVarString)) {
// doSomething
}

So someVarString can be null and the test is still valid. Whereas if you flip the test to:

if (someVarString.equals("somestring")) {
// doSomething
}

it will cause an NPE if someVarString is null.

Matthieu Cormier
  • 2,185
  • 1
  • 21
  • 32
5

No I think the best reason to do it like this:

0 == foo

is to make sure you don't forget one = which would make it

if (0 = foo)

which will usually raise a compiler error rather than

if (foo = 0)

which creates a hard-to-find bug.

cellcortex
  • 3,166
  • 1
  • 24
  • 34
  • 7
    And just about every C++ compiler will warn about this. I get `warning C4706: assignment within conditional expression` in Visual C++ when I write something like `if(foo = 0)`. And I set my compiler to turn warnings into errors, so I actually haven't been bit by this. – In silico Jul 25 '11 at 16:46
3

The advantage of this style is that in all cases the compiler is guarenteed to complain if you type = rather than == because you can't assign to a numeric.

For example

  bool a = 1;
  if (0 = a) 
  { }
  else if( 1 = a ) 
  { }

will not compile, whereas

bool a = 1;
  if (a = 0 ) 
  { }
  else if( a = 1 ) 
  { }

is not illegal (it is likely to produce compiler warnings)

That said, I agree that is looks ugly, and usually do it the other way round.

Tom
  • 5,219
  • 2
  • 29
  • 45
1

In terms of performance, no.

Readability is subjective; I personally find 0 == foo() slightly more awkward to read than foo() == 0.

The only argument I've seen in favour of if (0 == var) is that if you accidentally mistype this as if (0 = var), the compiler will complain. However, most modern compilers will issue a warning when they see if (var = 0), rendering the argument moot. Besides, this line of thinking isn't even applicable to your case since if (foo() = 0) isn't valid code.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

there's no performance impact, the reason people do it is to ensure they cannot accidentally type the = operator instead of the == comparison operator (as the compiler would complain you cannot assign to a constant).

I find that the readability penalty is more than I like, so I don't do it. Others have obviously gotten used to it.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148