I’ve been using, and advocating for others to use, Yoda conditions for a long time. Sometimes, I read or hear about someone who doesn’t like them, without actually describing why. From time to time I read a blog post that advocates against it: And it’s always the same reason.

One of the better blog posts advocating against it is this one: “ Why using Yoda conditions you should probably not be” by Grégoire Paris. If you don’t know what Yoda conditions are, you should absolutely read it before continuing.

The problem seems to always be: “It is hard to read!” My reaction is always: “Really? No, it’s not.”

When you see code like this:

if ( 'foo' === $bar ) { }

Do you really read it out, slowly, word by word like a child, in plain English? “If … the string … containing ‘foo’ … is exactly equal to … the variable named ‘bar’ … then”

Or do you see a conditional structure with a comparison between two objects that must be exactly equal?

Children who are learning to read, must practice to see words as a whole instead of reading them out syllable by syllable. Until they do that, we say that they are learning to read. Once they do that, we say that they know how to read.

There are some variations of this text online. It is all messed up, but you can still read it, right?

The phaonmneal pweor of the hmuan mnid, aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it dseno’t mtaetr in waht oerdr the ltteres in a wrod are, the olny iproamtnt tihng is taht the frsit and lsat ltteer be in the rghi t pclae. The rset can be a taotl mses and you can sitll raed it whotuit a pboerlm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Azanmig huh?

I agree, that it would be easier to read this text if the spelling wasn’t messed up, but you can still read *that*. Now you’re telling me you’re supposed to be a professional coder, but are having trouble with reading “something here should be exactly equal to something there”?

I think you don’t like Yoda conditions because you’re used to writing code in a different way, and you don’t like being corrected. At least not over something that’s not a big deal (Yoda conditions are not a big deal, it’s just a helpful habit to avoid errors).