Live data from Hacker News

How about trailing commas in SQL?

peter.eisentraut.org

31–40 of 272 posts

Re: How about trailing commas in SQL?

#31
post #23

Earlier quoted context omitted.

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.

That’s why I use leading commas.

The first line cannot have a leading comma resulting in inconsistency then too.

Re: How about trailing commas in SQL?

#33
post #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 com…

You've moved the problem from the last to the first element though. Surely people would prefer to be able to do this SELECT a, b, c, FROM ... ?

as mentioned elsewhere, i personally introduce a pad element to get fire and forget consistency

  SELECT 1 as pad
    , a
    , b

Re: How about trailing commas in SQL?

#34
post #16

Earlier quoted context omitted.

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.

That’s why I use leading commas. Unless I change the first column, no problem.

When you start working with sql you despise leading commas.

When you stop working with any other code than SQL you look back on your younger self and think ‘I was soooo young’ when typing those lovely leading commas.

Re: How about trailing commas in SQL?

#36
post #22

I suspect that it's perfectly valid to name a column "from" or any other SQL keyword so allowing a trailing comma would make the grammar ambiguous. Personally I agree with the sentiment, I find it annoying to have to juggle the commas on the last column name but I think there is likely a valid reason to do with making lexing easier to reason about.

The `KW item[,item]+ KW` grammar just sucks in general. Natural-ish grammars are really stupid and shortsighted.

Re: How about trailing commas in SQL?

#38
post #34
post #16

Earlier quoted context omitted.

That’s why I use leading commas. Unless I change the first column, no problem.

When you start working with sql you despise leading commas. When you stop working with any other code than SQL you look back on your younger self and think ‘I was soooo young’ when typing those lovely leading commas.

That sounds made up on the spot. I wrote leading commas in SQL for decades, no such thought crossed my mind. I still use and suggest them to people. They look especially good when aligning selected columns.

Re: How about trailing commas in SQL?

#39
post #34

Earlier quoted context omitted.

When you start working with sql you despise leading commas. When you stop working with any other code than SQL you look back on your younger self and think ‘I was soooo young’ when typing those lovely leading commas.

That sounds made up on the spot. I wrote leading commas in SQL for decades, no such thought crossed my mind. I still use and suggest them to people. They look especially good when aligning selected columns.

I hated them! I couldn’t stand the look.

Learned to love the functionality of these though and the looks grew on me.

Re: How about trailing commas in SQL?

#40
post #3

To this still empty thread: how about we just stop arguing and add these damn commas everywhere? How about having human- and devenv-oriented languages finally, after how many decades?

In my opinion: because the Robustness principle [1] is a non-insignificant cause of bugs and SQL injection is a major player in the vulnerability game.

The first case that jumps to me: if you write "a,b,c," you can't know whether you forgot a parameter, you passed an empty string where there shouldn't be one, or you intentionally left a trailing comma. And SQL already is human-oriented - compare your typical SQL to a data operation in K [2]. It just so happens that some things are hard and off-by-one errors are famously near the top of the list [3].

Whenever a database complains that your SQL query contains a trailing comma it's a sign that you may not have paid enough attention to your arguments (or even worse, that you're building queries by hand instead of using prepared statements). From where I stand, not allowing trailing commas is a feature, not a bug, and I would therefore object against them.

[1] "be conservative in what you do, be liberal in what you accept from others" - https://en.wikipedia.org/wiki/Robustness_principle#Criticism

[2] https://news.ycombinator.com/item?id=42999650

[3] http://www.randomprogramming.com/2014/09/quote-of-the-week-h...

Post reply on HN