简体   繁体   中英

Why care about trailing commas in Node.js?

In most Node.js libraries people take special care removing trailing commas after the last key-property pair of objects:

var test = {
    key1: 123,
    key2: 456,
    key3: 789
};

This produces some troubles while editing the code, eg to swap last two key-value pairs one has also to add one comma and to remove one. Some people move commas to the next line, which solves the issue with the last element but also makes the code a bit harder to read (IMHO):

var test = {
      key1: 123
    , key2: 456
    , key3: 789
};

On the other hand as far as I know the trailing commas in JavaScript produce troubles only in some IE browsers. So I'm wondering are there any technical reasons not to write hashes with trailing commas in Node.js? (Like the following:)

var test = {
    key1: 123,
    key2: 456,
    key3: 789,        
};

No, there is no technical reason to do that.

However, I never put trailing comas just because I think it makes for cleaner code. Probably some also have the habit coming from web development where, like you mentioned, you need to be careful about those because of IE.

Edit: This answer made sense back in 2012, but today, with major browser support and tools like Babel for older browsers, I think trailing commas should be the default for everyone. The benefits are that it makes adding a new line easier, and the relevant Git diff is cleaner.

Update: I've changed my position on this. I think trailing commas are great because you don't have to change two lines to add a property, and the diff shows only the added line.

Old and wrong:

I found a great reason to not use trailing commas , but it's not specific to node.js :

From Johan Sundström :

The benefit of this format is diff friendliness: adding or removing a property or array member almost never touches any other lines but the one where a change actually happened, whereas trailing-comma syntax almost always touches two lines to get the commas right.

Conversely, changed lines will never have anything unrelated on them, which helps scanning commits visually immensely.

It probably won't improve your runtime or anything like this, but you have a vantage using the trailing comma related to the version control.

If you do not use it, git will detected that one line was modified and another one was added. But if you use it, git will detected that only one line was added:

With using trailing comma:

在此处输入图片说明

Without using trailing comma:

在此处输入图片说明

First of all, I think trailing commas are ugly so I only use them when necessary (python tuples with just a single element) - and leading commas are even uglier.

Besides that, there is a reason to never use them: You don't have to take care if the code you are writing is for node.js or the client which might very well be an IE that does not like them.

There is no technical reason for omitting them in node.js.

I am a big fan of alpha-sorted keys in an object. There are a number of advantages to this. The primary one being the visual comparison of two similar objects. All the modern IDEs that provide debugging support for JavaScript (that I've seen) alpha-sort the properties. So when you're comparing an object at a breakpoint with your code it just makes it easier.

Having a trailing comma on the last pair in an object makes it easy to sort the pairs in the object: Select all lines and hit the sort key in your editor. No further adding/removing of commas is needed if the last pair is no longer the last pair.

I agree with all the other comments that (at first) it makes your code "unclean" or uglier. I found that after moving to trailing commas I quickly became used to it and after a number of weeks I started to prefer to see it that way.

As humans we are most comfortable with what we are used to. Given that most of the code that we see does not have a trailing comma that is what we like the most. After a few weeks with trailing commas you might find that you prefer that. And you might find it more practical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM