hciinq
hciinq

Reputation: 63

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.

Upvotes: 4

Views: 800

Answers (1)

João Dias
João Dias

Reputation: 17460

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

Upvotes: 2

Related Questions