Difference Between These (Bcondition == Null) and (Null==Bcondition)

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

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.

Test conditions in C++

Just a good practice. If by mistake, you use single = instead of ==, finding the bug in the first one is very difficult but the second one would fail to compile clearly showing the line of error.

Logical if difference

Actually there is no difference in both performance wise or in functional perspective. I can say it's a personal choice of coding style often called that as Yoda Style

In programming jargon, Yoda conditions (also called Yoda notation) is a programming style where the two parts of an expression are reversed in a conditional statement.

I prefer the later way as the first way kills the readability of code (at least for me)

What is the meaning of NULL != value in C++?

No, there is no difference. In case of == there might be some difference. The thing is that if you accidentally write = instead of == the compiler will give an error in the first case.

if (threadInfo = NULL) //always false. The compiler will give you a warning at best

if (NULL = threadInfo) //Compiler error

I personally hate that practice and think it's better to write code that can be read in a normal human language, not Yoda language.

what is the standard guideline to declare the == condition?

The only reason to write:

5 == variable

instead of

variable == 5

is that in the former case if you incorrectly put an assignment (single =) in place you will get a compile time error because you are trying to overwrite a constant.

However any decent compiler will give you a warning if you do:

if (variable = 5)

so IMHO it's not worth worrying about. I always use the latter if (var == num) form.


However in Java there is a common pattern that is worth using. When testing a string for equality, one should use:

if ("constant".equals(variable)) { ... }

instead of:

if (variable.equals("constant")) { ... }

since the latter can trigger a null pointer exception, and the former cannot.

The order of expressions in an if statement

1 === variable1 is same as the expression variable1 === 1 written in Yoda notation**: constant listed on left hand side, variable on the right hand side.

The main reason why some programmers choose to use it is to avoid the common mistake of writing if (a = 1) where the programmer actually meant if (a == 1) or if (a === 1). The following line of code will work but not as expected (a is assigned a value and if block will always get executed):

if (a = 1) {}

The same expression written the other way round will generate a syntax (or compile) error:

if (1 = a) {}

The programmer can immediately spot the error and fix it.

I do not like or use the Yoda notation. I try to keep my eyes open while coding.

** I am unable to find out the origin of this term.

Proper form in C++

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.



Related Topics



Leave a reply



Submit