Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!husc6!cmcl2!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <6108@brl-smoke.ARPA> Date: Sun, 12-Jul-87 16:38:12 EDT Article-I.D.: brl-smok.6108 Posted: Sun Jul 12 16:38:12 1987 Date-Received: Mon, 13-Jul-87 04:51:12 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB)) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 49 In article <654@pilchuck.Data-IO.COM> rice@pilchuck.Data-IO.COM (Ken Rice) writes: > while( c=test() != EOF ) >If this code breaks, ... Of course, it's already broken. Presumably you meant while( (c = test()) != EOF ) Don't feel bad; not long after commenting on (!p) vs. (p != NULL), I made a similar slip and wrote that (1 == 0) was precisely 1; I was actually talking about the example (0 == 0) but I (and others) didn't notice my typo. It does add support to the notion that code should be written as straightforwardly as possible; I admit that it's harder to evaluate (1 == 0) mentally than to evaluate 1 -- indeed that's why I was making that response in the first place. > c=test(); > while(c!=EOF) > { > > c=test(); > } Ugh, this Pascalism is horrible! There is conceptually a single operation ("get next character") being performed over and over until the operation fails (EOF). Writing the operation in two places is not only conceptually more difficult, it make it more likely that a future change (perhaps from c=test() to c=GetNextChar()) will miss one of the repeated occurrences, introducing a perhaps subtle bug. The standard C idiom while ( (c = get_next()) != EOF ) do_stuff_with( c ); (with `c' an (int), not a (char)!) may look complicated, but after one gains experience it seems like the "obvious", natural way to write this. Perhaps an ideal programming language would make this something like: until Get_Next_Character named `c' indicates No_More_Chars, Do_Stuff_With `c' but that's not far from the way one should read the C idiom. > return(x=test()); >Then, I want to kill. Especially since 99 times out of 100 this should have been return test(); >Let's write readable and MAINTAINABLE code; Code that we won't have to >change to see how it works. If it reads well and fixes lousy, it's wrong. I agree with the desire but dispute that the Pascalism is more fixable.