Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!sri-spam!ehrhart From: ehrhart@aai8..istc.sri.com (Tim Ehrhart) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <10509@sri-spam.istc.sri.com> Date: Mon, 29-Jun-87 16:00:48 EDT Article-I.D.: sri-spam.10509 Posted: Mon Jun 29 16:00:48 1987 Date-Received: Wed, 1-Jul-87 00:37:44 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <22250@sun.uucp> <17193@cca.CCA.COM> <13008@topaz.rutgers.edu> Sender: nobody@sri-spam.istc.sri.com Reply-To: ehrhart@spam.istc.sri.com (Tim Ehrhart) Organization: SRI International Lines: 50 Keywords: have my cake and eat it too In article <13008@topaz.rutgers.edu> ron@topaz.rutgers.edu (Ron Natalie) writes: >MY PET PEEVES: > >1. Comparing error returns from UNIX syscalls to be less than zero. > UNIX system calls that return ints, are usually defined to return > -1 on error. It drives me crazy to see code test for less than > zero. It doesn't say returns negative value on error, it says > -1. I'd guess some folks do that because they know that most of today's current microprocessors have more than enough bits in their status register to be able to know if a value is negative or not. This saves the CPU from actually having to do the compare of two values, then looking at it's status register bits to determine the result. What this really means in terms of microprocessor instructions is basically a single operand instruction versus two double operand instructions. For example on a 680x0, here's the assembler code: if ( x < 0) case: tstl d0 | test on value in d0 if ( x == -1) case: moveq #-1,d7 | load -1 into register d7 (if == -1) cmpl d7,d0 | compare d0 with d7 I know it's only a few assembler instructions and a few microseconds, but if I can get small optimizations like this at no cost I'll take them everytime. Using clearly stated '#defines' is place of the magic numbers 0 and -1 is my way of not compromising clarity and readability. >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) > Once again to save instructions. The function return value is usually already in d0, and it needs only check it's status bits for negative, etc.. as opposed to loading -1 into another register, then comparing the two register values. Sorry for getting so low level, but I think sometimes that is where the answers or the motivation for things lie. And no, I don't think "coding" for these hardware features are non-portable or cutesy. Just getting the job done with littlest work possible without compromising read- ability or maintainability. I can assure these two issue are very important to me also. Tim Ehrhart SRI International Menlo Park, CA 94025 415-859-5842