Distinguishing Between '(bCondition == NULL)' and '(NULL == bCondition)'
In various programming contexts, particularly when referencing MSDN resources, you might encounter two seemingly interchangeable conditional expressions: '(bCondition == NULL)' and '(NULL == bCondition)'. While they might appear equivalent at first glance, there is a subtle difference in their behavior when handling potential typos.
When comparing variables to NULL, the use of '(NULL == bCondition)' is preferred for better error detection in case of accidental misplacement of the assignment operator '=' instead of the comparison operator '=='. Consider the following example:
Code:
if (bCondition = NULL) // Typos { // Code never executes }
In this instance, a typo has occurred where '=' (assignment) was mistakenly used instead of '==' (comparison). This results in the conditional statement always evaluating to 'false' because the assignment operator assigns NULL to 'bCondition' instead of comparing it. Consequently, the code within the block will never be executed.
On the other hand, if we use '(NULL == bCondition)' instead:
Code:
if (NULL = bCondition) // Error { // ... }
In this case, the compiler would detect the typo and issue an error indicating that the assignment of NULL to bCondition is an invalid operation. This error detection serves as a safeguard against accidental assignments that could potentially lead to unexpected program behavior.
Therefore, the notation '(NULL == bCondition)' is generally recommended for checking conditions with NULL as it provides more robust error handling in the event of typos.
The above is the detailed content of Why is `(NULL == bCondition)` Preferred Over `(bCondition == NULL)` in C/C ?. For more information, please follow other related articles on the PHP Chinese website!