From: timk@world.std.com (Tim King)
Subject: Re: readable code
Organization: The World Public Access UNIX, Brookline, MA
Date: Thu, 31 Dec 1992 17:56:47 GMT

pdh@netcom.com (Phil Howard ) writes:

>I just saw a posting on comp.lang.c on the usage of ?: and one of the
>replies noted that you could use ?: to select the choice of function
>to be called with one set of argument list.

>C has a lot of features other languages just don't have.

I've always appreciated the power of the ?: operator, and I do not
find that it must necessarily make my code less readable.  The
readability of code should indeed be based upon an experienced
programmer's perspective.  Therefore, any C feature is readable
(to an experienced C programmer) when used appropriately.

As an example, which do you find more readable?

  (a) "I can go home now."

  (b) "Ich kann nun zu hause gehen."
    (Just in case I mangled it, sorry to any of you who know German.
     It's been a while since I took it in high school.)

Naturally, unless you're experienced in the German language, your attitude
will probably be that of Samuel Clemens (see "The Awful German Language"
by Mark Twain)  However, (b), providing I wrote it correctly, is no less
readable than (a) is to its intended audience.

So the question of whether "x = x + 1" or "x += 1" is more "readable" is
really a moot point.  To a C programmer, either will suffice.  To a
BASIC or Pascal programmer, the latter is foreign.  But we shouldn't be
writing C programs for BASIC and Pascal programmers.  They should learn
C if they want to program in it.

Readability is an element of programming style, form, and documentation
quality.  Since C is a free-form language (i.e., there is no fixed visual
form), C programs can be made more readable to C programmers than, for
example, BASIC programs can be made to BASIC programmers.

I find that spacing out expressions, in order to organize my ideas, and
using plenty of comments help the readability of my code.  For example:


x=a[i].value/(y<3?(z>5?5:z<1?1:z):(2*y+3*z)/6));


can be written as:

/* Limits val to a number between lo and hi, inclusive. */
#define Limit(val,lo,hi) (val>hi ? hi : val<lo ? lo : val)

x = a[i].value /                  /* x = the lookup value divided by ..  */
    (y<3
      ?  Limit(z,1,5)             /* .. z, bound to 1..5, if y<3         */
      :  (2*y + 3*z) / 6 );       /* .. or (2y+3z)/6 if y>=3             */


which is, IMO, much more readable than the following, which isn't too
far from the BASIC or Pascal syntax:

if (y<3) {                        /* If y<3,                             */
  if (z>5)                        /*   x = the lookup value / z bound to */
    x = a[i].value/5;             /*     1..5                            */
  elseif (z<0)
    x = a[i].value/1;
} else                            /* If y>=3,                            */
  x = a[i].value/ (2*y + 3*z) / 6;/*   x = the lookup value / ((2y+3z)/6)*/


The ?: form will also generate better code...  unless your compiler is
smart enough to translate the if..else form into ?:-style machine code.

-TimK
-- 
         Tim King         |  In the music industry, bad art is cured by giving
    timk@world.std.com    |  giving bad artists multi-million dollar record
  All original material   |  contracts...  and then playing their songs on
(c) 1992 J. Timothy King <>  the radio. :-) (Inspiried by Barry Shein)
Newsgroups: comp.lang.c