Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript Object trailing commas #40

Open
shanejann opened this issue Apr 24, 2019 · 3 comments
Open

Javascript Object trailing commas #40

shanejann opened this issue Apr 24, 2019 · 3 comments

Comments

@shanejann
Copy link

As most JS devs might have noticed at one point or another, Javascript allows trailing commas in many instances.

var arr = [
    7,
    8,
    9,    // <-- This is a trailing comma
]

I'd like to get opinions on the topic. I've gotten sent back in Code Review a number of times for this behavior. The arguments against it might be that json doesn't support trailing commas, in which case I would understand why this might be a bad behavior to condone.

@DanielSchaffer
Copy link

I like them because:

  • It makes it SUPER easy to move lines around in the IDE (in WebStorm it's ALT+UP/ALT+DOWN) - without the trailing comma, you have to manually go fix it
  • It makes for cleaner diffs when adding a new line entry - without a trailing comma, you get an extra line just for adding a comma

With a properly configured auto-fixing linter, you don't even need to think about it either.

@shanejann
Copy link
Author

shanejann commented Apr 24, 2019

My thoughts EXACTLY. I remember being like, "you want em to remove the comma, but look how beautiful that diff is??". It's super nice for anything like arrays, dependency injections, module declarations, etc. (Idk why, but I'm thinking specifically of TypeScript scenarios)

@DanielSchaffer
Copy link

DanielSchaffer commented Apr 24, 2019

Also: I don't think the JSON standard matters in the context of this conversation. Most of the time, we aren't writing JSON, we're writing JavaScript or TypeScript. If you forget and add a hanging comma in JSON, you're likely to be reminded pretty quickly that they're not allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants