Commas in Code

  • Which is your coding style, and why?

    select

    Field1,

    Field2,

    Field3

    from sometable

    or

    select

    Field1

    , Field2

    , Field3

    from sometable

    Similarly:

    select stuff

    from sometable

    where

    Field1 = 1

    and Field2 = 2

    and Field3 = 3

    or

    select stuff

    from sometable

    where

    Field1 = 1 and

    Field2 = 2 and

    Field3 = 3

    I was thinking of making this a poll, but not sure if you can add comments to polls.

    If this has already been discussed, please pardon me..couldn't find it.

    Kurt

  • In straight SELECT statements, I tend to use

    SELECT Field1, Field2, Field3

    FROM Table1

    and I continue the fields until I run out of horizontal space or I consider them no longer readable.

    If the fields go to a second line, I put the comma at the end of the first line. I do this partly for readability and partly because I find the comma alone at the beginning of a line distracting.

    As for the second pairing, I tend to make it

    WHERE Field1 = 1

    AND Field2 = 2

    AND Field3 = 3

    The indents let me know I'm not done with my WHERE statement and the ANDs being on the left serve a similar purpose.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • I'm pretty much good with the above mostly for readability and because SQL was originally designed to be "English like" which doesn't have commas at the beginning.

    People who put commas at the beginning do it to make the last entry in a SELECT list easier to comment out. The first entry is easier to comment out if the commas trail. All the other rows between the first and the last row are easy to comment out either way. Since I don't tend to comment out code once I get it working, I stick with the more "English like" habit of trailing commas.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I tend to put the commas in front, but that's a habit I get from writing SQL autogenerators, where it's slightly easier to special case the first line of a group than it is the last line.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanks for your thoughts, guys.

    I like the more English-like style, myself. To me it just seems easier to read.

  • I like to use the commas in front and the indented AND's, because they signify that something else is in the list and easier to comment out. Also, like the 1=1 after the WHERE clause to comment out one or more AND statements.

    SELECT

    t.column1

    , t.column2

    --, t.column3

    , t.column4

    FROM

    table1 t

    WHERE

    1=1

    --AND t.column5 = 'foo'

    AND t.column6 = 'bar'

  • I usually put commas at the end of the line in production code (I also tend to do one column per line in my SELECT clauses, for readability).

    But while developing ad-hoc, or testing, code I'll often use commas in front so that (as Jeff pointed out) it's easier to comment out the last lines.

    With ANDs I put them, indented, at the beginning of lines in production (I find this easier to read).

    But, as with above, I will often use a different style when doing ad-hoc queries or testing code. Then I'll often put the AND and the clause on separate lines to make it easier to comment out any section of the WHERE clause, beginning, middle or end (this works with columns and commas as well).

    Once the code goes into production my main concern is readability, not flexibility.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply