Ask HN: Do you prefer tailing or leading comma?

by aabbcc1241on 12/14/2024, 10:27 AMwith 4 comments

In the js space, people often leave a tailing comma at the end of each line, when writing the elements of an array.

I was the practise of using leading comma in Elm, where we put a comma at the beginning of each line, when writing elements of a list.

I'm also using leading comma (and leading and/or) when writing sql query.

For example:

    select
      user.id
    , user.username
    from user
    where user.ban_time is null
      and user.activate_time is not null

by compressedgason 12/14/2024, 12:42 PM

Trailing in languages which support ending the last item with a comma as this follows the usage of the semicolon.

  [ 1, 
    2, 
  ]
Leading otherwise as no language I can remember supports a leading comma on the first item:

  [ 1 
  , 2 
  ]
The entire point of this is to avoid needing to edit the prior line when inserting items into an existing lexical list this makes the diffs just:

  +
instead of:

  - 
  +
  +
If you don't care about how your line diffs look, don't bother. Just auto-format your code instead.

by dsqon 12/14/2024, 3:00 PM

For me leading commas make stuff easier to debug, you don't have to worry about the commas when you comment out the line on the select statement.

by vouaobrasilon 12/14/2024, 12:22 PM

Leading comma seems more intuitive because it immediately signals the end of an entry next to the entry.

by solardevon 12/14/2024, 9:45 PM

I just hit format code and let the IDE or formatter worry about it.