0

Possible Duplicate:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?

From this question it says "const object on left side of comparison" is some how "better" than doing otherwise. Why is this?

Community
  • 1
  • 1
Celeritas
  • 12,953
  • 32
  • 95
  • 174
  • 1
    Saying `if (5 == i)` isn't as much any more used. It's supposed to stop from saying you `if (i = 5)` by mistake. Sometimes called "Yoda Conditions" it is. A page of that and some others [here is](http://www.dodgycoder.net/2011/11/yoda-conditions-pokemon-exception.html). – chris Aug 30 '12 at 20:35
  • Google for: "Yoda Conditions" – Mysticial Aug 30 '12 at 20:37
  • 1
    Yoda conditions are often considered poor practice now, since they are less readable and most compilers warn about assignment anyway. – ssube Aug 30 '12 at 20:39

2 Answers2

1

Any decent compiler will warn you about assignment within a conditional expression, so that form isn't very relevant these days.

On another point "use unsigned for variables that are >= 0 // nice trick " I heard that using unsigned can be confusing and shouldn't use unless there's a reason. Does anyone agree or refute this?

Use unsigned when the value should be unsigned.

Ed S.
  • 115,705
  • 20
  • 165
  • 244
0

This is to avoid the "=" versus "==" mistake. If you mean "==" but type "=" and the object on the LHS is constant, the compiler will complain. For example,

if ( 3 == x )

is preferred to

if ( x == 3 )

because if you type

if ( 3 = x )

then you'll get an error, but if you type

if ( x = 3 )

then you may get a bug!

Bogatyr
  • 18,837
  • 7
  • 56
  • 72