Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site ubc-cs.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!alberta!ubc-vision!ubc-cs!ludemann From: ludemann@ubc-cs.UUCP (Peter Ludemann) Newsgroups: net.lang.c Subject: Re: Use of expression values in C Message-ID: <1172@ubc-cs.UUCP> Date: Sat, 20-Jul-85 15:03:24 EDT Article-I.D.: ubc-cs.1172 Posted: Sat Jul 20 15:03:24 1985 Date-Received: Thu, 25-Jul-85 06:54:36 EDT References: <5764@utzoo.UUCP> <2600006@ccvaxa> Reply-To: ludemann@ubc-cs.UUCP (Peter Ludemann) Organization: UBC Department of Computer Science, Vancouver, B.C., Canada Lines: 35 In article <2600006@ccvaxa> preece@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.