Small Thing, Big Impact

Published: (February 27, 2026 at 03:18 PM EST)
5 min read
Source: Dev.to

Source: Dev.to

Trailing commas

Trailing comma illustration

I will tell about a very obvious topic: trailing commas. Many of you already handle them and even care about them, because it feels natural to put a comma after the last parameter when defining a function or a class, or when calling them. This is just my self‑reflection, and I try to formulate and finalize this moment for myself.

Trailing commas are a basic concept now, but we need to go slightly back to understand why they arose. The first programming language that allowed a comma as a separator in syntax was the first version of Perl, designed by Larry Wall (December 1987). It wasn’t mentioned in the documentation at first, but the syntax caused no problems. It was explicitly documented only in Perl 4 (1991).

After this, the trailing‑comma concept started to appear in many languages, often in a limited scope (Python, Ruby, JavaScript, C#). This picture changed quickly after the invention of Git in 2005. With the increasing popularity of this version‑control system, trailing commas transformed from a mere syntax convenience into a team‑collaboration optimisation that allows smaller diffs and fewer merge conflicts. Since then, most modern languages have very wide support for trailing commas (Go, Rust, Swift, Kotlin, TypeScript) or have added limited support (C++, PHP).

Trailing comma makes diff small

An interesting fact is that trailing commas are very limited in Java. From Java 4 they are allowed in enums, and from Java 8 in array initialisers and annotation arguments. In Kotlin, trailing commas appeared from version 1.4 (2020).

Timeline of Trailing Comma Acceptance in Programming Languages

Why use a trailing comma?

Nothing forces us to use them; the code will compile and run either way. The problem appears when working in a team on the same code base, especially when multiple people edit the same file in parallel. Simple changes—like adding a new parameter to a constructor—can cause a merge conflict in a place where, logically, there should be none. The conflict arises because the missing or extra comma changes the surrounding line, even though the new parameter itself doesn’t conflict with existing logic.

These unnecessary conflicts waste time: resolving them, rerunning pipelines, or switching branches locally (stashing progress, syncing the project, etc.) all add overhead.

If you don’t care about trailing commas, you’ll likely encounter this problem repeatedly.

Making trailing commas mandatory with ktlint

The solution is simple: enable ktlint to treat a missing comma as an error and block PRs that contain the issue.

Configuring .editorconfig

To enforce this rule for every team member, configure the .editorconfig file (stored under version control) accordingly. The relevant flags are enabled by default, but you can set them explicitly:

# .editorconfig

ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
  • ij_kotlin_allow_trailing_comma — controls trailing commas in declarations (constructor parameters, function parameters, etc.).
  • ij_kotlin_allow_trailing_comma_on_call_site — controls trailing commas in calls (when invoking a constructor, function, etc.).

Kotlin’s style guide strongly recommends trailing commas in declarations, while usage on the call site is left to team convention. See the official Kotlin coding conventions – Trailing commas.

I personally prefer trailing commas in both cases for consistency and to prevent merge conflicts. After configuring ktlint, you can format the entire codebase with a single command:

./gradlew ktlintFormat

This adds missing trailing commas wherever appropriate, allowing you to commit the changes as a single, clean commit. I recommend doing this promptly and asking the whole team to rebase on the updated devel (or main) branch.

Why enable trailing commas in Kotlin?

When you start a new branch from develop, you can run into merge conflicts later if the branch has a long history. Trailing commas help avoid those conflicts and make code maintenance easier.

Enabling trailing commas in Android Studio

  1. Open Settings → Editor → Code Style → Kotlin → Other.
  2. Check Use trailing comma.

Now every time you reformat code with Option + Command + L, Android Studio will add a trailing comma where appropriate.

  • By default, trailing commas are added only in declarations.
  • To enable them at call sites, add the following flag to your .editorconfig file:
ij_kotlin_allow_trailing_comma_on_call_site = true

Practical benefit when sorting parameters

When you have many parameters and need to reorder them (e.g., using Option + Command + Arrow Up/Down), the operation can break if the last parameter lacks a trailing comma. Without the comma you’ll get a compilation error; with it, the sort works smoothly.

Sorting parameters with and without a trailing comma

Additional advantages

  • Consistency – every line in a parameter list looks similar, making future edits or copy‑pasting easier.
  • Safety – the trailing comma does not affect the compiled bytecode; it’s purely a source‑code convenience.
  • Merge‑conflict reduction – fewer diffs when collaborators add or reorder parameters.

The trailing comma is a small, safe improvement that can make Kotlin code more convenient and less error‑prone. I’ve closed this question for myself and will refer back to this article whenever I need to justify using trailing commas in Kotlin.

0 views
Back to Blog

Related posts

Read more »