4 ms·
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
by elric 2y ago
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.
- robertlagrant 2y agoIt'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.
- croes 2y agoThat’s why I use leading commas.
- Someone1234 2y agoThe first line cannot have a leading comma resulting in inconsistency then too.
- lloeki 2y agoThat'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.
- sixothree 2y agoYou’re clearly missing the entire point. The mismatched number of commas in either orientation requires you to alter unrelated fields when making modifications.
- croes 2y agoI rarely change the beginning of the query more likely the end.
- masklinn 2y agoTo 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).
- VWWHFSfQ 2y ago> 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.
- karmakaze 2y agoYou 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.
- masklinn 2y agoSo 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.
- trailingcoma 2y agoCurious to see your Postgres PR with this change in it. Is it done yet?
- trailingcoma 2y ago
- m-schuetz 2y agoDuring 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.
- croes 2y agoThat’s why I use leading commas. Unless I change the first column, no problem.
- baq 2y agoWhen 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.
- nurettin 2y agoThat 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.
- baq 2y agoI hated them! I couldn’t stand the look. Learned to love the functionality of these though and the looks grew on me.
- croes 2y agoI mainly work with SQL and like them.
- boxed 2y agoYea, 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.
- firen777 2y agoFrom 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.
- cess11 2y agoThat'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.
- mplanchard 2y agoMight not be so bad if the syntax error for it were better than Syntax error at or near ')'
- Cthulhu_ 2y agoIt'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/Reference/Trailing_commas 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.
- elric 2y ago> 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.