Hacker News new | past | comments | ask | show | jobs | submit login

Interesting that their json variant uses trailing commas. Too bad that's not more common.



I was surprised to see JSON in the low level messaging of a device that is supposedly (c) 1993-2015.


Not as surprised as I was to actually work with JSON in AVR C.


Yeah, broke the immersion for me.


It's more that it's sad that the official JSON spec doesn't have that nor comments. And that quotes are superfluously mandatory on all names.


Why?


When your parser accepts trailing commas, it becomes much easier to edit your lists and objects.

In standard json, the last element in a list or object is special, in that it can't have the trailing comma. Thus you have to special case every deletion and insertion of elements, to check if it needs a comma or not.

Without this requirement, you can remove and add elements without worry, which makes it easier for hand editing, and easier to avoid bugs in code.


One of the better ways I've seen of dealing with this, generally used in SQL which is one of the less lenient languages I've seen, is to prefix new lines of the list with commas. That is, when constructing a list using multiple lines (which is where the trailing comma is most problematic), the first line is not prefixed with a comma, and all subsequent lines are.

E.g.

    var foo = [
      "aa"
      , "bb"
      , "cc"
    ];
The usefulness of this may not be readily apparent in this simplistic example, but when the term is complex or spans multiple lines, it becomes useful both as an indicator of a new term starting, and in that identifying the end of a large term often isn't nearly as easy as identifying that start.

E.g.

    var foo = {
      attr1: "foo"
      , attr2: "bar"
      , method1: function() {
      }
      , method2: function() {
      }
    };
I'll admit I don't often use this technique. Most commonly it is with large custom SQL queries, which themselves are fairly uncommon for me. That said, SQL often lacks the mitigating techniques that make this less useful in common programming languages (such as computing complex terms ahead of time and assigning the result, gradual building of data structures, etc.).


That's why S-expressions are cooler. There are no commas.


Same reason we like languages that accept files with terminal newlines.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: