Kosmetika
Kosmetika

Reputation: 21304

React Native - What's the point of leaving trailing comma everywhere?

While working with React Native I noticed some strange convention by its' contributors and in examples - to leave trailing comma everywhere, as an example:

What's the point?

Upvotes: 3

Views: 2044

Answers (3)

Sir Alucard
Sir Alucard

Reputation: 41

You could end up with these kind of situations, simple but very hard to debug and find:

[
    -7,
    -2,
    -1,
    -3   // <-- this simple skip would cause a -7 here (-3 - 4) with no errors
    -4,
    -2,
    -9
];

So yes, I would prefer to adopt the trailing comma in every line.

Upvotes: 1

zerkms
zerkms

Reputation: 254926

Then when you perform diff only one line is changed.

If you don't do that - 2 lines will be marked as changed.

Technically you can put comma in the beginning of the line and change your style but it's (WARNING: hardly opinionated) ugly.

If I remember correctly, it's valid to specify it when you destructure JS objects, but it's invalid to do so when you use it as a JS object literal (please correct me if I'm wrong).

So this:

var { foo, } = obj;

is a valid ES6 code.

And this:

var o = { foo: 42, };

is not a valid JS code.

Upvotes: 9

Siguza
Siguza

Reputation: 23850

First, if you have a snippet like this:

{
    a: 'a',
    b: 'b'
}

and add a line to it, you have to remember to add a comma after 'b', otherwise you'll get a syntax error with this:

{
    a: 'a',
    b: 'b'
    c: 'c'
}

If you put a comma everywhere, you don't even have to think about that.
The same goes for swapping and removing lines.

Second, if you're using a version management system such a git or svn and add a line, you have to append a comma to the previous line, thus two lines have changed instead of one.

And third, it looks more consistent, especially when all keys and values have the same length:

{
    a: 'a',
    b: 'b',
    c: 'c',
    d: 'd',
    e: 'e',
    f: 'f'  // <-- Aren't you bothered by this?
}

Upvotes: 4

Related Questions