{"id":16401,"date":"2018-05-03T10:07:08","date_gmt":"2018-05-03T08:07:08","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=16401"},"modified":"2026-01-12T09:51:03","modified_gmt":"2026-01-12T09:51:03","slug":"a-fun-interactive-comparison-of-programming-language-verbosity","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/tutorials\/a-fun-interactive-comparison-of-programming-language-verbosity\/","title":{"rendered":"A fun, interactive comparison of programming language verbosity"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>&nbsp;<br \/>\nIf you want to start a flame war among developers, all you need to do is start a discussion about what programming language is \u201cbest\u201d. <\/p>\n<p>I wanted to do a twist on that, starting with something less subjective. I\u2019m simply curious about which languages are most or least verbose? <\/p>\n<p>A starting point could be to assess their \u201cconciseness\u201d in performing various tasks (and not make any judgment as to any language\u2019s efficiency or effectiveness).<\/p>\n<p>This leads me to <a href=\"http:\/\/www.rosettacode.org\/wiki\/Rosetta_Code\">Rosettacode.org<\/a>, 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). <\/p>\n<p>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 \u201cnative\u201d 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. <\/p>\n<p>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, <a href=\"https:\/\/www.highcharts.com\/demo\/bar-basic\">Highcharts<\/a>.<\/p>\n<h3>Python, Flask, Highcharts, and Azure (my playground) Environment<\/h3>\n<p>Azure is Microsoft\u2019s cloud (disclaimer, I work for Microsoft). It is a great infrastructure to <a href=\"https:\/\/azure.microsoft.com\/try\/cosmosdb\/\">host your data<\/a>, create <a href=\"https:\/\/azure.microsoft.com\/en-us\/services\/bot-service\/\">bots<\/a> and <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/app-service\/web-sites-create-web-jobs\">automate tasks<\/a>, deploy <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/app-service\/app-service-web-get-started-python\">Python apps<\/a> or <a href=\"https:\/\/azure.microsoft.com\/en-us\/services\/cognitive-services\/\">leverage AI<\/a>.<br \/>\nFor this demo, I installed a python 3.4 environment on Azure and a few libraries (<a href=\"https:\/\/www.crummy.com\/software\/BeautifulSoup\/bs4\/doc\/\">Beautiful Soup<\/a> and <a href=\"http:\/\/flask.pocoo.org\/\">Flask<\/a>)<\/p>\n<h3>Steps 1: Scraping<\/h3>\n<p>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 <code>languages_dict<\/code> below) in order to avoid scraping too much irrelevant data.<\/p>\n<pre># Selected language names and corresponding spelling\/encoding as in HTML page \r\nlanguages_dict={\"Java\":\"Java\",\"JavaScript\":\"JavaScript\",\"C\":\"C\",\"C.2B.2B\":\"C++\",\"C.23\":\"C#\",\"COBOL\":\"Cobol\",\"Haskell\":\"Haskell\",\"Python\":\"Python\",\"R\":\"R\",\"Julia\":\"Julia\",\"MATLAB_.2F_Octave\":\"Matlab\",\"Pascal\":\"Pascal\",\"Fortran\":\"Fortran\",\"BASIC\":\"BASIC\",\"Go\":\"Go\",\"Ruby\":\"Ruby\",\"SAS\":\"SAS\",\"Stata\":\"Stata\",\"Swift\":\"Swift\",\"Processing\":\"Processing\",\"UNIX_Shell\":\"UNIX Shell\",\"VBA\":\"VBA\",\"PowerShell\":\"PowerShell\"}\r\n\r\n#store language names in an array\r\nlanguage_name=[]\r\n\r\n#populate the array from the dict\r\nfor item in languages_dict:language_name.append(languages_dict[item])\r\n\r\n#get all tasks from Rosettacode.org\r\nurl_task=\"http:\/\/www.rosettacode.org\/wiki\/Category:Programming_Tasks\"\r\nr = requests.get(url_task)\r\nsoup = BeautifulSoup(r.text, 'html.parser')\r\ntable=soup.find(\"div\", {\"class\": \"mw-category\"})\r\n\r\n#Create an empty dictionary to be filled with the tasks as they appear in the HTML source and name (as header)\r\nurl_dict={}\r\n\r\n#get all links (a tags) \r\ntags=table('a')\r\n\r\n#iterate over tag list and fill the task\/url dictionary\r\nfor tag in tags:\r\n    url_dict[tag.get('title',None)]=tag.get('href',None)[6:]\r\n\r\n#store task names in an array\r\ntask_name=[]\r\nfor item in url_dict: task_name.append(item)\r\narray_language=[]\r\ncount=[]\r\ntask=\"\"\r\n\r\n#flask method to get the task the user wants to compare\r\nif request.method == \"POST\":\r\n    # get url from task the user requested\r\n    task = request.form['task']\r\n    url=\"https:\/\/www.rosettacode.org\/wiki\/\"+url_dict[task]\r\n    r = requests.get(url)\r\n    soup = BeautifulSoup(r.text, 'html.parser')\r\n    dict_count={}\r\n    for language in languages_dict:\r\n        try: \r\n            header=soup.find(\"span\", {\"id\": language})\r\n            snippet=BeautifulSoup(header.find_next(\"pre\").text, 'html.parser')\r\n            dict_count[languages_dict[language]] = len(snippet.text)\r\n        except: \r\n            continue\r\n\r\n    #sort dictionary\r\n    for lang in sorted(dict_count, key=dict_count.get, reverse=True):\r\n        array_language.append(lang)\r\n        count.append(dict_count[lang])\r\n<\/pre>\n<p>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 \u201cpre\u201d 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.<\/p>\n<p>Here are the resulting arrays for the snippets corresponding to a \u201c<a href=\"https:\/\/www.rosettacode.org\/wiki\/Loops\/For\">For Loop<\/a>\u201d:<\/p>\n<pre># array_language\r\n['C', 'Swift', 'Pascal', 'Go', 'BASIC', 'Haskell', 'UNIX Shell', 'Matlab', 'Processing', 'PowerShell', 'Fortran', 'Cobol', 'VBA', 'C++', 'Java', 'Julia', 'Ruby', 'JavaScript', 'Python', 'R', 'SAS', 'Stata', 'C#']\r\n\r\n#count (length of the snippets)\r\n[90, 73, 144, 167, 70, 115, 400, 103, 103, 116, 1211, 533, 193, 104, 119, 71, 55, 114, 92, 85, 293, 79, 261]<\/pre>\n<p>These arrays will then feed the bar chart.<\/p>\n<h3>Step 2: Highcharts<\/h3>\n<p>Python and Flask send the arrays of data to Highcharts using <code>{{ array_language | safe}}<\/code> and <code>{{count}}<\/code>. During the HTML page rendering, the server injects the arrays constructed with python. Notice that I use the <code>| safe<\/code> option to avoid any encoding surprise.<\/p>\n<pre>Highcharts.chart('container', {\r\n    chart: {\r\n        type: 'bar'\r\n    },\r\n    title: {\r\n        text: 'Programming language comparison: {{task | safe}} '\r\n    },\r\n    subtitle: {\r\n        text: 'Source: <a href=\"http:\/\/www.rosettacode.org\">rosettacode.org<\/a>'\r\n    },\r\n    xAxis: {\r\n        categories: {{ array_language   | safe}},\r\n        title: {\r\n            text: null\r\n        }\r\n    },\r\n    yAxis: {\r\n        min: 0,\r\n        title: {\r\n            text: 'Task size (char)',\r\n            align: 'high'\r\n        },\r\n        labels: {\r\n            overflow: 'justify'\r\n        }\r\n    },\r\n    tooltip: {\r\n        valueSuffix: 'chars'\r\n    },\r\n    plotOptions: {\r\n        bar: {\r\n            dataLabels: {\r\n                enabled: true\r\n            }\r\n        }\r\n    },\r\n\r\n    series: [{\r\n        name: '{{task}}',\r\n        data: {{count}}\r\n    }]\r\n});<\/pre>\n<h3>Results<\/h3>\n<ul>\n<li>Below is a screenshot of the  <a href=\"https:\/\/pythonappcela.azurewebsites.net\/demo\">application<\/a>.<\/li>\n<li>Here are the <a href=\"https:\/\/tutorial-thomasmsft.notebooks.azure.com\/nb\/notebooks\/How%20prolix%20programming%20languages%20are.ipynb\">Python script<\/a> and <a href=\"https:\/\/notebooks.azure.com\/ThomasMSFT\/libraries\/Tutorial\/html\/demo.html\">HTML page<\/a> hosted on Azure.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result.png\" alt=\"\" width=\"1200\" height=\"788\" class=\"alignnone size-full wp-image-16404\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result.png 1200w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result-560x368.png 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result-768x504.png 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result-760x499.png 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result-360x236.png 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02142858\/result-1160x762.png 1160w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><br \/>\nWe 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.<\/p>\n<p>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? \ud83d\ude42<\/p>\n<p>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\u2026).<\/p>\n<p>I had a lot of fun to set up this demo, feel free to share your experience or questions in the comment section below.<\/p>\n<p>I\u2019ll let Dilbert have the last word, though.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert.jpg\" alt=\"\" width=\"1200\" height=\"368\" class=\"alignnone size-full wp-image-16406\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert.jpg 1200w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert-560x172.jpg 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert-768x236.jpg 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert-760x233.jpg 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert-360x110.jpg 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/05\/02145209\/dilbert-1160x356.jpg 1160w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><br \/>\nSource: <a href=\"http:\/\/dilbert.com\/strip\/1992-09-08\">Dilbert<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Which programming languages are most and least verbose, given similar tasks? Let&#8217;s visualize it!<\/p>\n","protected":false},"author":37,"featured_media":16403,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[210],"tags":[1094,1031,885],"coauthors":[724],"class_list":["post-16401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-highcharts-core","tag-javascript","tag-python"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/16401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=16401"}],"version-history":[{"count":2,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/16401\/revisions"}],"predecessor-version":[{"id":29540,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/16401\/revisions\/29540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/16403"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=16401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=16401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=16401"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=16401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}