A fun, interactive comparison of programming language verbosity

If you want to start a flame war among developers, all you need to do is start a discussion about what programming language is “best”.

I wanted to do a twist on that, starting with something less subjective. I’m simply curious about which languages are most or least verbose?

A starting point could be to assess their “conciseness” in performing various tasks (and not make any judgment as to any language’s efficiency or effectiveness).

This leads me to Rosettacode.org, which is an awesome source of information for any programming enthusiast. It offers various tasks (more than 870) and code snippets to solve them, in many programming languages (more than 680).

I always wanted to explore Rosettacode.org and compare the different programming languages. It can help discover new ways of addressing the problems you face in your “native” language, and identify alternative ways of thinking. To that end, I wrote a little app to facilitate this process, which I will share that with you here.

The app, is very basic. It simply compares the length of code snippets from Rosettacode.org for different tasks and languages, and displays the result on a bar chart using my favorite Javascript library, Highcharts.

Steps 1: Scraping

The first step is to scrape RosettaCode to get all available tasks. I wrote a very short python script to do the scraping. I pre-selected a subset of programming languages (see languages_dict below) in order to avoid scraping too much irrelevant data.

When the user selects the task to compare, the script above looks for the pre-selected languages and evaluates the length of the corresponding snippets (within the “pre” tag after the corresponding header within the HTML). The script stores the results in an array which will be sent to the frontend via Flask to my Highcharts.

Here are the resulting arrays for the snippets corresponding to a “For Loop”:

Step 2: Highcharts

Python and Flask send the arrays of data to Highcharts using {{ array_language | safe}} and {{count}}. During the HTML page rendering, the server injects the arrays constructed with python. Notice that I use the | safe option to avoid any encoding surprise.

Results

We now have an interactive way to compare the length of the code we need to perform many tasks! A nice feature to be added to the chart would be to display the code snippet as a tooltip over each language. This would enhance our visual exploration of Rosettacode.

Many of you might be thinking that this is a rather silly exercise, and may even claim that it is faster for you to write more code in your favorite language than less code in a different language. You are right. This little exercise tells you nothing other than how verbose different languages are when performing similar tasks. Fun for the language nerds out there, right? 🙂

If we really wanted to review code-level execution-time efficiency on a task-by-task basis, it would be really fun to take this one step further and develop some method for measuring execution time and resources for each language. We could then extend this demo with a fancy dashboard for code efficiency (with lots of caveats again, of course…).

I had a lot of fun to set up this demo, feel free to share your experience or questions in the comment section below.