Why do node packages put a comma on a newline?

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?

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

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.

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

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật