Monday, July 14, 2008

Commas: Before or After the Line?


Create polls and vote for free. dPolls.com


I'm in a new group again which means I have to learn (and accept) other people's style. No one at WellCare put commas before the line (thankfully), but I've found a few here.

I've finally come to accept that this is just style and doesn't really matter, as long as the code does what it's intended to do.

Yes, it's silly, but we all have our little quirks right?

13 comments:

Clever Idea Widgetry said...

That's why the good Lord saw fit to create PL/SQL Developer and it beautification tool. That way everyone can write it their way.

oraclenerd said...

True enough.

Personally, I like typing it all out myself. I don't use intellisense at all. I believe it helps me remember things better, typing them out that is.

It's sort of like the cellphone and speed dial. I used to know everyone's phone number, now I hardly know my own. I don't want the same thing to happen to my tables... ;)

chet

DomBrooks said...

I need to vote "depends / both depending on statement"...


I type everything out as well. I justify it on the basis that it adds another sanity check.

Never trust a tab. I've never met a formatter that did things exactly as I like.

Boneist said...

I always put my commas after the line, because that's what I'd do in English - I wouldn't do:

Hello there ,how are you?

It just looks wrong!

As wiser heads than mine have said, code gets written once or twice, but read many more times. So, you write your code to be read.

(I methodically go through and move every comma to after the line in code that I'm changing... which is a bit sad, really!)

oraclenerd said...

@boneist

I do that too!

I agree, it "reads" better at the end of the line. Maybe that's why I have such a, umm...dislike for it?

chet

oraclenerd said...

@dom

Rumor has it that the new SQL Developer formatter is very flexible...you can set it up however you want.

I've not tried it yet though. I'm still going to type it out.

chet

Dan McGhan said...

Chet,

I do commas after the line in SQL and before the line in PL/SQL. How's that for strange ;)

SQL developer 1.5 made a great leap with its formatter, but it's still lacking. I'm really looking forward to this working well so that the code can be standardized.

Regards,
Dan

oraclenerd said...

That is strange...perhaps all your sense escaped through your hairless head? :)

Oh wait...what does that say about me?

chet

Anonymous said...

After the line...

Gonna have to fix that :-D

John T

Paul Oliver said...

Before the line is better in my opinion because commenting out columns doesn't leave you fiddling with commas.

Like so:

SELECT val1
,val2
,val3
,val4
FROM TABLE

I can comment out the last two columns (val3 and val4) quickly without giving a thought to making sure I put a comma at the end.

See:

SELECT val1
,val2
-- ,val3
-- ,val4
FROM TABLE

Yay! Valid SQL.

(Yes, if you comment out the first column you do have to fiddle with commas. However, I find that if I comment out columns it is almost never the first column)

However, if I wrote

SELECT val1,
val2,
val3,
val4
FROM TABLE

Commenting out val3 and val4 would leave the trailing comma after val2, which is a pain:

SELECT val1,
val2, -- BAD COLUMN HERE
-- val3,
-- val4
FROM TABLE

It gets even more annoying when I'm commenting out val4 and val2, for example. I have to do some searching and re-parsing to figure out just where all the columns go.

SELECT val1,
-- val2,
val3, -- ARGH!!!
-- val4
FROM TABLE

What's worse with comma after the line, once you are done troubleshooting (maybe bringing back some joins that you temporarily removed to see why you have too many rows) you have to go back and fiddle with your commas again to get it back to valid SQL.

Yes, comma-before may look alien at first (it did to me). However, the productivity gained by switching to it really is nice. I now tweak queries without giving much thought to where my commas are.

oraclenerd said...

@c0ach

I hear ya. That is the one-and-only reason to ever put a comma at the front of the line.

For me, it just comes down to the way I read, from left to right. As someone else said, "it looks alien" to me. ;)

Anonymous said...

I put the comma before, for the reason cOach pointed out plus the fact that cut and pasting from one query to another easier.

This not only applies to the select list but also to the list of tables, for example:

select acc.account_number
, p.party_name
, p.category_code
, p.country
, col.name collector
, (select sum(amount_due_remaining)
from ar_payment_schedules_all ps
where ps.customer_id = acc.cust_account_id
and ps.status = 'OP')
total_balance
from hz_parties p
, hz_cust_accounts acc
, hz_customer_profiles cp
, ar_collectors col
where p.party_id = acc.party_id
and acc.cust_account_id = cp.cust_account_id
and cp.site_use_id is null
and cp.collector_id = col.collector_id

M@ said...

I just discovered the comma-before-line way and I'm so happy. I can now use my editor's affinity for manipulating lines without having to think about the surrounding syntax.