1

Possible Duplicate:
How to check for equals? (0 == i) or (i == 0)
Why does one often see “null != variable” instead of “variable != null” in C#?
Why do some experienced programmers write expressions this way?
What is the meaning of NULL != value in C++?

For example,

 int k =5;
 if( 5 == k )
 {
 } 

is preferred over

if (k == 5)
 {
 }

Is it considered only for formatting purpose or is there any reason behind it?

Community
  • 1
  • 1
Prabhu
  • 723
  • 2
  • 10
  • 29
  • 1
    [How to check for equals? (0 == i) or (i == 0)](http://stackoverflow.com/questions/148298/how-to-check-for-equals-0-i-or-i-0), [What is the difference between if(CONST==variable) or if(variable==CONST)?](http://stackoverflow.com/questions/1626573/), [Why does one often see “null != variable” instead of “variable != null” in C#?](http://stackoverflow.com/questions/271561/), [== Operator and operands](http://stackoverflow.com/questions/677264/) [Why do some experienced programmers write expressions this way?](http://stackoverflow.com/questions/3309089/) – Cody Gray - on strike Dec 21 '11 at 14:17
  • 6
    Duplicate. This will be closed soon. My advice: don't bother. Let the compiler warnings do their job, and pay attention to them. – Fred Larson Dec 21 '11 at 14:17
  • 3
    It's because some people use silly compilers that don't warn when you accidentally write `if(variable = constant)`. Either that or they are silly enough to not enable the warnings on their compilers. – R. Martinho Fernandes Dec 21 '11 at 14:18
  • Actually, I know about this rule for Java, not C or C++, for testing object equality. The reasoning behind this is that for any object o, `o.equals(null)` is false. For instance, `"foo".equals(bar)` instead of `bar.equals("foo")`. I don't know whether (and don't think) it matters for C and C++. – fge Dec 21 '11 at 14:19
  • @fge Nobody said anything about .equals; the question applies to any language using = and ==. The *reason* is to avoid an NPE. – Dave Newton Dec 21 '11 at 14:33

6 Answers6

14

Because that form makes it harder to introduce a bug by forgetting one of the equals signs. Imagine if you did this:

if (k = 5)

This was intended as a comparison, but it's now an assignment! What's worse, it is legal, and it will mess up your program in multiple ways (the value of k is changed, and the conditional always evaluates to true).

Contrast this with

if (5 = k)

This is not legal (you cannot assign to a literal) so the compiler will immediately flag it as an error.

That said, this style of writing code (assignments within conditionals) is not as prevalent today as it once was. Most modern compilers will flag this as a warning, so it's unlikely to go undetected. Personally I don't like the second form and since the compiler is there to help I don't use it.

Jon
  • 428,835
  • 81
  • 738
  • 806
6

If you mistype and write

if (k = 5) // instead of ==

then you likely just introduced a hard to find bug. (Actually, it used to be hard to find. Nowadays, most compilers will flag this as a warning.)

This, however, will result in a compile-time error

if (5 = k)

BTW, this style is called Yoda Conditions :-)

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
3

It's to avoid the mistake of

if( k = 5 ) {

}

which would always equal true.

punkrockbuddyholly
  • 9,675
  • 7
  • 36
  • 69
1

A. Who said it's preferred ?!

the only reason i can think of it's to avoid:

 int k =5;
 if( 5 = k )//notice one "="
 {

 } 

like this you will get a compilation error, while the other way will work. but I think it's less readable and less preferred.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
1

First of all, most people prefer the second form, since it feels "more natural"; the first form is felt as "reversed", and in fact is often called "Yoda conditional".

The rationale behind using the first form is to avoid accidental assignment when typing = instead of == by error. Since in a conditional you can write any expression, = is allowed, so in case of mistyping the instruction

if(k = 5)
{

}

won't check if k is equal to 5, but will assign 5 to k and, since = returns a reference to its left hand operator, the condition will be evaluated as true and the if body will always be executed.

On the other hand, if you typed = instead of == in the Yoda conditional you would get

if(5 = k)
{

}

which results in a compilation error, since you can't assign anything to a literal (5).

Although they look like a good idea, "Yoda conditionals" are quite weird looking, and, most importantly, almost any good compiler with warnings turned on will warn you anyway if you write an assignment inside a conditional expression, so most people just use the "natural looking" form.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

It's because a common typo is typing = instead of ==.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180