5

Why do so many developers write commas this way?

var npm = module.exports = new EventEmitter
  , config = require("./lib/config")
  , set = require("./lib/utils/set");

Not this way?

var npm = module.exports = new EventEmitter,
    config = require("./lib/config"),
    set = require("./lib/utils/set");
2
  • 1
    Many? Do you have any concrete stats/evidence? Commented Jan 14, 2011 at 20:18
  • 4
    @BalusC: Does s/he need "concrete stats" to ask such a question? Anyone who's spent a couple months in JS world has seen this pattern somewhere, in a library or in a coworker's code. Commented Jan 14, 2011 at 20:24

5 Answers 5

7

They write them with the "," at the beginning of the line to make it easier to maintain the code (add lines or remove/comment out lines).

Given this:

var npm = module.exports = new EventEmitter
  , config = require("./lib/config")
  , set = require("./lib/utils/set");

It's much cleaner and easier to do this:

var npm = module.exports = new EventEmitter
//  , config = require("./lib/config")
  , set = require("./lib/utils/set");

as well as add new lines like this:

var npm = module.exports = new EventEmitter
  , config = require("./lib/config")
  , anothervalue = require("./lib/aval")
  , anothervalue2 = require("./lib/aval2")
  , set = require("./lib/utils/set");
Sign up to request clarification or add additional context in comments.

6 Comments

I don't see any reason the comma at the start would make either of those easier compared to the comma at the end?
This is just style, nothing more, if it takes you to long to comment out a line... switch your editor but don't write such (IMO, remember it's about style) code.
This really becomes more of an advantage when you're working on much longer objects / functions... With 3 or 4 lines, it really doesn't make of difference, but with an object with 30 or 40 attributes that have to be maintained periodically, it becomes very advantageous.
I don't personally use this style but the answer does a good job of explaining its use - one benefit that follows from it is that you are only editing the lines you actually wrote; a version control system's annotate function would not claim that you wrote a line that you simply appended a comma to.
I have come to the same conclusion over the years and came across node.js narg module which had this syntax and I was pleasantly surprised. I have thought I was an alien when doing this but glad to see others have come to the same conclusion :) IMO Software is its own language and there is no sense in constraining any code to the rules of the English language.
|
1

I never saw that pattern before in JS, so I'm not sure if there are that many developers that use it, but I would guess they do that so variable names are aligned, to emphasize that var actually defines three variables in your sample code.

If that's the case however, it would be clearer (and less weird) to just use var three times:

var npm = module.exports = new EventEmitter;
var config = require("./lib/config");
var set = require("./lib/utils/set");

Comments

1

This is strictly a programmer-specific syntaxtual preference. I know a lot of DBA's that sem to prefer that method, while I prefer the trailing comma myself. there's nor real difference except for personal preference/education.

Comments

0

var npm = module.exports = new EventEmitter
config = require("./lib/config") // ,
//anothervalue = require("./lib/aval"),
//anothervalue2 = require("./lib/aval2"),
set = require("./lib/utils/set");
See the extra "//"... you would have to add the "//" to an existing line at the end as well as add it to the next line...

Comments

0

EDIT 2: The above question prompted me to scratch an itch that has bothered me for some time. After quite a bit of rumination, I have come up with a good justification for leading commas: easily spot missing commas. This is a potentially nasty source of bugs, since, rather than complain, the compiler will simply add a semicolon, thus changing the meaning quite dramatically:

var a = foo(),
    b = bar()  // Oops...
    c = baz(); // Ouch!

The third line no longer initialises a new variable in the current scope. It assigns to an existing variable in an outer scope or initializes a new global variable if there isn't one.

This doesn't explain the use of leading commas in JSON. Perhaps it just carried over by fiat.

That said, I still favour separate declarations, which make it all simple and safe.

I'll leave my rant in place for posterity.


I agree that it appears all over the place. Almost every second library I peruse uses this convention. Frankly, I don't get it.

Trailing commas make it awkward to comment out the last line of a group...

var //a = foo(), easy
    b = bar(),
    c = baz();

var a = foo(),
    b = bar()/*, awkward
    c = baz()*/;

...whereas leading commas make it awkward for both the first and last line...

var /*a = foo() awkward
  , */b = bar()
  , c = baz();

var a = foo()
  , b = bar()/* awkward
  , c = baz()*/;

This is progress?

(One could of course win a cheap point by observing that semicolons are optional, but this only achieves parity at the expense of shunning best-practice.)

Worse, leading commas are also used for JSON. Here's a package.json generated by Express.

{
    "name": "application-name"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.5.10"
    , "jade"   : ">= 0.0.1"
  }
}

This is a PITA because most editors don't like indenting sibling lines differently to each other. Note that the first line of a group is indented more than the following lines.

The use of leading commas strikes me as a pointless affectation that causes problems without solving any. The only argument I can think of that has any merit is that leading commas line up with each other; not very compelling.

Fortunately, CoffeeScript is taking over the world, which renders the question moot (it even trounces JSON for config).

EDIT: I forgot to mention that I don't actually favour trailing commas for the above scenario. I think declaring individual vars makes the most sense. It's clean, regular and makes it very easy to comment out individual items.

var a = foo();
var b = bar();
var c = baz();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.