Trailing or Leading Commas?

thatjeffsmith Database Stuff 16 Comments

Tell Others About This Story:

For me, it’s leading. If I have a code or syntax problem, I find it easier to see the line missing the comma if I can look at the first char position.

Commas come first!

Commas come first!

Before and after formatting in SQL Developer

Before and after formatting in SQL Developer

Of course you may say that’s crazy, and any reasonable programmer would demand trailing commas. I’m guessing your IDE offers formatting options that allow you to choose what works best for you. A few minutes investing these could save you hours of searching through your code and probably a few headaches.

Most IDEs have way more options than you’ll ever need or investigate. But you look at code all day, often inherited from other less….artful folks. At least make it easier to read.

Tell Others About This Story:

Comments 16

  1. Speaking as another JeffS, I agree. Leading commas is better. It may be slightly harder to read, but the code management benefits outweigh that issue.

    Plus, it isn’t actually much harder to read. It is just harder to read *initially* because someone not used to it may not expect it.

    But there are many benefits:
    – easier to comment
    – easier to swap lines (esp. if using Vi)
    – Less likely to forget that last comma when adding a new line to the end of the list

    Jeff S

    kthanksbye

  2. Pingback: SQL Developer Quick Tip – Leading Comma Formatting

  3. Pingback: Jumpin’ Jack Flashback Tables

  4. I really see the advantage of trailing commas. Just ma brain is so trained for leading commas (as chet described above) – so it still hurts. Therefore I use trailing commas, even it makes me slightly more work).

    1. JeffS Post
      Author
    1. JeffS Post
      Author

      Yeah, when it comes down to it, the developer’s IDE should make the code look the way they need it to. How it’s stored in the database, not sure it really matters. Trivial discussion really, but this has generated more comments than most topics I’ve raised here, so it’s raised a few people’s cackles 🙂

  5. I have been using leading commas for 10+ years now. As Cade mentioned it makes it much easier to manage your code (or someone else’s). That being said, I still see different approaches to indentation and block comments that are just as personal as the placement of commas.

    1. JeffS Post
      Author
  6. About 3.5 years ago I switched to leading commas after many years of trailing commas, in everything from SQL, C, C++, C#, etc.

    Leading commas make your maintenance of lists SO MUCH EASIER. SELECT lists, parameter lists, ORDER BYs, GROUP BYs, code generation. Just about everything is easier. For the same reason I use a leading logical operator, like OR and AND. Commenting, cutting and pasting and moving things around is all FAR EASIER with leading commas. What surprised me was how it took me 15 years of professional programming before I even considered doing it this way (I saw it in code-generated SQL Server code).

    1. JeffS Post
      Author

      Took me about 10 years, and only after someone asked me how to setup Toad to allow for it. When I asked why, the answer totally left me gob-smacked. The obvious isn’t always so obvious? That’s why I love Twitter. So much more exposure to smart people, willing to share.

  7. I personally prefer (and put) commas at the right, but recognize that it works better if they are at the left. It’s easier to comment out columns, for example.

    Making missing columns visually obvious is better because they often don’t give syntax error. Rather it interprets “select columna columnb…” as an alias. Code would be safer if the “AS” wasn’t optional for aliasing.

    1. JeffS Post
      Author
  8. Pingback: Tweets that mention Trailing or Leading Commas? | 140,000 Characters or Less -- Topsy.com

  9. JeffS Post
    Author

    ” I find it hard to believe that developers with more than 2 or 3 years of experience haven’t developed their own style.”

    Maybe the difference between a developer and a programmer?

  10. I understand the arguments for and sometimes I wish I did it…but, we in the west read from left to right. a comma is a natural pause, therefore, it should always be at the end.

    I have mostly given up on this fight though. All I ask is that you be consistent, and consistent is ugly in your case, but that’s OK. I will live. Joking aside, I find it hard to believe that developers with more than 2 or 3 years of experience haven’t developed their own style. Sometimes upper case keywords, sometimes not. Sometimes upper case identifiers, sometimes not. How in the f can you not be consistent?

    People…other people have to read your code. For the love of all things holy, get some style.

Leave a Reply

Your email address will not be published. Required fields are marked *