Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 419
karthik Avatar asked May 02 '11 06:05

karthik


People also ask

Can you use == for null?

equals(null) will always be false. The program uses the equals() method to compare an object with null . This comparison will always return false, since the object is not null .

IS null == null in JS?

Summary. null is a special value in JavaScript that represents a missing object. The strict equality operator determines whether a variable is null: variable === null .

How do you know if a condition is null?

Use an "if" statement to create a condition for the null. For example, if the value is null, then print text "object is null". If "==" does not find the variable to be null, then it will skip the condition or can take a different path.

IS null conditional statement?

The null conditional is a form of a member access operator (the .). Here's a simplified explanation for the null conditional operator: The expression A?. B evaluates to B if the left operand (A) is non-null; otherwise, it evaluates to null.


Video Answer


2 Answers

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.

like image 52
karthik Avatar answered Nov 17 '22 06:11

karthik


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.

like image 36
Yakov Galka Avatar answered Nov 17 '22 08:11

Yakov Galka