edrd
02/21/2022, 10:23 PMit
only in single-line lambdas?
Example
Good: urls.filter { it.startsWith("https://") }
Bad (the further down code goes, the harder to understand what it
means):
urls.forEach {
// some code doing some complex stuff
// some more code to increase cognitive load
val response = request(it)
<http://logger.info|logger.info>("Request to $it got response: $response")
}
Klitos Kyriacou
02/21/2022, 10:32 PMit
should be used only in single-line lambdas, but at the same time I disagree with using it in lambdas with too many lines (the more lines, the more I disagree with using it
). It's not black/white, but shades of grey. In most cases, up to 3 lines is ok.
In particular, I avoid using it
in any lambda that has another lambda nested inside it.ephemient
02/21/2022, 10:50 PMlines.filter { it.all { it.isUpperCase() } }
because it's still clear what each name refers to. but if it's not clear then definitely name themtherealbluepandabear
02/22/2022, 8:13 PMit
can be used in any lambda expression, and I don't see any reason why you couldn't use it for more than one line; although it should be strictly avoided if you are nesting lambda expressions within each other, mostly because of the confusion.