Can we use nested scope function, apply, run, with...
# android
r
Can we use nested scope function, apply, run, with, let, also. Is it a good practice. If yes, how many nested scope would u prefer?
👍 1
d
There’s some guidance in the official documentation about this: https://kotlinlang.org/docs/scope-functions.html#function-selection
Although the scope functions are a way of making the code more concise, avoid overusing them: it can decrease your code readability and lead to errors. Avoid nesting scope functions and be careful when chaining them: it’s easy to get confused about the current context object and the value of
this
or
it
.
👍 1
👍🏻 1
k
In my experience, it tends to decrease the readability of a code.
Usually I'll use just one, and if I need more I might actually add to readability with a variable names. e.g.
Copy code
val something = x?.let{}
val somethingElse = something.apply{}
...
r
Cool😀