> In order to combat trailing commas I normally place the comma on the following line
Ah okay, so you're that guy whose code I have to reformat in every new job.
Re:Fixing JSON
by philovivero on Sunday August 21, 2016 @01:39AM
(#12329567)
Ah, so you're the guy whose code I have to re-reformat back to the proper format.
I use industry standard IOCCC formatting rules. Doesn't everyone?
If you're having to do it on every job, maybe you're the unusual one
Weirder still - why do I follow him from job to job?
I like where this is going :-)
Re:Fixing JSON
by Normal_gaussian on Sunday August 21, 2016 @08:14AM
(#12330642)
I highly recommend having a couple of reformatting scripts around - its a great way to find logical errors quickly.
Of course don't commit reformats where it diverges from the codebase.
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:
Re:Fixing JSON
by stevewilhelm on Sunday August 21, 2016 @01:41AM
(#12329576)
I have seen this approach also used when constructing complex SELECT FROM and WHERE clauses in MySQL
Re:Fixing JSON
by truth_sentinell on Sunday August 21, 2016 @08:05AM
(#12330611)
That is horrible from a human understanding perspective
Re:Fixing JSON
by Normal_gaussian on Sunday August 21, 2016 @08:12AM
(#12330633)
It makes perfect sense to me.
Which to be honest is what matters. However I would imagine that a lot of the problem grokking it is due to not being used to it.
Re:Fixing JSON
by paulddraper on Sunday August 21, 2016 @02:24PM
(#12332050)
Why?
It looks easier for me; I'm far more likely to notice a missing comma.
But I'm sure you must have a valid point, if you would elucidate?
Both vivid and accurate.
In order to combat trailing commas I normally place the comma on the following line, e.g.
With such a format there is only ever a problem deleting the first line, which I find is much much harder to do without also noticing what you've done to the larger statement.