I’m inclined to write if statements by using logical negation operator:
if (!p)
some_code();
Some people around me tend to use explicit comparison, so that the code looks like:
if (FOO == p)
some_code();
where FOO is one of false, FALSE, 0, 0.0, NULL, etc.
I prefer the short form because it is:
operator!=friendlygeneric programmingfriendly- laconic (and even more beautiful, as for me)
What are the pragmatic benefits of writing this otherwise (if any)?
Some claim that the pragmatic benefit is that programmers will find it easier to understand if you explicitly compare against NULL, FALSE, 0, etc., whereas the logical operator may be confusing to people who don’t understand how implicit conversions and booleans work in C/C++.
(Disclaimer: I don’t share this view myself.
if (p) ...andif (!p)...are the idiomatic ways to express this in C and C++, and programmers who have trouble understanding them have no business touching C or C++ code. Heath Hunnicutt’s comment is dead on.)