BY-NC-ND: Fluid Design

Calling The Kettle Black

closeThe world moved on since this content was published 7 years 4 months 7 days ago. Exercise diligent care when following any instructions and see opinions in the time they were written. If you must have an updated version, please ask kindly through the contact page.

Lately, I’ve been working a lot more with other people’s code. Oddly, there seems to be some trend for certain developers to reverse the arguments in an comparison. It even shows up in the PHP manual. Take for example this code piece extracted from the PHP manual :

<?php
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
?>

Wait a minute? What does that !== thingy do? That’s explained in the manual too, on the page about comparison operators. To quote:

Example Name Result
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)

The code is obviously syntaxically correct. It works. But something just doesn’t feel right about it. We, silly humans, compare a variable thing to a static thing. Is that car black? No? How about that one? The car can change, the color is static. You don’t ask: Is black the same color as that car? You know what black is. You want to know if that car matches it. So why the reversed order? Actually, I have absolutely no idea. I don’t know why one would compare ‘false’ to the readdir() result. There is no difference, and no apparent advantage to do it in the reverse order. False doesn’t become ‘half false’, or ‘sometimes false’ if you list it first. It is not faster, as the part between the parentheses will still be overriding operator precedence. The sub-expression (assignment) ‘$filename = readdir($dh)’ will allways be done first. And after that, both values still have to be matched for non-identicallity, no matter in what order you put them in.

Even worse, it doesn’t only happen with the non-identical operator. It happens with every comparison operator. Fortunately, it doesn’t happen with assignment operators. Why not? Because it doesn’t work. You can’t assign the holder to the outcome. Only the outcome to the holder. So why compare a given fact with the outcome? If you know why, please leave a comment, and give my the answer I’ve been looking for, for a long time. If you don’t know either: please, oh please, stop comparing ‘true’ to your $variable…

~RW

Filed Under: Two Cents

Tags:

Released: on Aug 17, 2007 under a Creative Commons Attribution-NoDerivs (CC-BY-ND) licenseCC-BY-ND

Comments (1)

Trackback URL | Comments RSS Feed

  1. Daniel Berstein says:

    The *reason* is quite simple: to avoid hard to catch bugs.

    Example:

    // Bug hard to catch (no syntax error)
    if ($name = 'daniel') { }
    // First run and you get a syntax error
    if ('daniel' = $name) { }

    This is the source of the practice of placing constants on the left side of your comparisons. Some of your examples don’t take advantage of the described behaviour, but the idea is that one gets used to have *always* non-variables on the left. Consistency is a virtue.