On Fri 23 Nov 2007 at 03:59:05 -0600, Matthew D. Fuller wrote:
> > > - Implicit comparisons as boolean (e.g., "if(x)" instead of
> > >   "if(x!=NULL)" and equivalents for numerics) bad.
> > 
> > Why?
> 
> Because they rely on side effects of conversion to a boolean
> expression, and they don't make it clear exactly what you're testing
> unless you already know exactly what the expression yields.  With
> explicit ">0" and "!=NULL", you do.  And it can matter with signed
> values; "!=0" and ">0" are two different assertions, but both evaluate
> to true.

I read "if (x) ..." as "if there is an x, ..." which maps nicely to both
integral and pointer types for x.

It also nicely fits with the definition of the if statement: "in both
forms, the first substatement is executed if the expression compares
unequal to 0". (Section 6.8.4.1 para 2;
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf page 145) 

So what if you're not certain the right thing happens if you write
"if (x)", and feel the need to write "if (x != 0)", what happens is
this: you again have an expression of integral type which is 0 or not.
The new, "better" condition is no better than the one there was before.

So, to be really really sure, you'd have to write "if ((x != 0) != 0)".
But this is still no good, it is still an integral expression! Quick,
make it "if (((x != 0) != 0) != 0)"!

Etcetera ad absurdum.

-Olaf.
-- 
___ Olaf 'Rhialto' Seibert      -- You author it, and I'll reader it.
\X/ rhialto/at/xs4all.nl        -- Cetero censeo "authored" delendum esse.

Reply via email to