Ifs and Buts and Strings
It has been a fun morning in the twitterverse. @kaeladan (aka David Wagner, my replacement at Yomego, and friend) tweeted that:
Dear Flash programmers, stop using if( foo ) or if( !foo ) to check if an object is null. It’s both Bad and Wrong. Luv, K #dodgylibraries
To support the argument, David put this gist on github:
var iAmAStringInstance :String = "";
var iAmANullString :String = null;
if(iAmAStringInstance)
trace("iAmAStringInstance: That there is an instance!");
else
trace("iAmAStringInstance: That there is a null object!");
if(iAmANullString)
trace("iAmANullString: That there is an instance!");
else
trace("iAmANullString: That there is a null object!");
It took me a while to understand what I had trouble with in David’s tweet, but the gist helps. The trick is that the semantic label that David has applied to the if (foo) conditional is not correct in the context of a String. If we use if (foo) we are not in fact testing for existence. We are testing for existence and non-triviality. The implication that all we’re doing is testing for existence versus nullity comes from the trace statements within the conditionals. These are well-meant, but misleading.
That David used a String for this example is significant. A typed object reference can only resolve to false if it is null. String is one of the few ambiguous cases, like a Number or an Object (which is ambiguous because it could be a String or a Number).
Once we got going some people argued that we should change the language so that an empty string or 0 casts to a Boolean as true. Clearly this is a hypothetical decision, since you can’t change the API of a language after the fact. They have a point in terms of consistency with a typed Object I suppose, though I think it’s a over-the-top to argue as DavidArno does that:
@alecmce The idea that a non-zero length string is true and a zero-length string is false is a broken paradigm. That was my point.
That’s a bit strong I think. How often does it break down your code? Sometimes, it is positively useful that an empty string (or 0) casts to Boolean as false.
I often want to test whether something is substantive. Take a URL for example – potentially someone could have initialised a variable for a URL in the constructor, or potentially it could be null. I want to know if it’s been filled in, so I can use if (url) to test for that. It would be as annoying in this brave new paradigm to have to write if (url != "") as it would to have to write if (url == null) in the current one.
To go back to the example then, we need to be careful to be clear about what our code actually means:
if(iAmAStringInstance)
trace("iAmAStringInstance is neither null nor empty");
else
trace("iAmAStringInstance is either null or empty");
if(iAmANullString)
trace("iAmANullString is neither null nor empty");
else
trace("iAmANullString is either null or empty");
In one sense then, I agree with David, though not in the sense he intended. It goes over Twitter’s character limit, and it’s rather less pithy, but perhaps he might have tweeted:
Dear Flash programmers, stop using if( foo ) or if( !foo ) to check if an untyped object, string or number is null. It’s Bad and Wrong in these particular contexts, if what you really are trying to do is to check for existence, rather than substance..
-
Anonymous
-
jacksondunstan
-
http://noiseandheat.com/ mnem
-
http://alecmce.com Alec McEachran
-
http://noiseandheat.com/ mnem
-
http://noiseandheat.com/ mnem
-
http://alecmce.com alecmce
-
http://noiseandheat.com/ mnem
-
http://alecmce.com Alec McEachran
-
http://alecmce.com alecmce


