Live data from Hacker News

How about trailing commas in SQL?

peter.eisentraut.org

221–230 of 272 posts

Re: How about trailing commas in SQL?

#222
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 not about typing; code is never really about keystrokes. It's about easily moving lines around (up/down, copy paste, etc), diff noise (only one line changed instead of two), etc. See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... ; it's always been allowed in JS, even though if I recall correctly Internet Explorer didn't allow it. Anyway, SQL is a different beast entirely, this is specifica…

> It's not about typing > It's about easily moving lines around

Those are the same thing, just different keystrokes.

> diff noise

This is a non-issue, or rather a solved issue. Any half competent diff tool does word diffs instead of just line diffs. Changing the syntax of many (every?) programming language to promote "better diffs" seems over the top. And what's the result? "a,b" -> "a,b,c" vs "a,b," -> "a,b,c," both have equally "noisy" diffs.

> Spreading columns and arguments across multiple lines is common if not mandatory for writing maintainable SQL.

I agree. But I disagree that a trailing comma makes it more readable or maintainable in any way. I can't look at a trailing comma without wondering what's missing.

Re: How about trailing commas in SQL?

#223
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…

I have never been a fan of leading commas. How often are we hastily moving column expressions around?

I also have a somewhat controversial style preference in regard to capitalization. Because SQL is case-insensitive, I will always type everything in lowercase and let syntax highlighting do its thing. I hate mixed capitalization. Not only does it feel like keywords are yelling at me, but also the moment someone else gets involved there will be inconsistent casing. Do you capitalize just the keywords or do you include functions? How about operators (e.g. “in” or “like”). More often than not I see individuals are inconsistent with their own queries. So I say to hell with it all and just keep it lowercased

Re: How about trailing commas in SQL?

#226
I'm a big fan of adding `WHERE 1=1` to the first line of a where clause. Then all remaining lines are prefixed with `AND` or `OR` which is nice for readability and indentation, and makes it easy to comment out a line while iterating.

Re: How about trailing commas in SQL?

#227
> We support most cases, except the ones that are too complicated to implement or cause grammar conflicts.

It would be really helpful if the author had provided at least a couple of these.

I can't think of any obvious examples that would be complicated/conflicting, so it's not even clear if this is real complexity or not. I mean, it might be, but let's at least demonstrate that concretely?

SQL grammar is pretty limited. Surely it can't take more than half an hour or so to check if any grammatical ambiguities could be introduced -- or at least a quick first pass? This whole post is postulating about theoreticals when it could just answer some of them.

Re: How about trailing commas in SQL?

#228
post #13
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…

> 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. You have simply moved the "special" entry to the beginning rather than the end. Side remark: I've noticed that when it comes to code formatting and grammar it's almost like there are broadly two camps. There are some instances of co…

> You have simply moved the "special" entry to the beginning rather than the end.

Yes, but

1. Terms are more commonly added to the end than the beginning.

2. The beginning is often already special, e.g. starting with `SELECT`.

3. The commas are visually aligned/consistent with the indentation.

4. Because of #3, it's far easier to spot a missing comma.

I worked for years in a SQL-heavy role, and this style was the preference there, for these reasons.

Post reply on HN