Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between i==0 and 0==i?

Tags:

c++

c

c#

php

First code:

  if(i==0) {// do instructions here}

Second code:

  if(0==i) { // do instructions here }

What is the difference between the blocks?

like image 817
Rafik Bari Avatar asked May 18 '12 16:05

Rafik Bari


1 Answers

Functionally, there is no difference.
Some developers prefer writing the second format to avoid assignment typos(in case you miss a =), so that compiler warns of the typo.
The second is famously known as Yoda Condition.

Yoda Condition

I say there is no difference because, you cannot guard yourself against every minuscule detail and rely on compiler to cry out aloud for you.If you intend to write a == you should expect yourself to write a == and not a =.
Using the second format just leads to some obscure non-readable code.
Also, most of the mainstream compilers warn of the assignment instead of equality typo by emitting an warning once you enable all the warnings(which you should anyways).

like image 77
Alok Save Avatar answered Oct 03 '22 13:10

Alok Save