Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <658@haddock.UUCP> Date: Tue, 30-Jun-87 16:00:57 EDT Article-I.D.: haddock.658 Posted: Tue Jun 30 16:00:57 1987 Date-Received: Wed, 1-Jul-87 06:44:38 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <22250@sun.uucp> <17193@cca.CCA.COM> <13008@topaz.rutgers.edu> Reply-To: karl@haddock.ISC.COM (Karl Heuer) Organization: Interactive Systems, Boston Lines: 38 In article <13008@topaz.rutgers.edu> ron@topaz.rutgers.edu (Ron Natalie) writes: >I have always wondered why people think NULL is more mnemonic than 0. >When I read them as exactly the same (and fortunately in C, they are >defined to be). When used properly, NULL refers only to a null pointer constant. Thus, I can read "if (x == NULL)" and know without further context that x is a pointer. (The constant 0 with an explicit (redundant) cast conveys even more information; I use that notation too.) >I also wonder about people who define TRUE to be any thing, since it leads to >things like "if( bool == TRUE )" which is different than "if(!bool)" There is that danger. However, the reason *I* define TRUE (actually I prefer YES, as in K&R) is for assignment ("flag=YES" instead of "flag=1"), not comparison. See above paragraph for reason. >Generally, I use !p when I'm dealing with things that are supposed >boolean values like "if(!isdigit(c))" Good idea. (Btw, note that some implementations of isdigit do not return 1 for success, so "if (isdigit(c) == TRUE)" is bad news.) >MY PET PEEVES: ... >2. Needless use of the comma operator and parenthesis to demonstrate > manhood to the obliteration of code readability, e.g. > if((fd=open("foo",1)<0) > SCREW this, too difficult, how about writing the code to indicate > what is going on: > fd = open("foo", 1); > if(fd == -1) The main advantage of this idiom is for "while" statements. The usual example is "while ((c = getchar()) != EOF) ...", which cannot be written cleanly without the embedded assignment. The use in "if" statements often permits one to collapse nested ifs, which can *improve* code readability. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint