When coding, I like my conditional expressions to be affirmative. It just feels cleaner to me somehow to say if (bob.isHavingANiceDay()) instead of if (!bob.isHavingABadDay()). There's one fewer mental reversal you have to make to understand the intent, and the code itself just feels positive, shinier and more optimistic about life.
This is one reason I have a guilty appreciation of languages that provide unless alongside if. It's entirely redundant syntactic sugar, but it lets me keep my happy, affirmative expressions. But that's not what I'm talking about now.
Java's handling of nulls conspires to make it impossible for me to write a conditional that I'm truly happy with. I generally write if (foo != null), which is a sad, negative expression that paradoxically really means "if foo is really there", which is positive! So while the expression itself is negative, the most positive code ends up in the "if" side of the conditional, and the negative "oh dear, I couldn't find it" code ends up in the else block where it should be.
If I turn the conditional around and write if (foo == null) I get is a positive expression that's really negative, and my code ends up the wrong way round!
As a result, I can never be happy.
Languages where null is false make me happy.
I am aware how sad this whole post makes me sound.
How about
unless (! foo) {
...
}
:-)
With JDK 1.5 static imports, you could write yourself a static function like this:
public static boolean isObject(Object obj) {
return object != null;
}
Then use like this:
if (isObject(foo)) {
...
}
Of course, "object" is also a verb with irascible connotations, so I guess isObject might make you feel sad too.
I'm not never reading your blog again.
I usually pick the expression I think will be true more often and put that first, and leave the less frequent choice for the else clause.
Ahhh Nulls
I was trying to explain how Nulls mess up boolean logic to a co-developer the other day.
Null doesn't mean false - it means unknown. Not an empty string, not false, just 'I dunno'. It's a special case that needs to be handled 'specialy'.
If your Null really means False, why is it set to Null and not to False?
I feel your pain though :-( It's even worse in PHP where a variable can also be set or unset :-/ (it may be the same for Java, but I don't DO Java).
if (isset($var) and $var!=Null)...
(I had 3 attempts at trying to get the grammer correct in this comment, lol)
What's truly sad is that the rest of us understand exactly what you're saying!
Guess which language...
If Not variable Is Nothing Then
if (foo instanceof Object) ..
ruby has unless and I don't like it. I know every other developer does,
but I can't love it.
It must be something related to how "unless" translate in my home language (a periphrasis, not a single-word->single-word translation).
But nil behaving like false is a wonderful thing. Every language should have that ;)
Chris,
Getting an exception thrown at you is potentially very expensive, and is a Bad Thing, especially if you know that both branches of the if clause have an equal chance of being executed.
That said, I feel your pain Charles. (The instanceof suggestion sounds interesting...)
I like to deal with error cases first, so generally I would make if statements full of negativity that only return false on error, and then write the rest of the task outside of any if statement because by then things "should just work".
Am I a negative individual?
Hear hear: Null should be false.
Richard@home: "null" doesn't mean "unknown", it means "none" or "there is no object in this cell." As such it seems perfectly natural to convert between presence/absence and true/false.