DEV Community

Scott Chamberlain
Scott Chamberlain

Posted on • Originally published at recology.info on

trailing commas

Let’s talk about trailing commas (aka: “final commas”, “dangling commas”). Trailing commas refers to a comma at the end of a series of values in an array or array like object, leaving an essentially empty slot. e.g., [1, 2, 3,]

I kind of like them when I work on Ruby and Python projects. A number of advantages of trailing commas have been pointed out, the most common of which is diffs:

diff --git a/hello.json b/hello.json
index e36ffac..d387a2f 100644
--- a/hello.json
+++ b/hello.json
@@ -1,4 +1,5 @@
 [
   "foo": 5,
   "bar": 6,
+ "apple": 7,
 ]
diff --git a/world.json b/world.json
index 14a2818..41f8a01 100644
--- a/world.json
+++ b/world.json
@@ -1,4 +1,5 @@
 [
   "foo": 5,
- "bar": 6
+ "bar": 6,
+ "apple": 7
 ]

Enter fullscreen mode Exit fullscreen mode

Example blog posts on the topic: https://dontkry.com/posts/code/trailing-commas.html, https://medium.com/@nikgraf/why-you-should-enforce-dangling-commas-for-multiline-statements-d034c98e36f8

Many languages support trailing commas, and some even consider it best practice to use trailing commas.

Ruby

["hello", "world"]
# => ["hello", "world"]
["hello", "world",]
# => ["hello", "world"]

Enter fullscreen mode Exit fullscreen mode

Works the same for hashes.

Python

["hello", "world"]
# Out[1]: ['hello', 'world']
["hello", "world",]
# Out[2]: ['hello', 'world']

Enter fullscreen mode Exit fullscreen mode

Works the same for sets and dictionaries.

Javascript

Mozilla gives a thorough overview of trailing commas in Javascript.

["hello", "world"]
// ['hello', 'world']
["hello", "world",]
// ['hello', 'world']

Enter fullscreen mode Exit fullscreen mode

Probably works for other data types…?

Rust

https://users.rust-lang.org/t/trailing-commas/13993

["hello", "world"]
// vs
["hello", "world",]

Enter fullscreen mode Exit fullscreen mode

Probably works for other data types…?

Julia

https://users.rust-lang.org/t/trailing-commas/13993

( 1, 2 )
# (1, 2)
( 1, 2, )
# (1, 2)

Enter fullscreen mode Exit fullscreen mode

works the same with arrays in Julia.

others

Apparently others do as well: Perl, C#, Swift, etc …

Disagree

Some do not like trailing commas:


R

However, the main dev work I do is in R, which does not support trailing commas.

c("hello", "world")
#> [1] "hello" "world
c("hello", "world", )
#> Error in c("hello", "world", ) : argument 3 is empty

Enter fullscreen mode Exit fullscreen mode

The one caveat is that you will see trailing commas in subsetting procedures of lists, vectors, data.frames, matrices, e.g.,

mtcars[1:3,]

Enter fullscreen mode Exit fullscreen mode

One blogger provides an override to allow trailing commas though I’d imagine it’s not a good idea to use as you probably don’t want such fundamentally different behavior in your own R console compared to others.

I’ve not seen any discussion of trailing commas in R as a language feature, whether good, bad or otherwise. Doesn’t mean it doesn’t exist though :)

Haskell

Like R, doesn’t allow trailing commas!

And in fact, allegedly (I don’t use Haskell):

Because it is much more common to append to lists rather than to prepend, Haskellers have developed the idiom of leading comma:

  ( foo
  , bar
  , baz
  , quux
  )

Enter fullscreen mode Exit fullscreen mode

JSON

Unfortunately for many people JSON does not allow trailing commas

see also: leading with commas

Top comments (6)

Collapse
 
cristiano profile image
cristiano • Edited

Someone else recently also told me trailing commas help with diffs which is great but I’ve also found that it helped me when I edit an array or object as the comma is already present on the previous entry, it is less prone to errors for me. 😄

Collapse
 
sckott profile image
Scott Chamberlain

Right, good point about being easier to make changes. I also appreciate that.

Collapse
 
daveparr profile image
Dave Parr

I've seen some people hit the leading comma in R as well. In fact I saw a speaker at Chicago SatRdays live stream using them: twitter.com/just_add_data/status/1...

Collapse
 
sckott profile image
Scott Chamberlain

interesting, is there a link to a talk?

Collapse
 
daveparr profile image
Dave Parr

Sure, the whole thing is here, and this is the point the leading commas are at

Thread Thread
 
sckott profile image
Scott Chamberlain

thanks!