5 ms·
I have often seen commas placed at the start of each line when columns are being listed. I'm a little surprised this guide doesn't follow that convention. It w
by no_protocol 10y ago
I have often seen commas placed at the start of each line when columns are being listed. I'm a little surprised this guide doesn't follow that convention. It would fit nicely with the established pattern of lining everything up:
SELECT first_name
, last_name
, email_address
FROM users
Can anyone speculate why it's not done this way in the guide? I think the reasoning behind it was to make later modification easier because adding or removing the last field required modifying two lines.
- ZenoArrow 10y agoYeah, basically it's a good practice to start field name rows with commas as it makes it quicker to comment them out as you're tweaking the SQL code (though there's no harm in putting multiple fields on a single row, especially if they come from related tables).
- delgaudm 10y agoI'm surprised too. I'm unaware of any disadvantages to doing it this way. I have always preferred this format myself. It makes things easier to comment out, it makes it so the clause never accidentally ends in a trailing comma before the next clause, and it looks nice.
- ryeguy 10y agoI just think it looks visually ugly. I've heard others think that too, so it's not just me. I don't feel like the problem it solves is really that big of a deal. I can always just clone the second line up since order normally doesn't matter.
- Treffynnon 10y agoThis is a pretty commonly asked question. The answer is two fold: 1. putting the comma before doesn't actually make commenting out columns easier - it just moves the problem to the other end of the list. Just try commenting out `first_name` in your example and you'll see what I mean. 2. because normal convention would be for the comma to come immediately after an item in written English.
- r00fus 10y agoRe; #1, I also use comma-first indenting and I actually put each field in it's own line specifically for ease in commenting (first_name would have its own line)
- Treffynnon 10y agoThis doesn't solve the problem at hand either unfortunately. You'll still have a hanging comma in front of `last_name`: SELECT -- first_name , last_name , email_address FROM users When parsed it will look like this query error to the system: SELECT , last_name , email_address FROM users
- restalis 10y agoBesides the first reason pointed out by Treffynnon, it's worth remembering that the code we write is primarily for our fellow humans and that a piece of code will most likely be read more times than written so we better strive to optimize it for legibility.
- lazzlazzlazz 10y agoI'm a little surprised that anybody follows the "commas at the beginning of the line" convention - but to each their own.