Calling The Kettle Black

Lately, I've been working a lot more with other people's codes. Oddly, there seems to be some trend for certain developer 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 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

To avoid phantom bugs!

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.

Post new comment

The content of this field is kept away from all humans and will not be shown publicly.
Do not bother commenting if your intent is promoting something. This has to be a *personal* website. If the link tries to "sell" me something, and the comment is not adding anything, the comment will very likely be deleted
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <img> <center> <table> <tr> <th> <td>
  • Lines and paragraphs break automatically.

More information about formatting options

Powered by Drupal - Design by artinet