2

I am trying to make a report of some code enhancement, however, i am not too sure what this item is called. Basically, instead of doing a conn == null, it does a null == conn for readability.

Before:

        if (conn == null){            
            if (log.isDebugEnabled()){
                log.debug("Failed to get a DB Connection");
            }
        }

After:

        if (null == conn){            
            if (log.isDebugEnabled()){
                log.debug("Failed to get a DB Connection");
            }
        }
Machavity
  • 30,841
  • 27
  • 92
  • 100
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • 2
    it's no enhancement and both are semantically the same. – Bala R Jun 28 '11 at 04:03
  • 2
    I wouldn't say that one is more readable than the other. They're equivalent in almost every way, and in every way that matters. – trutheality Jun 28 '11 at 04:05
  • 2
    I have heard the term [Yoda Conditions](http://wiert.wordpress.com/2010/05/25/yoda-conditions-from-stackoverflow-new-programming-jargon-you-coined/) used sometimes (at least on SO). Happy coding. –  Jun 28 '11 at 04:33

4 Answers4

7

It's not for readability, it's to prevent accidental assigning instead of comparing.

If you accidentally write:

if (conn = null)

in C or C++, that will set conn to null and then use that value as the condition. This has been carried over into Java despite the fact that it's not necessary there.

The other way around, the compiler catches it as an attempt to assign to a constant so it won't even compile.

As for what its called, I've never seen it given a specific name. You can call it what you want provided you explain it. I like Accidental assignment avoidance since I can just shorten that to triple-a.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Java only allows boolean expressions in `if` statements, so this can be only used for something like `if (true == someCondition)` in which case it's better to write `if (someCondition)` anyway – Ismail Badawi Jun 28 '11 at 04:10
  • 1
    Of course, such an assignment in Java doesn't cast itself into a boolean, so it's more a tip of the hat to history than actually a precautionary measure. – Edwin Buck Jun 28 '11 at 04:10
  • 1
    Java will still accept `if (x = true)`, where x is a boolean variable. It will not accept `if (true = x)` (which is silly, but alas..). So this inverted conditional will still save you, even if there is no magical cast to boolean or whatnot. –  Jun 28 '11 at 04:35
3

This practice is called "putting the constant before the variable in an equality test".

Ostensibly its purpose is to avoid the a specific kind of typing error:

if (conn = null) { ...

where the = is assignment (when == comparison was intended). Modern compilers warn about this error, so this practice is not really needed any more.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

It is just to avoid assigning to a variable by mistake.

That is if you use conn=null then null is assigned to the variable, however, if you do null=conn then the compiler will through an error since you cannot assign to 'null'.

So it prevents errors where you mistakenly type one '=' instead of two '=='

Sujoy
  • 8,041
  • 3
  • 30
  • 36
0

That particular syntax is quite useless. But consider when you are comparing an object with unknown initialization state to a 'constant' object.

String unknown;
String KNOWN = "Hello";

It is much better to do your comparison so:

if(KNOWN.equals(unknown)) do something;

As it saves a null check which if omitted could cause a runtime exception.

Perception
  • 79,279
  • 19
  • 185
  • 195