home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19083 < prev    next >
Encoding:
Text File  |  1992-12-31  |  3.5 KB  |  88 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!world!timk
  3. From: timk@world.std.com (Tim King)
  4. Subject: Re: readable code
  5. Message-ID: <C04xuo.476@world.std.com>
  6. Organization: The World Public Access UNIX, Brookline, MA
  7. References: <1992Dec31.020953.12265@netcom.com>
  8. Date: Thu, 31 Dec 1992 17:56:47 GMT
  9. Lines: 77
  10.  
  11. pdh@netcom.com (Phil Howard ) writes:
  12.  
  13. >I just saw a posting on comp.lang.c on the usage of ?: and one of the
  14. >replies noted that you could use ?: to select the choice of function
  15. >to be called with one set of argument list.
  16.  
  17. >C has a lot of features other languages just don't have.
  18.  
  19. I've always appreciated the power of the ?: operator, and I do not
  20. find that it must necessarily make my code less readable.  The
  21. readability of code should indeed be based upon an experienced
  22. programmer's perspective.  Therefore, any C feature is readable
  23. (to an experienced C programmer) when used appropriately.
  24.  
  25. As an example, which do you find more readable?
  26.  
  27.   (a) "I can go home now."
  28.  
  29.   (b) "Ich kann nun zu hause gehen."
  30.     (Just in case I mangled it, sorry to any of you who know German.
  31.      It's been a while since I took it in high school.)
  32.  
  33. Naturally, unless you're experienced in the German language, your attitude
  34. will probably be that of Samuel Clemens (see "The Awful German Language"
  35. by Mark Twain)  However, (b), providing I wrote it correctly, is no less
  36. readable than (a) is to its intended audience.
  37.  
  38. So the question of whether "x = x + 1" or "x += 1" is more "readable" is
  39. really a moot point.  To a C programmer, either will suffice.  To a
  40. BASIC or Pascal programmer, the latter is foreign.  But we shouldn't be
  41. writing C programs for BASIC and Pascal programmers.  They should learn
  42. C if they want to program in it.
  43.  
  44. Readability is an element of programming style, form, and documentation
  45. quality.  Since C is a free-form language (i.e., there is no fixed visual
  46. form), C programs can be made more readable to C programmers than, for
  47. example, BASIC programs can be made to BASIC programmers.
  48.  
  49. I find that spacing out expressions, in order to organize my ideas, and
  50. using plenty of comments help the readability of my code.  For example:
  51.  
  52.  
  53. x=a[i].value/(y<3?(z>5?5:z<1?1:z):(2*y+3*z)/6));
  54.  
  55.  
  56. can be written as:
  57.  
  58. /* Limits val to a number between lo and hi, inclusive. */
  59. #define Limit(val,lo,hi) (val>hi ? hi : val<lo ? lo : val)
  60.  
  61. x = a[i].value /                  /* x = the lookup value divided by ..  */
  62.     (y<3
  63.       ?  Limit(z,1,5)             /* .. z, bound to 1..5, if y<3         */
  64.       :  (2*y + 3*z) / 6 );       /* .. or (2y+3z)/6 if y>=3             */
  65.  
  66.  
  67. which is, IMO, much more readable than the following, which isn't too
  68. far from the BASIC or Pascal syntax:
  69.  
  70. if (y<3) {                        /* If y<3,                             */
  71.   if (z>5)                        /*   x = the lookup value / z bound to */
  72.     x = a[i].value/5;             /*     1..5                            */
  73.   elseif (z<0)
  74.     x = a[i].value/1;
  75. } else                            /* If y>=3,                            */
  76.   x = a[i].value/ (2*y + 3*z) / 6;/*   x = the lookup value / ((2y+3z)/6)*/
  77.  
  78.  
  79. The ?: form will also generate better code...  unless your compiler is
  80. smart enough to translate the if..else form into ?:-style machine code.
  81.  
  82. -TimK
  83. -- 
  84.          Tim King         |  In the music industry, bad art is cured by giving
  85.     timk@world.std.com    |  giving bad artists multi-million dollar record
  86.   All original material   |  contracts...  and then playing their songs on
  87. (c) 1992 J. Timothy King <>  the radio. :-) (Inspiried by Barry Shein)
  88.