Equal to operand

R

rasayani

Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}
 
J

Joona I Palaste

rasayani said:
Does anyone know what the advantage is of using the following statement
char *p = NULL;
if (NULL == p)
{


char *p = NULL;
if (p == NULL)
{

The advantage is that if you mistakenly type = instead of ==, the first
form will cause a constraint violation or a syntax error, while the
second form will silently compile and cause p to always be NULL.
 
R

Richard Bos

Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}

Yes. It will confuse newbies. It is also said to have a beneficiary
effect on typos, but I suspect the placebo effect.

Richard
 
C

Christian Bau

Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}

Some people use the first form because in that case typing "=" instead
of "==" by accident will produce a syntax error that the compiler will
catch.

On the other hand, turn all warnings on on your compiler (I certainly
hope you do that), and it is most likely that you will get a warning for

if (p = NULL)

as well. And the first version is less readable, which will cost you
long term in maintenance cost.
 
C

Christopher Benson-Manica

Joona I Palaste said:
The advantage is that if you mistakenly type = instead of ==, the first
form will cause a constraint violation or a syntax error, while the
second form will silently compile and cause p to always be NULL.

Although some helpful compilers may warn you about a possibly
incorrect assignment. Such warnings are a double-edged sword, because
you get reprimanded whether you actually made a mistake or not.
 
J

John Bode

rasayani said:
Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}

If you mistakenly type

if (p = NULL) {...}

then p will be assigned NULL instead of compared against null, and the
branch will never be taken, which is a bug. On the other hand, if you
mistakenly type

if (NULL = p) {...}

you will raise a diagnostic at compile time, since NULL cannot be assigned
to. So the NULL == p form is a good way to catch a certain class of typo,
but only in cases where one of the operands is not an lvalue.

For this *specific* instance, you can avoid the headache completely by
typing

if (!p) {...}

since !p is equivalent to p == NULL.

It would have been nice if dmr had realized early on that allowing
assignment expressions in conditionals could lead to problems, or if he had
used a different operator for assignment or equality (i.e., := and ==, or =
and :=, or something where the two operators don't start with the same
character). But he didn't, and we're stuck with the consequences.
 
R

red floyd

Christian Bau said:
[redacted]
Some people use the first form because in that case typing "=" instead
of "==" by accident will produce a syntax error that the compiler will
catch.

Also because people take MS samples as gospel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top