https://kotlinlang.org logo
#codingconventions
Title
# codingconventions
s

Sam Stone

04/26/2022, 4:17 AM
When should one use lambdas vs. top-level functions vs. inner functions?
e

ephemient

08/12/2022, 8:59 PM
do you mean top level vs nested classes, or nested classes vs inner classes?
s

Sam Stone

08/12/2022, 9:00 PM
Yes, nested. Corrected.
k

Klitos Kyriacou

08/15/2022, 8:18 AM
Responding to the main question: • Use top-level functions by default. • Use an inner function if it is: ◦ very short ◦ only makes sense when used by its enclosing function ◦ is used more than once in the enclosing function • Use a lambda when it is: ◦ short ◦ only makes sense when used by its enclosing function ◦ is only used once You can interpolate this principle to nested classes and anonymous classes.
s

Sam Stone

08/19/2022, 4:08 PM
@Klitos Kyriacou are those your own conversations? Have you seen those conventions published anywhere?
And what is the point of storing the lambda in a variable if it is only used once? Just inline the code.
k

Klitos Kyriacou

08/19/2022, 4:14 PM
I haven't seen them published, but I was reporting my observations based on all the Kotlin code I've seen over the past couple of years and seeing what looks readable/unreadable and also seeing what the common idioms seem to be.
6 Views