Live data from Hacker News

How about trailing commas in SQL?

peter.eisentraut.org

1–10 of 272 posts

Re: How about trailing commas in SQL?

#2
Ugh. Why? To make copy/paste programming easier? To make query generation easier? When you're writing code to generate queries, it's worth doing it right. Just about every programming language has an easy way to take an array of strings and add a separator between each element. Like PHP's implode: implode(', ', ['foo', 'bar', 'baz']) == 'foo, bar, baz'.

Every time I see a trailing comma, I think "is this a bug? did they forget an element?".

Edit: as a reply to the comments below: that's a lot of hassle to save you hitting backspace once. There's premature optimization, and then there's single keystroke optimization.

Re: How about trailing commas in SQL?

#4
post #2

Ugh. Why? To make copy/paste programming easier? To make query generation easier? When you're writing code to generate queries, it's worth doing it right. Just about every programming language has an easy way to take an array of strings and add a separator between each element. Like PHP's implode: implode(', ', ['foo', 'bar', 'baz']) == 'foo, bar, baz'. Every time I see a trailing comma, I think "is this a bug? did t…

It's so you can do this in code:

  my_things = [
    "thing_1",
    "thing_2",
    "thing_3",
  ]
And not have to juggle commas when you add a new end thing.

Re: How about trailing commas in SQL?

#5
post #2

Ugh. Why? To make copy/paste programming easier? To make query generation easier? When you're writing code to generate queries, it's worth doing it right. Just about every programming language has an easy way to take an array of strings and add a separator between each element. Like PHP's implode: implode(', ', ['foo', 'bar', 'baz']) == 'foo, bar, baz'. Every time I see a trailing comma, I think "is this a bug? did t…

To make writing and maintaining sql easier. To make sql diffs better. To make sql more consistent with other languages.

Every time this trips me up, it’s hand written SQL, because literally every other language I routinely use supports trailing commas.

The additional complexity during codegen is barely existent. If you’re using an orm or code generator this will be an issue once at most (if and when you write your own).

Re: How about trailing commas in SQL?

#6
I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end.

So, my solution for this was always

  SELECT a
       , b
       , c 
  FROM ...
instead of:

  SELECT a,
         b,
         c,  -- people want to allow trailing comma here
  FROM ...
Leading commas also are also very regular and visible. A missing trailing comma is a lot harder to see.

Before people start to mess with the standard, I'd suggest to maybe just change your coding style - and make the start of the list special - instead of the end.

I'd suggest going for a lot less trailing commas - instead of allowing more.

Re: How about trailing commas in SQL?

#7
I feel like this ship has sailed. SQL has been around for more than 50 years and everyone who needs to generate it has already put that extra `if` statement in to suppress trailing commas.

What annoys me far more often is the lack of support for trailing commas in JSON.

Re: How about trailing commas in SQL?

#9
post #2

Ugh. Why? To make copy/paste programming easier? To make query generation easier? When you're writing code to generate queries, it's worth doing it right. Just about every programming language has an easy way to take an array of strings and add a separator between each element. Like PHP's implode: implode(', ', ['foo', 'bar', 'baz']) == 'foo, bar, baz'. Every time I see a trailing comma, I think "is this a bug? did t…

During prototyping, I'm frequently adding and removing entries from lists, or change their order. It's really annoying when you have to update the commas afterwards. Much nicer dev experience when ever entry ends with a comma, and you can juggle them around at will.

Re: How about trailing commas in SQL?

#10
post #7

I feel like this ship has sailed. SQL has been around for more than 50 years and everyone who needs to generate it has already put that extra `if` statement in to suppress trailing commas. What annoys me far more often is the lack of support for trailing commas in JSON.

Funny I’m the exact opposite: I essentially never write JSON by hand, or add json content to repositories, so could not care less about the lack of trailing commas, I do semi routinely write or review SQL.
Post reply on HN