When should one use lambdas vs. top-level function...
# codingconventions
s
When should one use lambdas vs. top-level functions vs. inner functions?
e
do you mean top level vs nested classes, or nested classes vs inner classes?
s
Yes, nested. Corrected.
k
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
@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
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.