Of Languages and Keywords

2012-05-28 18:40

I recently had a discussion with a co-worker about feasibility of using anonymous functions in Python. We happen to overuse them quite a bit and this is not something I’m particularly fond of. For me lambdas in Python are looking pretty weird and thus I prefer to use them sparingly. I wasn’t entirely sure why is it so – given that I’m quite a fan of functional programming paradigm – until I noticed a seemingly insignificant fact.

Namely: the lambda keyword is long. With six letters, it is among the longer keywords in Python 2.x, tied with return, import and global, and beaten only by continue and finally. Quite likely, this is what causes lambdas in Python to stand out and require additional mental resources to process (assuming we’re comfortable enough with the very idea of anonymous functions). The long lambda keyword seems slightly out of place because, in general, Python keywords are short.

Or are they?… Thinking about this, I’ve got an idea of comparing the average length of keywords from different programming languages. I didn’t really anticipate what kind of information would be exposed by applying such a metric but it seemed like a fun exercise. And it surely was; also, the results might provoke a thought or two.

Here they are:

Language Keyword Total chars Chars / keyword
Python 2.7 31 133 4.29
C++03 74 426 5.76
C++11 84 516 6.14
Java 1.7 50 289 5.78
C 32 166 5.19
C# 4.0 77 423 5.49
Go 25 129 5.16

The newest incarnation of C++ seems to be losing badly in this competition, followed by C#. On the other side of the spectrum, Go and Python seem to be deliberately designed to avoid keyword bloat as much as possible. Java is somewhere in between when it comes to sheer numbers of keywords but their average length is definitely on the long side. This could very well be one of the reasons for the perceived verbosity of the language.

For those interested, the exact data and code I used to obtain these statistics are in this gist.

Be Sociable, Share!
Be Sociable, Share!
Tags: , , , , ,
Author: Xion, posted under Computer Science & IT »


8 comments for post “Of Languages and Keywords”.
  1. TeMPOraL:
    May 28th, 2012 o 19:23

    Interesting.

    There’s a funny thing people in Emacs/Lispland often do – you can add a small piece of code to Emacs that collapses word “lambda” into symbol “λ”. It’s only a rendering thing (the word “lambda” is still there), but it makes code a bit more readable. So instead of:

    1. (lambda (x y) (* x y))

    you get:

    1. (λ (x y) (* x y))
  2. Xion:
    May 28th, 2012 o 22:10

    I’ve got a colleague that has this. It replaces not only lambda, but also ‘and’, ‘or’, ‘True’, ‘False’, ‘None’ and probably some keywords and operators with their mathematical equivalents. Afterwards even ordinary Python code looks extremely cryptic :)

  3. TeMPOraL:
    May 30th, 2012 o 14:16

    Hahaha, half-way to APL? :).

  4. Xion:
    May 31st, 2012 o 11:11

    If you did that to Haskell, I think you would be extremely close :)

  5. GenericG:
    June 2nd, 2012 o 21:29

    For the purpose of fairness: I think the count is off by 3 in C++11 favour ;-) Those pesky spaces!

    I am curious though, why did you not include brainfuck? :-)

  6. Xion:
    June 5th, 2012 o 20:22

    The lack of keywords whatsoever seems like a perfectly valid reason to omit it ;P

    And by the way, the issue of what constitutes a keyword is not actually all that trivial. I had to omit all the Lisps and e.g. Haskell for precisely that reason. Maybe the whole concept of a keyword is very specific to certain class of languages: imperative ones, with Basic- or C-like syntax?…

  7. Astrych:
    July 11th, 2012 o 10:41

    TeMPOraL and Xion:
    Did you hear about this interesting language? ;)

    http://labs.oracle.com/projects/plrg/Publications/fortress1.0beta.pdf

Comments are disabled.
 


© 2023 Karol Kuczmarski "Xion". Layout by Urszulka. Powered by WordPress with QuickLaTeX.com.