4

Hi In our company they follow a strict rule of comparing with null values. When I code if(variable!=null) in code review I get comments on this to change it to if(null!=variable). Is there any performance hit for the above code? If anybody explains highly appreciated.

Thanks in advance

giri
  • 26,773
  • 63
  • 143
  • 176
  • Shouldn't be any performance gain or degradation either way afaik, guess it's just how they like to do it. – esaj Feb 13 '11 at 15:45
  • This coding style is called "Yoda conditions", if you search for it you find many examples and discussions. – Cephalopod Feb 13 '11 at 16:24
  • Possible duplicate of http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c – Julius A Feb 13 '11 at 16:59
  • @J Angwenyi: The answers there will answer this question, but it's still a different language. – AbdullahC Feb 13 '11 at 17:01
  • There is a great answer to this question over at http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c – Jan Dragsbaek Feb 13 '11 at 15:47

4 Answers4

11

I don't see any advantage in following this convention. In C, where boolean types don't exist, it's useful to write

if (5 == variable)

rather than

if (variable == 5)

because if you forget one of the eaqual sign, you end up with

if (variable = 5)

which assigns 5 to variable and always evaluate to true. But in Java, a boolean is a boolean. And with !=, there is no reason at all.

One good advice, though, is to write

if (CONSTANT.equals(myString))

rather than

if (myString.equals(CONSTANT))

because it helps avoiding NullPointerExceptions.

My advice would be to ask for a justification of the rule. If there's none, why follow it? It doesn't help readability.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

This has nothing to do with performance. It's used to prevent that you assign accidentally instead of comparing. An assignment null = var won't make any sense. But in Java var = null also won't compile so the rule of turning them around doesn't make sense anymore and only makes the code less readable.

Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
3

No performance difference - the reason is that if you get used to writing (null == somevar) instead of (somevar == null), then you'll never accidentally use a single equals sign instead of two, because the compiler won't allow it, where it will allow (somevar = null). They're just extending this to != to keep it consistent.

I personally prefer (somevar == null) myself, but I see where they're coming from.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • 2
    I don't see how `somevar = null` will compile in an `if` condition (except if the type of `somevar` is `Boolean`) – AbdullahC Feb 13 '11 at 16:29
  • 1
    Good point - I don't know about Java, but I know in some of the similarly-structured languages like C or C++, it will compile, with unexpected results. And coding standards tend to carry over to similar languages for a lot of developers. – Joe Enos Feb 13 '11 at 16:46
3

It's a "left-over" from old C-coding standards.

the expression if (var = null) would compile without problems. But it would actually assign the value null to the variable thus doing something completely different. This was the source for very annoying bugs in C programs.

In Java that expression does not compile and thus it's more a tradition than anything else. It doesn't erver any purpose (other than coding style preferences)