The D Programming Language: values and voids (was: == vs =)
Dave Decot
decot at hpisod2.HP.COM
Wed Apr 13 15:18:30 AEST 1988
> I think this is better written as "if ((status = *statreg))"; some
> people even recommend "if ((status = *statreg), status)".%
>
> If you write the if as "if (a = b)", many readers of your code
> (possibly including yourself) will first think "aha, this is a typo".
> Many other readers will read it as "if (a == b)". It is therefore a
> good idea to use a visually distinct form. The two forms I list above
> both result from the idea that `=' should not be the top level operator
> in a conditional expression.
I agree with the reasoning, but I think an even clearer way of expressing
what you really mean is this:
if ((status = *statreg) != 0)
...
Generally, I will only use an implied comparison to 0 in conditional
contexts when the name of a "Boolean" variable or macro returning
a purely "Boolean" result is the entirety of the condition.
For instance,
if (strcmp(x, y) != 0)
...
but
if (isprint(*p))
...
Dave Decot
hpda!decot
More information about the Comp.lang.c
mailing list