-3

Recently I saw programmer put literal in front of if condition

if ( -99 == $poll_id )

Why they did that? What is the difference with normal if e.g.:

if ( $poll_id == -99 )
Azam
  • 797
  • 2
  • 8
  • 23
  • how can you search for that question? 'null' is very specific word. – Azam May 18 '14 at 20:20
  • The style you describe is sometimes referred to as "yoda conditions". Searching on Stack Overflow with that term will quickly turn up some duplicates. It's not an easy thing to search for if you don't already know what it's called :) – user247702 May 18 '14 at 20:30
  • yeah this is the first time i saw that term. you don't see that in code. thx for the reference. if I didn't ask, I won't get the answer :) – Azam May 18 '14 at 20:34

1 Answers1

1

This an idiom that is borrowed from C. It is called Yoda Conditions.

The idea behind the idiom is avoiding errors when you type one equal sign instead of two.

When you write

if ( x = 3 )

instead of

if ( x == 3 )

The code would compile, but work incorrectly.

If you get in the habit of turning the condition around, the code with a single equal sign would not compile.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Why did you post an answer instead of closing the question as a duplicate? The [linked questions list](http://stackoverflow.com/questions/linked/271561?lq=1) of the proposed duplicate lists 30 questions on the topic already. – user247702 May 18 '14 at 20:31
  • I think my question is easier for searching – Azam May 18 '14 at 20:36