When do you use "if ( a = b )"? (was Re: Funny mistake)
Mark William Hopkins
markh at csd4.csd.uwm.edu
Mon Apr 1 12:46:56 AEST 1991
In article <15053 at ganymede.inmos.co.uk> conor at inmos.co.uk (Conor O'Neill) writes:
>To me it is clear that using assignments inside conditions is very bad
>style because it makes the intention of the original programmer unclear.
Then render this in a suitable format that avoids needless redundancy:
if ((A = f1()) != 2 || (A = f2()) != 3 || (A = f3()) != 4)
f4(A);
else
Error(A);
I believe that your proposed definition of clarity would be turned on its head
with this example.
Mixing imperatives with expressions accords well with the style of natural
language (as opposed to a purely logical style), and thus can read better.
This especially goes for the logical operators, which actually translate
better as natural language connectives (with all its temporal connotations)
than pure logic connectives (e.g. and = and then).
Witness this classic (I saw something like it in YACC source):
(A = new-object()) || Error(A);
Translation: "Make a new object or bust."
Or...
(Op = FindOp(S)) && return Op->Type ||
(Sym = NewSym(S)) && return Sym->Index;
Translation:
"Find an operator to match the string S and return its type,
or make a symbol to match S and return its index."
More information about the Comp.lang.c
mailing list