Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

5 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
Interesting to read that I have been following a specific style... Yeah, I can now say that it is not just my preference :-) –  Marjan Venema Nov 11 '12 at 9:46
add comment

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.

share|improve this answer
    
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 Nov 11 '12 at 10:28
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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