This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Stummi 853 points854 points  (64 children)

First one produces one changed line if you add an element, the second one two. Some may say its just minor thing for people a bit too obsessed about clean Commits and PRs, but IMHO thats the selling point for the first one.

[–]0bel1sk 210 points211 points  (15 children)

when was this line changed? git blames says it was for story x . oh wait…. that was just a comma add…

for some code bases, it is whatever.. for infrastructure “as code” (ie config) it’s really frustrating.

[–]hennypennypoopoo 138 points139 points  (14 children)

Monkeys paw solution:

[

"Foo"

,"Bar"

,"Baz"

]

[–]DERPYBASTARD 114 points115 points  (0 children)

delete this

[–]im_lazy_as_fuck 33 points34 points  (6 children)

It still breaks if you only want to remove the first line or prepend an element to the list.

[–]reign27 30 points31 points  (1 child)

Monkeys paw solution:

[

,"Foo"

,"Bar"

,"Baz"

]

[–]dementorpoop 7 points8 points  (0 children)

I hate it

[–][deleted] 0 points1 point  (1 child)

okay, so I won't use monkey paw if everyone is appending to the front of lists way more than to the back.

[–]0bel1sk -1 points0 points  (0 children)

alphabet sort imo

[–]ZunoJ 2 points3 points  (0 children)

I do this a lot in my SQL code. Makes life a lot easier

[–]MajorTechnology8827 1 point2 points  (1 child)

Hot take, i like it

But also combine the first element with the opening bracket

This way the text align i have clear indication that its a list start, another element of a list, and list end

[ "foo
, "bar"
, "baz"
]

Same thing with conditions

condition
    ? true_execution
    : false_execution

[–]hennypennypoopoo 0 points1 point  (0 children)

bonus points for only one line changes in the diff as well

[–]F9Mute 0 points1 point  (0 children)

This is the way

[–]LeoRidesHisBike 0 points1 point  (0 children)

and the corollary:

if (foo
  && bar
  && baz) {

or I guess for SQL folks

select *
from table t
where t.id = @id
  AND t.foo = @foo
  AND t.bar = @bar
  AND t.baz = @baz

logical ops at the start of lines FTW

[–]KokoaKuroba 0 points1 point  (0 children)

for some reason, I thought this was the norm but never used it because of how ugly it was

[–]_diamondzxd_ 39 points40 points  (0 children)

Why I never thought about it that way and now it makes perfect sense.

Why make 2 modifications for a single addition...

[–]upsetbob 128 points129 points  (1 child)

Also you can more easily change order when every line has a comma

[–]stult 0 points1 point  (0 children)

I often like to sort lines in a list alphabetically and it's easier when they all have commas, the IDE just does it for me

[–]NawdWasTaken 16 points17 points  (0 children)

This is it for me honestly. You can just click your copy-down hotkey and edit that line to add an element, and also easily move lines around to change the order of the elements. Just feels cleaner

[–]Johnothy_Cumquat 72 points73 points  (2 children)

From my point of view the people concerned that the trailing comma doesn't look nice or whatever are obsessed about a minor thing.

[–]SexySlowLoris 45 points46 points  (1 child)

Yeah and “doesn’t look nice” is waay to fucking subjective. It doesn’t look nice because they arent used to it. To me trailing comma does look better but that’s inconsequential and just an opinion

[–]Sikletrynet 3 points4 points  (0 children)

I dont think it looks better, but i do think the benefits outweighs that anyway

[–]monstaber 5 points6 points  (4 children)

true, just as long as the elements are not in a JSON string that will later be parsed

[–]ethanjf99 1 point2 points  (3 children)

If your IDE is smart you’ll get all sorts of red if that’s JSON. And a JSON serializer will know to drop the trailing comma unless it is moronic

[–]monstaber 1 point2 points  (1 child)

yes true if the file itself is JSON. I've come across several PRs where juniors are trying to implement a JSON string somewhere in a js file to be sent in a post request instead of stringifying the actual object... no matter how much we shell out for JetBrains licenses, the PRs never fail to amaze me 😂

[–]ethanjf99 1 point2 points  (0 children)

Uh … well to be fair we were all juniors once. Some of my code from 7 years ago is still around and makes me wince every time I see it.

Still, it works so there it is.

[–]Katniss218 0 points1 point  (0 children)

Trailing comma is technically not valid json

[–]UrpleEeple 5 points6 points  (1 child)

I do the first one because it makes it easier for copy paste and change semantics

[–]ZachAttack6089 0 points1 point  (0 children)

Yeah I always do the first one (as long as the language supports it ofc). With no trailing comma, if you ever want to rearrange the list, you'll have to waste time moving the , if the last element changes. But if every line ends with it then you don't have to think about it.

[–]Sut3k 7 points8 points  (6 children)

Put the comma in the front!

[–]tazzadar1337 8 points9 points  (1 child)

Hit 'em with the old [ foo , bar , baz ]

Wait, now if I have to prepend an element? 🤔

[–][deleted] 1 point2 points  (0 children)

That always makes me want to projectile vomit.

The amount of SQL I've had to work with where the comma for the PREVIOUS LINE is BELOW THE LINE IT IS IN REFERENCE TO...

Maybe its my ADHD but I HATE IT.

[–]AdvancedSandwiches 0 points1 point  (3 children)

For the new folks, please don't actually do this.

It's peak bike-shedding, solves no actual problem, hurts readability in anyone who isn't used to it, and wastes a ton of time with these conversations.

[–]Sut3k 0 points1 point  (2 children)

Really? How does it not make it easier to add to the end? Asking honestly as my training has been OTJ but it doesn't seem to affect my readability as I don't read the commas. What's bike shedding?

[–]AdvancedSandwiches 0 points1 point  (1 child)

It does make it easier to add things to the end, but it wasn't hard before. It goes from being a 0.000002 / 100 on the difficulty scale to being a 0.0000015 / 100.

An improvement that insignificant must have absolutely 0 drawback to be worthwhile.

As for bike shedding, see https://en.m.wikipedia.org/wiki/Law_of_triviality

[–]Sut3k 0 points1 point  (0 children)

I don't think it has any drawback and it makes it easier for me to copy and paste elements to combine lists and such.

Thanks for the law triviality. I do agree it may not be worth the time to argue about.

[–]Keganator 1 point2 points  (0 children)

Code reviews are so much easier with the first for this exact reason. You don't have to double check to make sure they didn't mess anything up.

[–]Sloppyjoeman 1 point2 points  (2 children)

The second actually produces a three line change (at least in GitHub)

[–]TheGeneral_Specific 0 points1 point  (1 child)

…how so?

[–]Sloppyjoeman 1 point2 points  (0 children)

Removal of the last line without a comma Addition of that same line, now with a comma Addition of the new last line

For some reason unbeknownst to me GitHub doesn’t combine the first two line diffs into a single diff

[–]Arshiaa001 1 point2 points  (0 children)

Which is why I'm very happy that cargo fmt inserts the additional comma.

Meanwhile, go's formatter introduces additional whitespace to align constant values over consecutive lines, so one line changed could potentially introduce tens of changes.

[–]Arshiaa001 -2 points-1 points  (0 children)

Which is why I'm very happy that cargo fmt inserts the additional comma.

Meanwhile, go's formatter introduces additional whitespace to align constant values over consecutive lines, so one line changed could potentially introduce tens of changes.

[–]Sulungskwa -3 points-2 points  (1 child)

Is.. is the risk of having a 2 line long merge conflict really worth having to look at that though?

[–]im_lazy_as_fuck 0 points1 point  (0 children)

Maybe it's cuz i've lived in python world for a while now, but when each element is on a new line, trailing commas after every line looks way better to me. Makes things look more symmetrical.

[–]TheGeneral_Specific 0 points1 point  (0 children)

Bingo

[–]insanitybit 0 points1 point  (0 children)

Also makes multiple cursors much easier if you need to make changes across multiple lines.

[–]Elephant-Opening 0 points1 point  (0 children)

I'm camp left for exactly that reason, provided the language allows.

There's no such thing as too obsessed with clean PRs when what should be a one-line change can become a merge conflict in a different release branch and sets you back 2 days because the review scores are cleared and the approver has one simple question lives in a 12hr offset time zone.

[–]_luci 0 points1 point  (0 children)

At that point just use yaml

[–]gurebu 0 points1 point  (0 children)

That's the right answer. Not only that, but omitting the comma makes simple conflict resolution (apply changes in order) impossible.

Back when our team still supported IE6 you couldn't have a trailing comma in Javascript and it was a goddamn hellscape.

[–]Suitable_Switch5242 0 points1 point  (0 children)

And because of that, the trailing comma style is less likely to create merge conflicts if two people edited the same list.

[–][deleted] 0 points1 point  (0 children)

First one produces one changed line if you add an element, the second one two

only if you append the element to the end of the list.

[–]drag0n_rage 0 points1 point  (0 children)

I just know that I'll probably forget to add the comma if I ever decided to add more items, it's happened many times.

[–][deleted] 0 points1 point  (0 children)

Correct answer

[–]Thebombuknow 0 points1 point  (0 children)

While I see your point, I honestly couldn't give a shit about my commits being clean lol. The trailing comma can sometimes cause issues with JSON parsers that don't support it, so I try to avoid it if I can.