Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. alecmce revised this gist Mar 19, 2010. 1 changed file with 8 additions and 11 deletions.
    19 changes: 8 additions & 11 deletions MakeYourBooleanChecksBoolean.as
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,30 @@
    // This is just one of the reasons why
    // treating null as a boolean false
    // is Bad and Wrong
    // meh...

    var iAmAStringInstance :String = "";
    var iAmANullString :String = null;

    if(iAmAStringInstance)
    {
    trace("iAmAStringInstance: That there is an instance!");
    trace("iAmAStringInstance is neither null nor empty");
    }
    else
    {
    trace("iAmAStringInstance: That there is a null object!");
    trace("iAmAStringInstance is either null or empty");
    }


    if(iAmANullString)
    {
    trace("iAmANullString: That there is an instance!");
    trace("iAmANullString is neither null nor empty");
    }
    else
    {
    trace("iAmANullString: That there is a null object!");
    trace("iAmANullString is either null or empty");
    }

    // Trace outputs:
    //
    // iAmAStringInstance: That there is a null object!
    // iAmANullString: That there is a null object!
    // iAmAStringInstance is either null or empty
    // iAmANullString is either null or empty
    //
    // As I said, both Bad and Wrong ;)

    // Right. Not particularly good, but not particularly bad either.
  2. @mnem mnem created this gist Mar 19, 2010.
    33 changes: 33 additions & 0 deletions MakeYourBooleanChecksBoolean.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // This is just one of the reasons why
    // treating null as a boolean false
    // is Bad and Wrong

    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!");
    }

    // Trace outputs:
    //
    // iAmAStringInstance: That there is a null object!
    // iAmANullString: That there is a null object!
    //
    // As I said, both Bad and Wrong ;)