Skip to content
Mar 19 2010 / alecmce

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

    This was actually the subject of one of my first posts: The Truth Of Strings. I agree with you (and I guess David) that it is indeed confusing since if differs with most other types by not only checking for null but going further and doing String-specific work by checking for emptiness. Personally, I would have preferred consistency, even if it meant using “if (str && str.length == 0)”.

  • jacksondunstan

    This was actually the subject of one of my first posts: The Truth Of Strings. I agree with you (and I guess David) that it is indeed confusing since if differs with most other types by not only checking for null but going further and doing String-specific work by checking for emptiness. Personally, I would have preferred consistency, even if it meant using “if (str && str.length == 0)”.

  • http://noiseandheat.com/ mnem

    A good summary :)

    I think my particular aversion from it stems from the belief that an ‘if’ should only consider boolean conditions. I specifically use the term ‘belief’ there. There’s no reason for a language to not allow ‘if’ to consider other properties of the condition, such as the way String objects are treated in AS. It does carry a risk of making a language seem ambiguous if programmers aren’t well versed in all the nuances though. Hmm… Does that make the language more complex? Or richer? Maybe both :)

    I’d be curious as to what it’s doing internally though. I wonder if it’s all handled when the condition is cast to a Boolean?

    Out of curiosity, would you consider a String for initialising a URL as substantive if it only contained whitespace? Or does substantive arise from length? (no giggling in the back there)

    • http://alecmce.com Alec McEachran

      I think that the argument is really about to what extents we go to allow brevity in our code. However, I protested against your tweet because I just don’t find it an issue 99% of the time. If it’s so seldom an issue, why complain? There is so much else worth complaining about in the Flash runtime!

      I think that if someone is stupid enough to pass ” ” into a url parameter, I think that the program should try to treat it as substantive, and it’ll likely then break with a not-found error. We should only go so far to hold the hands of stupid coders! :)

      • http://noiseandheat.com/ mnem

        It’s all good fun. One day there will be the Perfect Language. We may have to kill the users first though.

  • http://noiseandheat.com/ mnem

    A good summary :)

    I think my particular aversion from it stems from the belief that an 'if' should only consider boolean conditions. I specifically use the term 'belief' there. There's no reason for a language to not allow 'if' to consider other properties of the condition, such as the way String objects are treated in AS. It does carry a risk of making a language seem ambiguous if programmers aren't well versed in all the nuances though. Hmm… Does that make the language more complex? Or richer? Maybe both :)

    I'd be curious as to what it's doing internally though. I wonder if it's all handled when the condition is cast to a Boolean?

    Out of curiosity, would you consider a String for initialising a URL as substantive if it only contained whitespace? Or does substantive arise from length? (no giggling in the back there)

  • http://alecmce.com alecmce

    I think that the argument is really about to what extents we go to allow brevity in our code. However, I protested against your tweet because I just don't find it an issue 99% of the time. If it's so seldom an issue, why complain? There is so much else worth complaining about in the Flash runtime!

    I think that if someone is stupid enough to pass ” ” into a url parameter, I think that the program should try to treat it as substantive, and it'll likely then break with a not-found error. We should only go so far to hold the hands of stupid coders! :)

  • http://noiseandheat.com/ mnem

    It's all good fun. One day there will be the Perfect Language. We may have to kill the users first though.

  • http://alecmce.com Alec McEachran

    I think I have changed my mind, by the way. David is right, I think. We should test for existence atomically, and non-triviality atomically, and conjoin the atomic tests if we want to test a String for both existence and non-triviality. Harumpf.

  • http://alecmce.com alecmce

    I think I have changed my mind, by the way. David is right, I think. We should test for existence atomically, and non-triviality atomically, and conjoin the atomic tests if we want to test a String for both existence and non-triviality. Harumpf.