Use of expression values in C
preece at ccvaxa.UUCP
preece at ccvaxa.UUCP
Wed Jul 17 00:26:00 AEST 1985
> 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.
It implies (to me, at least) a test of whether it is possible to
open the file. Now, if you wanted to call it "TRY_TO_OPEN", I would
be more likely to interpret the name correctly, but some people might
interpret that name as a Boolean indicating whether or not to try to
open the file. Naming is very tricky. Doing the operation is very
clear.
There's always room to argue about whether a particular expression
is too complex. In this case I don't think anyone would have difficulty
with the original form. The insightful programmer tries to notice and
simplify expressions that are likely to be a problem. Arbitrary rules
are unlikely to be satisfying.
--
scott preece
gould/csd - urbana
ihnp4!uiucdcs!ccvaxa!preece
More information about the Comp.lang.c
mailing list