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

Leading comma is nice in SELECT statements because you can comment toggle individual lines. Indenting your code to make it more readable is basically what anyone with room temperature IQ does automatically. A lot of these other tips look like they're designed to deal with SQL design flaws, like how handling nulls isn't well defined in the spec so it's up to each implementation to do whatever it wants.


A lot of databases support trailing commas in select clauses.

Which is just as well. I want to scratch my eyes out every time I see someone formatting with comma starting the lines. It's the kind of foolish consistency that is a big part of performative engineering.


> I want to scratch my eyes out every time I see someone formatting with comma starting the lines

Right!? I _physically_ recoil every time I see that. I think that's the clearest example of normalisation of deviance [1] I know. Seems like anyone that enters the industry straight from data instead of moving from a more (software) engineering background gets used to this.

And the arguments in favour are always so weak! - It's easier to comment out lines - Easier to not miss a comma

Those are picked up in seconds by the compiler. And are a tiny help in writing code vs violating a core writing convention from basically every other language.

[1]- https://danluu.com/wat/


I'm a data person and despite seeing this for years, still despise that approach to commas. Seriously, it's not that hard to comment out the damn comma.


> A lot of databases support trailing commas in select clauses.

Which ones?

Postgres, Oracle, SQL Server, MySQL, MariaDB and SQLite do not allow that.


Just append a random constant at the end of every SELECT column list instead, 42 to the rescue! (I kid, I kid.)

I can't tell yet whether my experiment starting an all-OR WHERE clause with 0=1 so each OR could start the next line would go over like a lead balloon here too or not.

One thing I've actually found useful especially in SQL is always including a single-line comment in front of the closing of every multi-line comment, so that using a single-line comment to toggle off the opening of the multi-line comment toggles the entire block back into action and the end is already set (the closing comment is itself already commented out). This obviously doesn't work if multi-line comments start nesting, but that can confuse parsers enough already.

Trying to get an entire team on the same page with SQL formatting is one of the mothers of all bikesheds. In any case it's useful to be aware of the idioms whether or not they are personally palatable.


SQL has so little room for expression or opinionated formatting, so it's funny to people bikeshed over comma placement. I'm kind of jealous that they have time to think about whether left comma or right comma offends them greatly.


I guess I'm being spoiled by BigQuery :)

To be fair, BigQuery SQL is improving at quite a pace. If you follow their RSS, they are often announcing small but solid affordances like trailing commas, the new RANGE datatype, BigLake, some limited grouping and equality for arrays and structs, etc.

It is also probable that they expose Google's new query pipe syntax. Currently there are some hints from the error messages in the console that it's behind a feature flag or something.


Postgres and postgres-likes (e.g. Redshift) notably don't support trailing commas in select clauses.


The only ones that matter don't. If you care that much about seeing a comma somewhere you shouldn't, you have no real problems and shouldn't be complaining.


I host to help in the continued scratching of your eyes out in the future.

Leading commas are king. Simple as.


Do you also use leading commas in the IN clause? What about in function definitions? Or JSON? Leading commas are ugly cargo culting. Simple as.


This is only true if you comment out the last line in the SELECT clause. It’s ugly code and the justification doesn’t pass the sniff test.


When you comment the first statement, that doesnt have the ",", it will break and you still have to remove the "," from the second line, so your comment is not valid.




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

Search: