Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should lambda argument be moved out of parentheses?

Kotlin coding conventions (https://kotlinlang.org/docs/coding-conventions.html#lambdas) say: "If a call takes a single lambda, pass it outside of parentheses whenever possible."

What is the reason for this recommendation? Personally it strikes me as a confusing special-casing resulting in an ugly code but I imagine there might be a good reason for that.

like image 511
hciinq Avatar asked Sep 02 '25 06:09

hciinq


1 Answers

As you said, it is simply a recommendation because it makes the code easier to read (in JetBrains' people opinion and in my opinion as well). Your code would also work but it becomes harder to read.

val list = emptyList<Int>()
list.filter { it > 10 }  // no parentheses
list.filter({it > 10})   // with parentheses and harder to read

Below you can see the result of the "Inspect code" action on IntelliJ when you have something like the code above. It is clearly only a style "issue". enter image description here

like image 196
João Dias Avatar answered Sep 04 '25 20:09

João Dias