Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ll-xn!mit-eddie!husc6!cca!g-rh From: g-rh@cca.CCA.COM (Richard Harter) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <17171@cca.CCA.COM> Date: Sat, 27-Jun-87 03:41:02 EDT Article-I.D.: cca.17171 Posted: Sat Jun 27 03:41:02 1987 Date-Received: Sun, 28-Jun-87 01:01:58 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> Reply-To: g-rh@CCA.CCA.COM.UUCP (Richard Harter) Organization: Computer Corp. of America, Cambridge, MA Lines: 53 In article <6858@auspyr.UUCP> mick@auspyr.UUCP (Mick Andrew) writes: > >I agree 99.9% with all of his comments on coding style, especially >the awful habit of making code "better" by reducing the total character count. > >In the spirit of > if(p != NULL) vs. if(!p) > >I offer one of my favourite obfuscated one liners: > > if (strcmp(s1, s2) == 0) vs. if (!strcmp(s1, s2)) > > >In general, I try to use the yardstick, >"if it seems like a neat trick, don't do it!" Speaking as one who distinctly falls into the "not minimizing the total character count" school, I fear I must disagree with these specific examples. When I see if (!p) I read it as if p is not valid then ... The (!p) syntax tells me that p is among the class of items that may be treated as boolean (under the C language conventions) and that we are testing whether it is false. This is not a matter of "saving characters"; it is a matter of classification. When I see if (p != NULL) it tells me two rather different things. First of all it tells me that p is an item for which there are one or more coded values, among which is NULL, and that for all cases where p is not NULL, there is some action to be taken. Secondly it tells me that the file that the statement is in includes stdio.h (or that the author of the code is a dweeb.) And that should tell me that the code in this file needs stdio.h, FOR I DO NOT CONSIDER IT GOOD PROGRAMMING PRACTICE TO INCLUDE INCLUDE FILES WHICH ARE NOT USED. This is not an issue of compactness; rather it is an issue of clarity of format. Different people, different strokes, and all that. My vote is for simple clear code, generously commented. But clarity is relative -- if you understand the principles of a particular algorithm or language or the standard conventions of a large program then a piece of code may be quite clear to you and cryptic to someone without that understanding. And I do not really see that one can claim to understand C and also claim that 'if (!p)' is obscure. -- Richard Harter, SMDS Inc. [Disclaimers not permitted by company policy.]