Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I came across this formatting pattern in Haskell, but I still prefer trailing commas for one reason: I can trivially apply line-wise operations (e.g. sort or align) on the key-value pairs without breaking the syntax. When I sort your first snippet line-by-line, it becomes

  , "age": 229
  , "appearance": "Tree-like.  It's a tree."
  , "sex": "monoecious species"
  { "time": "3 minutes past four"
  }
and the syntax is broken. With trailing commas, the syntax always stays valid:

  {
    "age": 229,
    "appearance": "Tree-like.  It's a tree.",
    "sex": "monoecious species",
    "time": "3 minutes past four",
  }


With prefix commas it also always stays valid

    {
    , "age": 229
    , "appearance": "Tree-like.  It's a tree."
    , "sex": "monoecious species"
    , "time": "3 minutes past four"
    }


Is the first comma legal in Json or JavaScript? I'd have thought that'd be an error.


Valid but introduces undefined as the first item of the array


You are assuming that trailing commas are ignored as valid, which is true of JavaScript but not of JSON or of C (the other example).


Yes, I'm aware of this being an error in JSON (which is precisely the point of the discussion). I wasn't aware that it's a problem in C. Maybe gcc is more lenient here.


Trailing commas are allowed in C!


C89 v. C99 difference IIRC

edit: I was wrong: http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#initialize...


No: tptacek did not bother paying attention to the context; as far as I know "uint8_t * data, * buffer, ;" (spaces added after * to avoid italics) would never be valid.


How is that the context? In the sense of the intersection between Javascript and JSON, trailing commas have pretty much always worked in C, which is something all of us who write code that generates C rely on, like a lot.


I googled and trailing comma was allowed since C89, I was wrong. For code that generates code I always did something like this cause of that mistake:

  { a, b
  , c
  ...
  , z
  };


The contextual example from C was "uint8_t * data, * buffer;" (spaces added to avoid italics), and no: a trailing comma is absolutely not allowed.


Replied here, to the first time I saw you write this:

https://news.ycombinator.com/item?id=12368022




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: