Home Commas!, Everywhere,
Post
Cancel

Commas!, Everywhere,

Trailing commas is a great feature in Kotlin that has been around since version 1.4 (2020).

A trailing comma is a comma left dangling w/o purpose, for instance in this example:

1
2
3
4
listOf(
    "foo",
    "bar", // <-- trailing comma
)

Why use trailing commas

The primary motivation is to produce cleaner diffs. If we didn’t use the trailing comma in the previous example, and we wanted to add a new element to the list, we would have to change multiple lines:

1
2
3
4
5
listOf(
    "foo",
    "bar", // Comma added
    "baz" //  actual addition
)

🔖 For a more elaborate example, see the proposal to add trailing commas to JavaScript here

Why does this matter?

By keeping the versioning history clean, we make it easier for whoever comes digging into our code next time. They’ll immediately see the actual reason for "bar" being there since blame will identify the commit which added "bar" instead of our change that just added the comma.

We also make our changes slightly easier to review. Given that there’s no cost involved in having the trailing comma, it feels like a no-brainer to add it.

Oh.. and we also make it easier to re-arrange items in the list!

Enforce trailing commas

Ktlint enforces trailing commas by default since v0.48. I like to add it as a Gradle plugin and pre-commit hook, so it runs automatically when Gradle performs verification tasks in CI, and before I accidentally commit bad code locally.

Note that Ktlint also enforces a much larger set of rules, which provide similar benefits as trailing commas.

This post is licensed under CC BY 4.0 by the author.