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
]
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"]
Works the same for hashes.
Python
["hello", "world"]
# Out[1]: ['hello', 'world']
["hello", "world",]
# Out[2]: ['hello', 'world']
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']
Probably works for other data types…?
Rust
https://users.rust-lang.org/t/trailing-commas/13993
["hello", "world"]
// vs
["hello", "world",]
Probably works for other data types…?
Julia
https://users.rust-lang.org/t/trailing-commas/13993
( 1, 2 )
# (1, 2)
( 1, 2, )
# (1, 2)
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
The one caveat is that you will see trailing commas in subsetting procedures of lists, vectors, data.frames, matrices, e.g.,
mtcars[1:3,]
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
)
JSON
Unfortunately for many people JSON does not allow trailing commas
Top comments (6)
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 anarray
orobject
as the comma is already present on the previous entry, it is less prone to errors for me. 😄Right, good point about being easier to make changes. I also appreciate that.
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...
interesting, is there a link to a talk?
Sure, the whole thing is here, and this is the point the leading commas are at
thanks!