4

does anyone know why some developers (especially seen in the sources of Zend Framework 2) write the expected value before the actual value in comparisons?

Example:

if (true === $actualValue) { ... } 

instead of

if ($actualValue === true) { ... } 

This case is not defined in the PSR coding standard.

Note: There is a similar topic for c++ but without really helpful answers.

1
  • I thought it was as a way of eliminating the possibility of assignment within a condition accidentally (i.e. if you forget one of the equals signs). Not sure if that's the main reason, but I've seen that before. true = $actualValue throws a parse error, while $actualValue = true doesn't. Commented Aug 20, 2012 at 11:40

1 Answer 1

10

What you are seeing is Yoda conditions. There is no standard defining these (at least not to my knowledge). They are merely a way to protect yourself against a common coding error (assignment in your conditions).

Example:

if( number = 4 ) // Works perfectly if( 4 = number ) // Throws an exception 
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for reading the question correctly (unlike I did) and knowing the reference :)
I had to look it up, but I did know the name, Jeff recently blogged about it: codinghorror.com/blog/2012/07/new-programming-jargon.html
Good god, I wish I could Give you another +1 for the laughs I just got from the link. I normally just read Joel
Thanks for the answers. May the force be with you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.