Legacy code. Or: If they are both null, only one is null, or neither is null...



  • Direct copy-paste:

                   if ((tag == null && externalURL == null) || (tag == null && externalURL != null) || (tag != null && externalURL == null) ||
                        (tag != null && externalURL != null)){
                        replace_tag.append("\"");
                    }
    Ah, I love diving into our legacy code in order to find out how to implement the same thing in a new system.  Makes me feel smart.



  • Wasn't there a lively thread going about what null compares as == and != to? I've a feeling some implementations might make null == null and null != null BOTH return false. Meaning if tag and externalURL are both null then the if block won't execute.



  • IIRC, in relational algebra (ie, SQL), any comparison involving null returns false.



  • [quote user="Carnildo"]IIRC, in relational algebra (ie, SQL), any comparison involving null returns false.[/quote]

     In MySQL, any comparison involving null returns null, not false.

     
    Example:

    mysql> SELECT 1=1, 1=2, NULL = NULL, 1 = NULL; 

    1=1 1=2 NULL = NULL 1 = NULL
    1 0 NULL NULL

    1 row in set (0.00 sec)

     I don't know about other SQL variants, but I would guess that this is a ANSI SQL standard.
     

    Effing forum is throwing away my table border :|



  • [quote user="Carnildo"]IIRC, in relational algebra (ie, SQL), any comparison involving null returns false.[/quote]

    Only if you use equal or <, >, etc.

    If you use IS, you can get results on nulls.

    The following will return records where the field is null.

    select * from table where city is null

    The following won't return anything, I believe.

    select * from table where city = null



  • [quote user="m0ffx"]Wasn't there a lively thread going about what null compares as == and != to? I've a feeling some implementations might make null == null and null != null BOTH return false. Meaning if tag and externalURL are both null then the if block won't execute.
    [/quote]

    To me this code looks like java and the comparisons with null works properly in that language.  A==null is true if A is null and vice-versa.

    In other words this code can be abbreviated by "replace_tag.append("\"");".

    Even if there had been a time where this comparison would have workec differently it doesn't make sense to me. My bet is the person kept altering the if statement and finally couldn't be bothered to clean it up.
     



  • [quote user="Phalphalak"]

    To me this code looks like java and the comparisons with null works properly in that language.  A==null is true if A is null and vice-versa.

    In other words this code can be abbreviated by "replace_tag.append("&quot;");".

    Even if there had been a time where this comparison would have workec differently it doesn't make sense to me. My bet is the person kept altering the if statement and finally couldn't be bothered to clean it up.

    [/quote]

    The real WTF is that you wrote exactly what I was going to.



  • [quote user="Carnildo"]IIRC, in relational algebra (ie, SQL), any comparison involving null returns false.[/quote]

    Am I the only person wondering where this off-topic comment came from? This is clearly not SQL, nor is SQL a synonym for "relational algebra".



  • [quote user="savar"][quote user="Carnildo"]IIRC, in relational algebra (ie, SQL), any comparison involving null returns false.[/quote]Am I the only person wondering where this off-topic comment came from? This is clearly not SQL, nor is SQL a synonym for "relational algebra".[/quote]It looks like it was generated by a bot. It was well constructed enough that I just ignored it and moved on, even though it really doesn't make any sense.



  • I believe the problem is that Java NULL and SQL NULL are two completely different things.
    While in SQL, NULL stands for something like "Not Applicable", in Java it just means that a field that is meant to point to an object, at the moment doesn't - like a null pointer in C. Consider the difference between NULL and UNDEFINED in JavaScript. Thus, if the above snipped is really written in Java or C, it's nothing more than nonsense.



  • (SQL) comparisons with NULL are more complicated than "always returns null", actually:

     

    a

    b

    a AND b

    a OR b

    TRUE

    TRUE

    TRUE

    TRUE

    FALSE

    TRUE

    FALSE

    TRUE

    NULL

    TRUE

    NULL

    TRUE

    TRUE

    FALSE

    FALSE

    TRUE

    FALSE

    FALSE

    FALSE

    FALSE

    NULL

    FALSE

    NULL

    NULL

    TRUE

    NULL

    NULL

    TRUE

    FALSE

    NULL

    FALSE

    NULL

    NULL

    NULL

    NULL

    NULL

    Expressions containing NULL with no guard (COALESCE, IS NULL, IS DEFINED) will evaluate to NULL.



  • [quote user="Volmarias"][quote user="Phalphalak"]

    To me this code looks like java and the comparisons with null works properly in that language.  A==null is true if A is null and vice-versa.

    In other words this code can be abbreviated by "replace_tag.append("&quot;");".

    Even if there had been a time where this comparison would have workec differently it doesn't make sense to me. My bet is the person kept altering the if statement and finally couldn't be bothered to clean it up.

    [/quote]

    The real WTF is that you wrote exactly what I was going to.
    [/quote]

    You mean you were also going to write "workec" instead of "worked"? Wow, this is indeed the real WTF. :P 



  • [quote user="Carnildo"]IIRC, in relational algebra (ie, SQL), any comparison involving null returns false.[/quote]


    It's really a question about the realm of logic that the logic operators operate in. Are they diagraph or trigraph? C++ versions operate as diagraphs. Nulls are converted to false immediatly before we do any logic operations. SQL works in trigraph, where nulls are preserved through the operations and only converted to false if a true/false judgement has to be made.


Log in to reply