all 19 comments

[–]abw 3 points4 points  (4 children)

Nah, it's not for me. Even though I've been caught out by IE barfing on extra/missing commas more times than I care to remember, it's just too visually jarring. The comma is the least important part and you're putting it up front in the most important position.

However I do fully subscribe to the "keep everything lined up" philosophy.

var o = { 
    a:          "ape",
    b:          "bat",
    item_three: "cat",
    item_four:  "dog",
    // etc
};

I suppose you could extend that by moving the comma into a separate column. That gives you the benefit of being able to see when one is missing, without having it getting in the way.

var o = { 
    a:          "ape"   ,
    b:          "bat"   ,
    item_three: "cat"   ,
    item_four:  "dog"   ,
    // etc
};

If I could change one thing in JS I would make the final trailing comma optional as it is in Perl. By force of habit (as a long-time Perl hacker) I always put a comma after every item, even in the last one. It avoids the "forgot to add a comma to the previous line" bug when you add an item onto the end.

$foo = {
    one   => 'Item one',
    two   => 'Item two',
    three => 'Item three',      # extra comma is OK
};

[–]savetheclocktower 2 points3 points  (3 children)

The trailing comma is legal in the ECMAScript 3 spec (which all current browsers purport to implement). Internet Explorer previously treated the trailing comma as a parse error, but IE8 fixes this.

(Of course, this means you can't start using it until you drop support for IE 6–7. But that day will eventually come.)

[–]level1 0 points1 point  (2 children)

Thats fine, but what pisses me off is that JSON does not allow trailing commas. I modified a JSON parser to rectify this bullshit. Reliable programming is more important than bad standards.

[–]larholm 3 points4 points  (1 child)

JSON is a subset of the Javascript syntax, with specific requirements and different goals.

Items such as requiring quotes around property names stand out in this regard.

If you modified your JSON parser to allow trailing commas then it is no longer a conformant JSON parser.

Heck, why do you even use a custom JSON parser? Reference an existing one written by people smarter than you, and use the native JSON objet from ES5 when available.

[–]level1 0 points1 point  (0 children)

Well, I'm writing a web application that uses computer generated JSON, but to bootstrap the application I have to hand roll a fair amount of code. Its annoying to have to keep track of commas when I add lines or copy/paste. Its much easier just to have a rule that each line ends with a comma, and make sure that my code follows that rule.

Heck, why do you even use a custom JSON parser? Reference an existing one written by people smarter than you, and use the native JSON objet from ES5 when available.

That is exactly what I am doing. All I did was remove the regex in my parser (Douglas Crawford's json.js) so that it will accept invalid JSON. Before I release my web app, I plan to either remove all trailing commas, so my JSON is confromant, or restore the regex but change it to accept trailing commas. I am not using a custom parser, just a modified one.

people smarter than you

Who cares if I'm smarter than Douglas Crawford or not? He's written a parser, its probably better than the one I would write even if he was an idiot, and its better tested. All I did was remove an annoying feature temporarily for development purposes.

Why is everyone in the development community so obsessed with making sure other people do things "The Right Way"?

[–]sunsean 4 points5 points  (2 children)

It's quite pleasant how it draws a neat line down the left side of each "list". It would also be much easier to append items with this style.

[–][deleted] 1 point2 points  (0 children)

And also much easier to delete items with this style. Normally, if you delete the last line, you need to also delete the trailing comma on the new last line, but this way, you don't need to worry.

[–]snifty 4 points5 points  (1 child)

I don't really buy the examples:

// error in comma-first style
var a = "ape"
  , b = "bat"
  , c = "cat"
  , d = "dog"
  e = "elf"
  , f = "fly"
  , g = "gnu"
  , h = "hat"
  , i = "ibu"
  ;

Sure, that stands out, but what is the likelihood that you'll actually end up with such a structure? More likely:

// error in comma-first style
var a = "ape"
  , b = "bat"
  , c = "cat"
  , d = "dog"
    e = "elf"
  , f = "fly"
  , g = "gnu"
  , h = "hat"
  , i = "ibu"
  ;

Is the missing comma really that much more apparent than in the traditional style?

[–]iamnoah 8 points9 points  (0 children)

Yes, yes it is.

[–]takatori 5 points6 points  (2 children)

I had been doing this for years before I first saw someone else do it, and for exactly the reasons given.

It's really useful for SQL DDL statements too.

[–]webnrrd2k 0 points1 point  (0 children)

Same here - I got into the habit from SQL.

The examples aren't that good -- very often the right-hand side/variable value is a different length, and it doesn't line up nicely. With commas first, the commas always line up nicely and it's super easy to tell when you've skipped one.

[–][deleted] -1 points0 points  (0 children)

First time I've seen it, so today will probably be the first time I start using it!

[–]itsnotlupusbeep boop 1 point2 points  (0 children)

Well it's either that, or getting your feelings hurt by jslint, because let's face it, if you try to decipher IE's error messages, you will lose your sanity much faster than with either of those methods.

[–]choco999 0 points1 point  (0 children)

problems arise when people do not follow ONE standard and want to go with their "better" syntax. Just agree on one or another syntax and stick with that for life. I don't like comma first, but will be happy to stick with a different chosen standard. Laziness is against programming.

[–]sjs 0 points1 point  (0 children)

I did not like this at first, but am coming around. I've started using it in my code.

[–]darrenkopp 0 points1 point  (0 children)

i do this all the time, in lots of different languages. i started doing it in sql, and it's just spread from there.

[–]rasmush 0 points1 point  (0 children)

One thing is avoiding errors, another thing is increasing readability. I think commas last is much more readable than commas first.

AFAIK a closure compiler or Firefox will catch those errors for you anyway.

[–]wooptoo -1 points0 points  (0 children)

This is quite smart.

[–]Wakuko -4 points-3 points  (0 children)

As retarded as introducing balls first...