Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!gatech!hao!husc6!cmcl2!brl-adm!adm!dsill@NSWC-OAS.arpa From: dsill@NSWC-OAS.arpa (Dave Sill) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <8270@brl-adm.ARPA> Date: Mon, 13-Jul-87 11:42:23 EDT Article-I.D.: brl-adm.8270 Posted: Mon Jul 13 11:42:23 1987 Date-Received: Tue, 14-Jul-87 05:40:56 EDT Sender: news@brl-adm.ARPA Lines: 43 John Haughwrote: >I ask the group, [which] do you prefer? > > if (access (file, 0)) { > fd = open (file, 0); > if (fd >= 0) > good_stuff (); > else > error_handler (); > } else > error_handler (); > > - or - > > if (access (file, 0) && (fd = open (file, 0)) >= 0) > good_stuff () > else > error_handler (); In general I agree with John. When possible, complex nested conditional structures should be rewritten as complex conditional expressions in a single structure. However, one must be careful to do this only when it is appropriate. For example, in code like the above, where there are two separate calls to error_handler() it would be preferable, if generating an error message, to be able to identify the exact cause of the error. If the file can't be access'd, the first structure would allow a message like "File exists but you can't access it" (assuming the file had already been successfully stat'd), while the second would have to say something like "Couldn't open file." It's important not to let the structure of the code determine the functionality of the program (aka "the tail wagging the dog" syndrome). The desired functionality should be predetermined before the code writing is done. -Dave Sill dsill@nswc-oas.arpa The opinions expressed above are those of the author and do not necessarily reflect those of the Department of Defense or the U.S. Navy.