Use of expression values in C

Peter Ludemann ludemann at ubc-cs.UUCP
Sun Jul 21 05:03:24 AEST 1985


In article <2600006 at ccvaxa> preece at ccvaxa.UUCP writes:
>
>> For those who think "if ((foo=fopen(filename,"r"))==NULL) { ... }"
>> is hard to understand, why not create a macro to handle this:
>> #define opentest(filename,mode,ptr) ((ptr=fopen(filename,mode)==NULL)
>> and then you can write "if (opentest(filename, "r", foo)) { ... }"
>----------
>Unfortunately, the name 'opentest' doesn't imply that the variable
>named 'foo' has been set to the new fd and that the file is now open.
>... Naming is very tricky.  Doing the operation is very clear.

If doing the operation is so much clearer, why do
we bother with subroutines?  After all, to figure out what
the subroutine does, you've got to look through its code, but
if the operation were done inline, there would be no problem :-)

Perhaps, the solution to all this would be a procedure.
Calling 'openfile(filename, &fileptr, "r")' gets 'fileptr' set
appropriately.  If the open fails a message goes to stderr and
'exit' is called.  Of course, for some software (editors, for example),
this is not desirable behavious, so something else is do
We could go on arguing this forever.

But, for the record, I consider assignments in 'if' statements
dangerous because I expect 'if' statements to only do tests.
Likewise function calls within 'if' statements where parameters
get updated (like the macro I gave above).  Likewise cryptic stuff like:
    while (*s++ = *t++); 
Use 'strcpy' instead!
Trying for micro-efficiencies at the cost of obscure code
is the sign of an immature programmer or a poor typist.  Run
a profiler on your programmer if you think it needs speeding up -
you'll probably find something really gross that slipped by you
while you were trying to construct an 'if' statement with three
'++'s imbedded in it.



More information about the Comp.lang.c mailing list