Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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).


> To make writing and maintaining sql easier.

From my experience there are a lot of programmers that simply don't write SQL at all. They use an ORM or some query building library in their preferred programming language. So they're the ones that don't understand the problem when handwriting SQL, because they simply don't do it. Even the parent comment here is talking about PHP array syntax instead of SQL.

I write all of my queries directly in SQL, and I run into the trailing comma issue somewhat frequently. It's a minor annoyance, but I've just been dealing with it forever and accepted it.


Everyone knows the hardest part about writing SQL is having to get Claude to tell you where the commas should go.


You can already do this without changing the SQL language spec. If you really care about the things you say, follow the other comments and make the first element special which is changed much less frequently.


So you can’t do it, you can do a different thing which (as I already answered in details elsewhere) still sucks but differently.

Great contribution, thanks for nothing.


Curious to see your Postgres PR with this change in it. Is it done yet?


From my own experience:

1. It makes git diff less noisy when I need to add new element at the end since you don't need to add an extra comma at the end of the now-second-to-last element.

2. It makes reordering the last element a lot more pleasant during development since you don't need to add and remove comma when you are moving the line.

And the same applies (or I wish applies) to SQL. Time and time again I wish trailing comma is a thing in SQL when I'm constructing and testing even slightly more complex queries.


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.


Yea, and if you do trailing commas, "unless I change the last column, no problem". This is exactly the problem.

Supporting leading or trailing redundant comma are both good options. Not supporting either isn't.


I rarely change the first column in a query but often the end especially while debugging. So a leading comma isn't the same problem to me.


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.


I mainly work with SQL and like them.


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.


That's for where the language parser allows you to have have leading commas i.e line continuations without a trailing comma.

And some, amusingly, don't.


You’re clearly missing the entire point. The mismatched number of commas in either orientation requires you to alter unrelated fields when making modifications.


I rarely change the beginning of the query more likely the end.


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 specifically about hand-written SQL which needs to be optimised for readability and comprehension, both in the query itself and the diffs changing them. Spreading columns and arguments across multiple lines is common if not mandatory for writing maintainable SQL.


> 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.


That's what I was thinking. Sometimes it's called join, like Enum.join/2 in Elixir.

Either you're cuddling the strings with your hands and it'll take time and care anyway, or you're doing programmatic massage, in which case most DB-driver capable languages has a bunch of functions that solve issues like these.


Might not be so bad if the syntax error for it were better than

  Syntax error at or near ')'




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: