3 ms·
Sadly, I've seen them in other languages that don't have the same problem, because people have picked up the habit without really understanding why and where it
by AndrewDucker 13y ago
Sadly, I've seen them in other languages that don't have the same problem, because people have picked up the habit without really understanding why and where it should be used.
- h2s 13y agoUnfortunately they are becoming more and more prevalent in the PHP community. For example, here they are in the Symfony project's coding standards: http://symfony.com/doc/current/contributing/code/standards.html http://symfony.com/doc/current/contributing/code/standards.h... The rationale is that they make it a compiler error if you accidentally perform an assignment operation ("xyz" = $name) instead of a comparison ("xyz" === $name). The thing is, this is only a significant risk if you adopt the bad habit of performing assignment operations within conditional statements, like this: if (null !== ($charset = $env->getCharset())) { The above style has been popularised in PHP by the Symfony project. I think it's hideous, and that the idea of adopting one ugly style choice (Yoda Expressions) to mitigate the risk introduced by another ugly style choice (assignment in conditional operations) is reminiscent of the children's song about the old lady who swallowed a fly.
- Arnor 13y agoI used to advocate for Yoda in PHP for the same reason that it's useful in C: if ($var = 1) ... will always evaluate true. But since you should be using === in this context, there's really no excuse for using a single... if ($var === 1) ... Also, can PHP devs please do me the favor of splitting if (null !== ($charset = $env->getCharset())) { into two lines: $charset = $env->getCharset(); if ($charset !== null) { ... Isn't that prettier? The line that the first one saves doesn't really make anyone's life easier...
- dionidium 13y agoHuh? You might still mistype/forget an =, even if you never intend to perform assignment in a conditional. You never have to do it on purpose to do it by mistake, in other words.