When do you use "if ( a = b )"?
D'Arcy J.M. Cain
darcy at druid.uucp
Tue Mar 19 00:33:48 AEST 1991
In article <65837 at eerie.acsu.Buffalo.EDU> john c chu writes:
>It's been my mistake everytime I've done it!! I realize that it is a
>legal construction and I know what it does, but I was wondering...
>Is there a good use for this?
Certainly:
if (a = b())
...;
Where the body uses the value returned from b(). For readability you
could write:
a = b();
if (a)
...;
but that isn't very efficient. I prefer the following:
if ((a = b()) != 0)
...;
On any reasonable compiler that should generate the same code as the first
example but my intention is perfectly clear. Anyone trying to maintain
my code doesn't have to investigate the surrounding code when he sees that
to figure out if I really meant it.
--
D'Arcy J.M. Cain (darcy at druid) |
D'Arcy Cain Consulting | There's no government
Toronto, Ontario, Canada | like no government!
+1 416 424 2871 |
More information about the Comp.lang.c
mailing list