15

While exploring msdn sites ,most of the condition checking places they are using (NULL == bCondition).

what is the purpose of using these notations?

Provide some sample to explain these please.

Thanks.

Greenhorn
  • 658
  • 2
  • 8
  • 24
karthik
  • 16,456
  • 70
  • 72
  • 119

6 Answers6

29

The use of NULL == condition provides more useful behaviour in the case of a typo, when an assignment operator = is accidentally used rather then the comparison operator ==:

if (bCondition = NULL)  // typo here
{
 // code never executes
}

if (NULL = bCondition) //  error -> compiler complains
{
 // ...
}

C-compiler gives a warning in the former case, there are no such warnings in many languages.

David Mason
  • 2,674
  • 3
  • 28
  • 41
karthik
  • 16,456
  • 70
  • 72
  • 119
  • Is he talking about `=` or `==`? – Oscar Mederos May 02 '11 at 07:04
  • I think he is asking about '=='? – Jess May 02 '11 at 07:05
  • 1
    @Jess:The main purpose is that if some one forget to place the assignment operator instead of comparison operator.it will throw error.thats why in msdn sites they are using the comparisons like these NULL == bCondition – karthik May 02 '11 at 07:08
  • +1 - Ok, I didn't see that you were referring to a user typo. – Jess May 02 '11 at 07:11
  • @Oscar, he's saying that NULL = bCondition will be flaged as an error by the compiler. And I recommend reading Expert C Programming: Deep C Secrets, it has great tips and tricks to help avoid mistakes. – shebaw May 02 '11 at 07:11
13

It's called Yoda Conditions. (The original link, you need high rep to see it).

It's meant to guard against accidental assignment = in conditions where an equality comparison == was intended. If you stick to Yoda by habit and make a typo by writing = instead of ==, the code will fail to compile because you cannot assign to an rvalue.

Does it worth the awkwardness? Some disagree, saying that compilers do issue a warning when they see = in conditional expressions. I say that it simply happened just two or three times in my lifetime to do this mistake, which does not justify changing all the MLOCs I wrote in my life to this convention.

Community
  • 1
  • 1
Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
  • 6
    @Flavius: not exactly dead, you just need enough rep to see it. I can't get the trend of moderators on SO to delete everything. It could at least be migrated to Programmers SE. – Yakov Galka Feb 22 '12 at 08:52
  • 1
    There's a [WiKi page](http://en.wikipedia.org/wiki/Yoda_conditions) WiKi page about it now. – Benjamin Jul 17 '14 at 14:08
9

There is no difference. It is an ancient way of defensive programming that has been obsolete for over 20 years. The purpose was to protect from accidentally typing = instead of == when comparing two values. Pascal programmers migrating to C were especially prone to write this bug.

From Borland Turbo C released in 1990 and forward, every known compiler warns against "possibly incorrect assignment", when you manage to type out this bug.

So writing (NULL == bCondition) is not better or worse practice than the opposite, unless your compiler is extremely ancient. You don't need to bother about writing them in any particular order.

What you should bother with, is to adapt a coding style where you never write assignments inside if/loop conditions. There is never a reason to do so. It is a completely superfluous, risky and ugly feature of the C language. All industry de-facto coding standards ban assignment inside conditions.

References:

  • MISRA C:2004 13.1
  • CERT C EXP18-C
Lundin
  • 155,020
  • 33
  • 213
  • 341
  • MSVC, at least by default, doesn't warn you against this bug. – shebaw May 06 '11 at 15:18
  • 2
    @shebaw Impressive that they manage to make a compiler that is worse than TC from 1991. Get a new compiler. – Lundin May 08 '11 at 16:54
  • 2
    +1 for the best answer. I detest Yoda style. There are so many better ways to avoid this flaw in the language than backwards write it around. – David Hammen Jul 26 '11 at 13:39
8

Many people prefer writing NULL == bCondition so that they accidently don't assign the NULL value to bCondition.

Because of typo it happens that instead of writing

bCondition == NULL 

they end up writing

bCondition = NULL // the value will be assigned here.

In case of

NULL = bCondition // there will be an error
Tushar
  • 1,192
  • 1
  • 7
  • 16
4

It's simply a good defensive measure. Some may also find it more convenient to read. In case of a mistyped assignment instead of the equality operator, the compiler will attempt to modify NULL, which is not an lvalue and will produce an error message. When using bCondition = NULL, the compiler might produce a warning about using an assignment as a truth value, a message which can get lost and go unnoticed.

Michael Foukarakis
  • 35,789
  • 5
  • 74
  • 113
1

While usually there is no difference between variable == value and value == variable, and in principle there shouldn't be, in C++ there sometimes can be a difference in the general case if operator overloading is involved. For example, although == is expected to be symmetric, someone could write a pathological implementation that isn't.

Microsoft's _bstr_t string class suffers from an asymmetry problem in its operator== implementation.

jamesdlin
  • 48,496
  • 10
  • 105
  • 134
  • There are valid cases for the asymmetry. E.g., when implementing a smart pointer, it makes perfect sense to have the == operator inside the class which takes raw pointer as an argument. In such situation reverting the order will result in compilation error. – SomeWittyUsername Jul 22 '16 at 08:19