I would just use if/else
# stdlib
g
I would just use if/else
3
m
Do you mean this?
Copy code
val result = something()
if (result == null) {
    println("it was null")
}
return result
instead of:
Copy code
return something() ?: run {
    println("it was null")
    null
}
or:
Copy code
return something().alsoIfNull {
    println("it was null")
}
g
yes, exactly
it also a lot more future proof, you can add else to this if, or add some custom return etc
m
Good point. For that reason, would you always assign a val before returning it?
g
it depends on the case of course
but for this example, yep, I would just create a local variable
if you want to avoid local variable I would rather do this:
Copy code
return something().also {
    if (it == null) {
       println("it was null")
    }
}
👍 3
but if else is good enough
m
I like that because it’s easy to comment out and gets rid of the “what does alsoIfNull do?” question.
In the example assigning a local val, if you comment out the middle 3 lines then lint wants to inline.
g
no, of course, I introduce local variable if it required, not alwasy before return, what would be the point of it?