Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!ddl From: ddl@husc6.UUCP (Dan Lanciani) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <2430@husc6.UUCP> Date: Tue, 30-Jun-87 20:30:34 EDT Article-I.D.: husc6.2430 Posted: Tue Jun 30 20:30:34 1987 Date-Received: Wed, 1-Jul-87 07:21:57 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <1176@copper.TEK.COM> Organization: Harvard University Computer Services Lines: 78 In article <1176@copper.TEK.COM>, stevesu@copper.TEK.COM (Steve Summit) writes: | It has been pointed out that my original posting had a blatant | error in it: (!p) and (p != NULL) are not at all equivalent, but | in fact are exactly opposite. I was at first acutely embarrassed, | but I am now flatly amazed that the statements have been argued | so hotly, by some of the best minds on the network, without | noting this crucial fact. I sincerely doubt that the "best minds on the network" are the ones doing the arguing. Nevertheless, I noted this "crucial fact" and hence my question as to whether you had really mastered the idiom. I guess I was too subtle and you missed the point. | The fact that this error is so easy to make (I spent several | hours on the original posting, and I read through it countless | times, and I still missed it) ends up illustrating the point very | well: when coding, say what you mean. I really hope you mean the fact that this error is so easy for YOU to make illustrates the point that YOU need to be very careful when coding. The error was immediately obvious to me and to the only other person I mentioned it to. When I code "if(!p)" I say exactly what I mean and I mean what I say. Your reasoning is about as good as my saying, "Since I can't fly an airplane, it is clearly impossible to do so." | Since I started this, I might as well make my thinking process | clear: we have a pointer variable, and it either holds or does | not hold a single "out of band" nil value. The closest | transliterations of those English statements into C are | | if(p == NULL) | and | if(p != NULL) | | (assuming a proper #definition of NULL). I would claim that the closest transliterations are "if(!p)" and "if(p)," if only because they DON'T assume a proper #define of ANYTHING. | In my opinion, the most important thing is to keep that | transliteration step simple. Again, C has made that step very simple for you by providing the "if(p)" construct. Of course, this is my opinion. | Everything else (compilation time, | efficiency, source code character count) is secondary. Other than the fact that I happen to find less cluttered code easier to read, the issue of character count is a joke. Compilation time is a programmer resource and can often be adjusted... But efficiency is not always a secondary issue. In fact, it is RARELY a secondary issue. Sometimes efficiency makes the difference between an application's working or failing. More often however, this attitude encourages the production of needlessly slow programs with which users must suffer for years. | The | biggest problem in Software Engineering is not code size or | efficiency but correctness: a program is supposed to do what you | want it to do, but rarely does. The more I can make my C code | "read" like an English description of what the code is supposed to | do, the fewer transliteration errors I will make, and the easier | my code will be for me, and others, to read. Maybe you should try COBOL. It was designed to read like an English description. C was not. Why not let C read like a C description of the problem? When I program in C I do not transliterate from English because I find C to provide a better mechanism for describing many problems. This is my personal style. You have your own style. I have no trouble reading code written in either style. Can't we leave it at that rather than trying to prove that one style somehow encourages correctness and another style tends to discourage it? Dan Lanciani ddl@harvard.*