A minor muddle of mental models

September 30, 2004 1:11 PM

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.

Previously: Why "Empire" is the best Star Wars movie

Next: Abu Ghraib