On 1/30/2010 9:40 AM, Tim Streater wrote:
> On 30/01/2010 13:28, Ben Bacarisse wrote:
>> Tim Streater<> writes:
>> <snip>
>>> And I prefer to write "==true" because it makes my code more readable
>>> to me.
>>
>> You are probably still talking about PHP, but just in case this gets
>> taken as a more general point, it is unwise to do that in C because
>> true must be just one value and any non-zero values is acceptable as
>> "not false" (such as those that the isxxxx functions might return).
>>
>>> If I have "if (x>3) { ..." I can read this as "if x is greater
>>> then 3 then do so-and-so". I read "if (x) { ..." as "if x then do
>>> so-and-so". WTF? If x *what*? Forgot to put the cat out? Needs its
>>> hair cut?
>>
>> I'd say that is the fault of whoever chose x as the name. If x is a
>> boolean, the name should indicate WTF: if (x_needs_hair_cut)... If x
>> is not boolean (say it is the number of until a hair cut is required)
>> I would test against 0: if (x_days_to_hair_cut == 0)...
>
> Could do that I suppose, but "if (debugflag_is_true) { ..." gets tedious
> quite quickly.
You'd prefer `if (debugflag_is_true == true)', I guess?
There's also the question of when to stop:
if (x)
if (x == true)
if (x == true == true)
if (x == true == true == true)
...
It's mostly a matter of personal style, but I've never
found a convincing reason (in any language) to compare with a
Boolean constant. I like explicit comparisons of pointers
to NULL and numbers to zero: `if (ptr != NULL)' rather than
`if (ptr)', but it's just a preference. (I particularly abhor
`if (!strcmp(answer,"no"))', which reads so wrongly it rouses
righteous rage.)
Also, as mentioned up-thread, `if (isdigit(ch) == true)'
is just plain wrong, R-O-N-G, wrong.
--
Eric Sosman
lid