Trailing comma, or no trailing comma

Oct 6, 2017

Javascript
Git
Code Review

Linting is an essential part of development. It helps you keep a consistent level of quality and rules to stick to.

However there’s one rule that until recently I turned off. Comma-dangle.

Comma-dangle states whether the last item in a object should have a trailing comma. I used to think this was pretty useless and liked how it was without the comma. But a work mate recently brought up a good point.

Git diffs…

Say you have an object without a trailing comma on the last item…

1const object = {
2 first: "hello",
3 last: "goodbye"
4};

If I then go and add a new item to the end of that object, my git diff will look like so:

1const object = {
2 first: "hello",
3 last: "goodbye"
4 last: "goodbye",
5 final: "the end"
6};

Notice how it says that last has changed? Because we’ve added a comma on the end… a bit annoying. If we used trailing commas:

1const object = {
2 first: "hello",
3 last: "goodbye",
4 final: "the end",
5};

Git Diff will now not complain about last as we haven’t needed to update that line, we just added final onto the end.

This seems like a small win, but when you have to review tens/hundreds of lines of code, every little thing you don’t have to double check is definitely a win!

Javascript
Git
Code Review

Contact.

LET'S WORK

TOGETHER

I am open to both contract and freelance developer projects and would love to hear what your ideas are.

Feel free to drop me an email and I'll let you know how I can help you!

mail@benstokoe.co.uk