Kotlin 1.4 — Trailing Commas

Sanket Meghani
2 min readJun 3, 2020

--

Kotlin released milestone 1 for version 1.4. One of the changes in language is the introduction of trailing commas. It is a minor syntactical change which allows us to place an additional comma at the end of parameter list in function, lambdas etc.

One of the advantage of placing a trailing comma in a parameter list is, it makes the version control diff clearer when we add or remove last parameter in the list.

Following example shows the diff when a new parameter was added while trailing commas are not used.

As shown in above screenshot, when we add a new parameter, the diff shows two lines as changed (line no 21 and 22). One for the comma and one for the third parameter itself. Similarly the diff shows two lines as changed (line no 8 and 9) where we invoke the function as shown below.

With kotlin 1.4, we can add a trailing comma to parameter list as show in following screenshot (line no 14).

Now if we add a third parameter to above function, then the diff is much clearer, as shown below it only shows a single line changed where we added a parameter.

Having clearer diff on version controlled code may not sound like a big deal. But they certainly make the code reviews easier and could also clearly indicate intent of code change.

--

--

Sanket Meghani

Technical Advisor, Architect & Fullstack Developer — JavaScript, NodeJS, React, Redux, Java, Container Technologies