I have some code that in Java I'd write ```if (get...
# getting-started
b
I have some code that in Java I'd write
Copy code
if (getFoo() != null && barEnabled()) {
    doSomething();
}
In Kotlin I could write
Copy code
getFoo().takeIf { barEnabled() }?.let { doSomething() }
but I'm not convinced that's any easier to read, mostly because of the separation between the nullable getFoo() and the let. Any recommendations on how best to write that in Kotlin? Is using scope functions always preferred?
d
imho there is absolutely nothing wrong with an if statement here. It perfectly captures your intent.
j
It's possible to use scope functions, but not always recommended. I used to overuse them as well. Use them if you actually want to chain something on the value, not to replace if checks.
b
Thanks both, that makes sense! 👍