Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Lately, I've been working a lot in PHP and specifically within the WordPress framework. I'm noticing a lot of code in the form of:

if ( 1 == $options['postlink'] )

Where I would have expected to see:

if ( $options['postlink'] == 1 )

Is this a convention found in certain languages / frameworks? Is there any reason the former approach is preferable to the latter (from a processing perspective, or a parsing perspective or even a human perspective?)

Or is it merely a matter of taste? I have always thought it better when performing a test, that the variable item being tested against some constant is on the left. It seems to map better to the way we would ask the question in natural language: "if the cake is chocolate" rather than "if chocolate is the cake".

share|improve this question
1  
I never ever write code like that but to be fair "if chocolate is the flavor of the cake" does sound natural. Natural language is more flexible. –  Rick Sladkey May 5 '11 at 23:17
3  
@Rick It might sound natural in language, but you can't deny that when you see code like that, you have to stop first (maybe only for a second) to think what is it that it's trying to do. –  Edgar Gonzalez May 6 '11 at 1:45
2  
@Edgar Gonzalez: Agreed, I am firmly against it in code. –  Rick Sladkey May 6 '11 at 1:52
2  
Chapter 19 of Code Complete 2nd Edition (under the section "Boolean Expressions: Common Problems With Boolean Expressions") actually recommends this practise for the exact reason stated in many of the answers here: to prevent assignment in C-derived languages when comparison was meant. –  CraigTP May 6 '11 at 10:09
2  
I've often seen these referred to as "Yoda Conditions" –  Brian Sep 12 '12 at 20:58

3 Answers 3

up vote 60 down vote accepted

The main reason to do this (so-called "Yoda conditional") is to prevent accidents whereby you accidentally use an assignment operator (=) instead of the equal comparison operator (==).

That is, if you made the mistake of doing:

$foo = 5;
if ($foo = 1) {
  // Stuff
}

The statement will evaluate to true (or, in the case of some languages—like PHP—a truthy value) and you'll have a hard-to-find bug.

But if you did:

$foo = 5;
if (1 = $foo) {
  // Stuff
}

You'll receive a fatal error because you can't assign $foo to an integer.

But as you pointed out, reversing the order generally makes things less readable. So, many coding standards (but not all, including WordPress) suggest or require $foo == 1 despite the bug hunting benefits of 1 == $foo.

Generally, my advice is to follow whatever established coding standard there is, if there is one: for WordPress, that means using Yoda conditionals.

When there isn't, and it's impossible to establish one through consensus with your peers, it's dealer's choice.

share|improve this answer
2  
I remember when designing a language (long time ago) that we specifically made := be the assignment operator (with == for equality test) in order to avoid this sort of trouble. –  Donal Fellows May 5 '11 at 23:37
5  
I have written many, many, lines of code, and I have never accidentally typed = instead of ==. The difference is so accentuated everywhere that i've just never got them confused. On the other hand, I have read many pieces of code that are confusing or otherwise hard to understand. As such, I would put the priorities on the readability :). Regardless, good answer. –  crazy2be May 6 '11 at 0:22
4  
Yet another good reason to use -Wall -Werror or your compiler/interpreter's equivalent. There are very few situations where an assignment inside a condition is correct, let alone more readable. A lot of languages don't even allow it. –  Karl Bielefeldt May 6 '11 at 3:21
6  
Pedantic: While if($foo = 1) evaluates to true in some languages, in PHP it evaluates to 1 instead; if($foo = 20) evaluates to 20; if($foo = 0) evaluates to 0, which unlike the others is false. This can add a whole 'nother layer of complexity to the bug. –  Charles May 6 '11 at 5:00
2  
Actually, the WordPress Coding Standards DOES call for Yoda Conditionals: codex.wordpress.org/WordPress_Coding_Standards#Yoda_Conditions –  Tom Auger Sep 18 '12 at 14:46

It's a defensive coding mechanism meant to prevent an accidental use of the assignment operator.

Consider a misuse/error of the assignment operator in place of the equality operator

if ( $options['postlink'] = 1  )

The above conditional would always return true, but that's probably not what the original programmer had in mind. Consider, in it's place, this

if( 1 = $options['postlink'])

Here, PHP (and most other languages) would refuse to run, as it's impossible to assign anything to the fixed value of 1. By coding up all the conditional statements this way, you automatically ensure no accidental usage of an assignment operator in a conditional.

share|improve this answer

I like using that convention in java to remove the possibility of a null pointer exception. So something like this won't cause you any problems or need any extra code:

String foo = null;

if ("bar".equals(foo))
{
    //Do something
}
share|improve this answer
1  
I like this, but I hate the general idiom. –  Thomas Eding May 6 '11 at 3:35
2  
If a null value is not valid by that point in code you should have already checked for it anyway or designed your code in such a way that a null value would be impossible. –  Ed S. May 7 '11 at 20:03
3  
this seems like an easy way to mask problems. Dusts aren't cleaned by shrugging them inside the carpet. –  Lie Ryan Jul 13 '11 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.