wonder what people think about this - sometimes i ...
# codereview
g
wonder what people think about this - sometimes i think the elvis operator can be a little confusing, especially in combination with other operators - using an orElse function to specify a default
Copy code
fun <T> T?.orElse(default: T) = this ?: default
for example i have a when statement like this
Copy code
return when {
           foo.blah?.someNullableFlag ?: false -> true
}
vs
Copy code
return when {
           foo.blah?.someNullableFlag.orElse(false) -> true
}
seems a little more readable to me
s
from a strictly language-based point of view, I don’t think the elvis operator is hard to grok after being told what it does
anyone learning Kotlin should definitely go over the null-handling operators like
?:
and
?.
as far as your particular
when
goes, I’ve usually been told when dealing with
Boolean?
to do equality checks even if it looks a little silly
if (maybeBoolean == true)
, for example
👍 3
g
i don’t think it’s hard to grok on a single statement level, but it can get a little hairy when doing multiple things on a line, and in my particular case assigning the false ‘default’ to the expression side as opposed to the outcome side can cause confusion. really like the == true trick, i think i’ll go with that
👍 1
e
I'd also do an equality check
Copy code
return when {
          foo.blah?.someNullableFlag == true -> true

}
g
yes, that’s basically what i did
j
I like this little helper function! Is there a good reason you didn't
inline
it? Because it seems a perfect candidate for it to me.
g
no reason, i was just playing around with the readability of it