5

I'm learning node.js and am trying out Express. My first app had this code:

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

Reading through the mongoose tutorial gives me this:

var mongoose = require('mongoose')
  , db = mongoose.createConnection('localhost', 'test');

On strict mode, JSHint gives me

app.js: line 6, col 32, Bad line breaking before ','.

Which shows that I'm not the only one out there who's bugged by this syntax.

Is there any reason to declare vars this way instead of adding the comma at the end of the line?

5 Answers 5

11

I've heard it's called NPM style "comma-first" rule. Example from the doc:

var magicWords = [ "abracadabra"
                 , "gesundheit"
                 , "ventrilo"
                 ]
  , spells = { "fireball" : function () { setOnFire() }
             , "water" : function () { putOut() }
             }
  , a = 1
  , b = "abc"
  , etc
  , somethingElse

The doc doesn't state the motivation behind this coding style, but I guess it's to prevent dumb syntax errors from placing an excessive comma at the end of a comma-separated list, and to alleviate the mental tax it places on you from trying to get that part of the syntax right.

Also note that NPM style recommends "not to use semicolons". If you adopt this style, you're also doing away with the semicolon that fills the final position of a var statement, making it tempting to place a comma there instead.

As for the JSHint error, there's a switch to allow comma-first style line breaks.

1
  • Interesting to read that I have been following a specific style... Yeah, I can now say that it is not just my preference :-) Commented Nov 11, 2012 at 9:46
4

I think that the main reason is so that if you add an extra item, you create an "add new line" change in your version control system, rather than a "Replace line with 2 new lines" change, with the only change to the first line being addition of a comma.

Tools like git-blame will then report the commit where you added parts relevant to the new item, rather than the item after it.

3

To expand a bit on ento's comment about syntax issue, JavaScript does not allow extra commas within object literals. For example, in PHP one might assign an array as follows:

$foo = array(
      "thing 1",
      "thing 2",
      "thing 3", // <-- extra comma
);

The extra comma means that you do not have to give special treatment to the last item in a list. You don't have to remember to add the comma if later on you need to add something to the list, and version control only has to track one line changing.

In some versions of JavaScript (JScript), however, the trailing comma causes errors. So you need to leave it off. Later, when adding things you need to remember to add it, and version control has to track two changes.

Adding the comma at the start of every line instead of the end, means that you don't have to treat the last item specially. If you use the leading comma, and leave out semi-colons then items you never have to worry when adding or removing items from a list.

1
  • Old Internet Explorer (IE ≤ 7) versions choke on the comma, in different ways: Within object literals, ({a:1,}) an error occurs. Within array literals, [,], the length is incorrectly set (the length must be one, but it's actually 2; IE ≤ 8).
    – Rob W
    Commented Nov 11, 2012 at 10:28
0

For a human reader, the leading comma indicates that the line is a continuation of the one above - although personally I think the indentation by itself does that job just fine.

After checking the rules for automatic semicolon insertion it does seem surprising that this would cause an error in a static analyser. Maybe the authors just have a taste of their own.

0

At least there is no technical reason. From a technical point of view, it even seems a bad idea, considering the tendency of JS engines to add semicolons wherever they think they need one.

Aesthetically, it's just like the old OneTrueBraceStyle problem - ask 5 people, get (at least) 5 different answers.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.