Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why lambda argument should 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading