14 ms·
I ran into a Yoda Expression when coding a step condition check in JCL: http://publib.boulder.ibm.com/infocenter/zos/v1r11/index.jsp?topic=/com.ibm.zos.r11.ieab
by JimmaDaRustla 13y ago
I ran into a Yoda Expression when coding a step condition check in JCL: http://publib.boulder.ibm.com/infocenter/zos/v1r11/index.jsp?topic=/com.ibm.zos.r11.ieab600/iea2b690573.htm http://publib.boulder.ibm.com/infocenter/zos/v1r11/index.jsp...
It was infuriating because the logic is not forward thinking - if you want to check if a return code is an error (RC > 4) then your check has to be 4 < RC. The worse part was, the COND function was to test if the step was being skipped, so if you wanted to invert the logic to execute the step if the previous steps had no errors, it became 4 >= RC.
Edit: And because it is more of a "function" call, you can't modify the order. You MUST use it that way.
- cdl 13y agoThey are counter-intuitive and make code unnecessarily harder to read and reason about. It doesn't make sense to first be presented with a predicate and then have to figure out the subject.
- ccoggins 13y agoYeah, they can be tricky to read sometime. The only reason I've ever heard for writing code like that is to prevent accidental assigning in an if block. For instance if(ptr = 0) compiles fine in C and is almost certainly not what you want, but if(0 = ptr) is a compile error.
- AndrewDucker 13y agoSadly, 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.
- ternaryoperator 13y agoThat was indeed the rationale when they first appeared.
- dlhavema 13y agoi see it making sense for equality tests ( != , == ), but not for comparisons...