Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!pt!ius2.cs.cmu.edu!edw From: edw@ius2.cs.cmu.edu (Eddie Wyatt) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <1217@ius2.cs.cmu.edu> Date: Mon, 29-Jun-87 15:30:58 EDT Article-I.D.: ius2.1217 Posted: Mon Jun 29 15:30:58 1987 Date-Received: Sun, 5-Jul-87 20:18:36 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <13008@topaz.rutgers.edu> Organization: Carnegie-Mellon University, CS/RI Lines: 120 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. > > 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) > > -Ron > > Comment 1. Its all a question of taste. Comment 2. Though the code may be logically equivalent, the assemble actually generated differs. Example for Suns 3.2 cc: Consider the program main() { int x,y; if (x=y); } Running cc -S over it yields : LL0: .data .text |#PROC# 04 .globl _main _main: |#PROLOGUE# 0 link a6,#0 addl #-LF12,sp moveml #LS12,sp@ |#PROLOGUE# 1 movl a6@(-0x8),a6@(-0x4) jeq L14 L14: LE12: unlk a6 rts LF12 = 8 LS12 = 0x0 LFF12 = 8 LSS12 = 0x0 LP12 = 0x8 .data Consider the program main() { int x,y; x = y; if (x); } Running cc -S over it yields : LL0: .data .text |#PROC# 04 .globl _main _main: |#PROLOGUE# 0 link a6,#0 addl #-LF12,sp moveml #LS12,sp@ |#PROLOGUE# 1 movl a6@(-0x8),a6@(-0x4) tstl a6@(-0x4) ; NOTE the extra tstl jeq L14 L14: LE12: unlk a6 rts LF12 = 8 LS12 = 0x0 LFF12 = 8 LSS12 = 0x0 LP12 = 0x8 .data Note the extra test instruction. So the two methods are not totally equivalent. I prefer "if ((fd = open("foo",O_WRONLY)) < 0)". Major difference being, I parathesize the operation. Just a matter of taste, that's all. Also another note, any good (even bad) optimizing compiler should reckonize that the tstl is over kill and remove it. -- Eddie Wyatt e-mail: edw@ius2.cs.cmu.edu terrorist, cryptography, DES, drugs, cipher, secret, decode, NSA, CIA, NRO.