Readability in Programming Languages

Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

Readability in Programming Languages

  • Comments 37

I saw a side by side comparison of a bunch of scripting languages online recently. Scripting Languages: PHP, Perl, Python, Ruby My first, and second reaction was yuck! Now I have my biases – biases which may  not be shared by others of course. But I like readable code and for me anytime I see a special character (anything not an alphanumeric) it slows me down. This got me thinking about where we are going in design of programming languages? Are we moving forward (what ever that means) or backwards or just sideways?

Back when I started programming close to 40 years ago the big three programming languages were FORTRAN and COBOL with an up an coming language called BASIC. FORTRAN (short for FORmula TRANSlation) was used by mathematicians (my math major brother had to learn it) and scientists. COBOL (the B stands for Business) was used for business applications. BASIC was a teaching/Learning language that was spreading into business. COBOL was both loved and hated by different people for its wordiness. But it was at least understandable. Take the loop below:

PERFORM VARYING WS-BOTTLE-NUM FROM 98 BY -1
               UNTIL WS-BOTTLE-NUM < 2
END-PERFORM

Pretty close to an English sentence. Compare that to this sample for a C-style language (C#)

for (WSBOTTLENUM = 98; WSBOTTLENUM >= 2; WSBOTTLENUM--)
{
}

Which one is more obvious? Pretend you are not an experienced programmer.

BASIC (Visual Basic in this case) is somewhere in the middle.

For WSBOTTLENUM = 98 To 1 Step -1
 
Next

The step – the counting down – is more easily understandable for me at least. Now let’s take a look at something very simple. k = i / 10;

This drives beginners crazy. What’s going on here? Sure we programmers know but a lot of beginners struggle with which direction the operation is going. Compare that to the same code in COBOL

divide i by 10 giving k

Wordy? Sure, but at least even a beginner can read it. Now I am not arguing that we should all go back to COBOL though honestly with modern IDEs and features like Intellisence it would be a lot easier than it was back when I was typing out punch cards. Rather I am suggesting that beginner languages can and probably should be more wordy rather than more obscure – that English is easier to pick up than “what does # in this programming language mean?”

Just for fun, if you want to see what different programming languages really look like visit the 99 Bottles of Beer site.

This Website holds a collection of the Song 99 Bottles of Beer programmed in different programming languages. Actually the song is represented in 1434 different programming languages and variations.

Somewhat related posts:


  • With mainstream languages, Fortran came out in 1957 and Cobol shortly after that (1962).  The state-of-the-art today (2011) arguably is Java, maybe C#.  Not exactly brilliant progress for 50 years.

  • For non-English speaking programmers, the closeness to English is just a verbosity.

    BTW, as I remember, part of "C" popularity stemmed from its getting rid of all this English nonsense of Pascal:

    DECLARE A AS ARRAY OF INTEGER

    is nice in a school class, but if you have to type it (or God forbid, on a punch-writer), you quickly start to appreciate

    int[] A;

    In the end, you learn the language once, but you type every day :)

  • How about an example in APL to really screw with your head.

    This following immediate-mode expression generates a typical set of Pick 6 lottery numbers: six pseudo-random integers ranging from 1 to 40, guaranteed non-repeating, and displays them sorted in ascending order:

    x[⍋x←6?40]

  • My favourite English go to language is Revolution (AKA LiveCode) it originally came from MetaCard which was an Apple HyperCard system but designed for Windows and Unix, now Rev supports all three platforms. Nothing like:

    Local tFadeLimit

    Put 100 Into tFadeLimit

    Repeat For 100 Times

      Subtract 1 From tFadeLimit

      Set The BlendLevel Of This Stack To tFadeLimit

      Wait For 1 Tick

    End Repeat

    For a window that fades into view. :D

  • COBOL was supposed to be usable by management and self documenting. The usable by management part never worked out. It was used in business environments with their big 600 lpm printers. The folks that developed C were people who were programming on ASR-33 teletypes running at 110 baud (~ 10 chars/second). They had no time for a wordy language!

  • Code in modern languages is less readable to non-programmers... so what?

    You wouldn't want non-programmers thinking they can read or — gasp! — write code, do you?

  • It depends on how the characters are used, not just what they are.  In a language like Tcl or Scheme or Forth, the syntax is extremely simple.  The documentation has a big list of symbols so if you see "%%#!@" in code you don't have to dig through language syntax manuals to try to parse it.  Just look up that symbol in the index.

    Your example of "k=i/10" is a good one: in COBOL it's easier for someone who's never written code to read it, but can someone who's trying to write it learn anything from that?  What if you want to add another subexpression?  Now you'll need to learn the syntax for that, too.  Calling a "set!" function is both easy for a C programmer to learn, and for a newbie to learn, and also makes it easy to extend this knowledge.

  • for (WSBOTTLENUM = 98; WSBOTTLENUM < 2; WSBOTTLENUM--){

    }

    Yeah this will run the block exactly once. I am not able to pretend I were not a programmer, but to me something like

    do { /* */ wsb++; } while ( wsb < 98 );

    seems to be easier then to remember syntax that looks like english, but doesn't follow syntactical or grammatical rules.

  • Wordy stuff is OK if you can type.  Most programmers are still 2-6 finger typists.  A perform statement would take them a lot longer than something with braces and parentheses.  Like any language, it is a matter of getting used to.  In GEORGE III (ICL1900 JCL), we could write

    WE COMERR, GO L100

    In its long form (and in GEORGINA), this was

    WHENEVER COMMAND ERROR, GOTO L100

    Initially, everyone used the WHENEVER version.  After 30 minutes of two finger typing and a lot of swearing, the class magically shifted to the two letter acronyms.  Maybe that is what we need in programming languages: a long form and a short form and a mix of both.

  • Hey - the C# should read:

    for (WSBOTTLENUM = 98; WSBOTTLENUM > 1; WSBOTTLENUM--) { }

    I would point out that "divide i by 10 giving k" is all fine, but now try writing "k=((3+i)*(10+(j-1)))*5" in COBOL and you might find out that it's not readable any longer at all. COBOL also features virtually no control structures, an utter lack of classes and objects, and so on. If you want to teach people Pidgin English, it's fine, but: "the teaching of COBOL cripples the mind irreversibly and should be regarded as a capital offence" as Dijkstra once said... and he was correct.

  • Hey - the C# should read:

    for (WSBOTTLENUM = 98; WSBOTTLENUM > 1; WSBOTTLENUM--) { }

    I would point out that "divide i by 10 giving k" is all fine, but now try writing "k=((3+i)*(10+(j-1)))*5" in COBOL and you might find out that it's not readable any longer at all. COBOL also features virtually no control structures, an utter lack of classes and objects, and so on. If you want to teach people Pidgin English, it's fine, but: "the teaching of COBOL cripples the mind irreversibly and should be regarded as a capital offence" as Dijkstra once said... and he was correct.

  • First let me destroy your very first idea: It is readable if and ONLY if you are a English speaker...

    I speak English, but my birth language is Italian and I've grow up speaking Spanish. Now think on the thousands of people whom don't even use this alphabet system, but instead Cyrillic, Arabic, Indian, Chinese, Japanese... etc etc. :p.

    Also there are a very limited number of constructs like the for loops, for which a symbolic structure actually makes it easier to learn.

    Finally once you learn a language you want to be able to do as much as possible with as little words, as a C oriented programmer i totally hate anything longer that { and } to establish a code block.

  • I don't think you were paying attention to Python. It doesn't get much more concise and readable than that.

  • @N: You should reread the C syntax; in THIS form the code will execute ZERO times (abort condition is checked before the loop is entered) ...

  • I think the comparison to C is a bit unfair; C was never meant to be very readible, but to be efficient.

    What would the following loop look like in COBOL:

    while ( *dest++ = *src++ )

      ;

Page 2 of 3 (37 items) 123