Assignment in test: OK?

Chris Torek chris at mimsy.umd.edu
Wed Sep 12 17:04:52 AEST 1990


In article <18326 at ultima.socs.uts.edu.au> jeremy at sinope.socs.uts.edu.au
(Jeremy Fitzhardinge) writes:				[??? .edu.au ???]
>... How many would prefer
>	if (thing != NULL)
>	{
>		fp = fopen(thing, "r");
>		if (fp == NULL)
>			barf();
>	}
>to
>	if (thing && (fp = fopen(thing, "r"))
>		barf();
>?

I would, along with most compilers: the second version has an unclosed
left parenthesis. :-)

Anyway, I would (and often do) rewrite examples like the latter as

	if (thing && (fp = fopen(thing, "r")) != NULL)
		barf();

or

	if (thing != NULL && (fp = fopen(thing, "r")) != NULL)
		barf();

(NB: this is what the second quoted example does when the missing close
paren is added, and differs from what the first quoted example does.
In pseudo-code, this is

	if we have a filename and the file can be read
		call barf

while the first quoted example is

	if we have a file name, but the file cannot be read
		call barf

As usual, when making code concise you must be sure to keep it
correct.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list