Re: IN clause behaving badly with missing comma and line break

From: Francisco Olarte <folarte(at)peoplecall(dot)com>
To: Philip Semanchuk <philip(at)americanefficient(dot)com>
Cc: Roman Cervenak <roman(at)cervenak(dot)info>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: IN clause behaving badly with missing comma and line break
Date: 2023-01-18 16:06:21
Message-ID: CA+bJJbw=RvKYrespkhD5+=y8SmVXmhtewhaf2MbfVAb2wV6NQQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, 18 Jan 2023 at 16:21, Philip Semanchuk
<philip(at)americanefficient(dot)com> wrote:

> .... We’ve adopted a formatting standard that helps us to avoid surprises due to missing commas. We put one string literal on each line and place the commas all in the same column with a significant amount of white space to the left of the commas. With this safeguard in place, it’s very easy to spot a missing comma.
>
> WHERE t IN ('a' ,
> 'foo' ,
> 'bar' ,
> )

I believe that one is a syntax error ( last comma ).

I use a slightly different one, I put commas before the second and
subsequent elements.

WHERE t IN (
'a'
, 'foo'
, 'bar'
)

Both in SQL and in other languages with list constructs. I switched to
that a couple decades ago as many languages do not allow a trailing
comma in literal lists, and ..

- Adding/deleting a first value is rarer ( in my experience ) than
adding/deleting a last one. With the comma first you only edit two
lines when adding/deleting the first one ( commas last means you have
to do it when touching the last ). Same can be said when moving lines
around using cut & paste, you only have to touch the lines contents
when moving the first one.

- Commas line up without worrying about padding when constants have
different lenghts. Also I do not have to worry if a line goes of the
edge due to narrow editor windows.

- I look more to the beginning of the lines than the end, so spotting
missing commas is easier ( for me ).

- Looks pretty to me :-p

I uses it a lot in SQL, specially for field lists in DML, as I tend to
put them in several lines, and found it better ( for me ) then commas
last.

Francisco Olarte.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Philip Semanchuk 2023-01-18 16:09:38 Re: IN clause behaving badly with missing comma and line break
Previous Message Philip Semanchuk 2023-01-18 15:20:57 Re: IN clause behaving badly with missing comma and line break