Wednesday, 28 September 2011

Weird use of Conditional Operator

Found this gem in my companies code today.

It is not exactly that it is wrong, strictly speaking, it's just... well, you can probably guess yourself what I would rather see here.

public boolean checkIfWeHaveState() 
    return state != null ? stateDao.countStates(state) > 0 : false;

P.S. off the top of my head, the only reason why I would ever do something like the above, is if Java doesn't do Short-circuit evaluation (for example with the & operator).

Of course, the following example is often used, and here a conditional statement is warrented.

public int getRowCount() 
    return list != null ? list.size() : 0;

Of course, these are but very small issues in the grand scale of things, but it was Sherlock Holmes who said “To a great mind, nothing is little.”

No comments:

Post a Comment