wonder what people think about this - sometimes i ...
# codereview
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
Copy code
return when {
           foo.blah?.someNullableFlag.orElse(false) -> true
seems a little more readable to me
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
as far as your particular
goes, I’ve usually been told when dealing with
to do equality checks even if it looks a little silly
if (maybeBoolean == true)
, for example
👍 3
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
I'd also do an equality check
Copy code
return when {
          foo.blah?.someNullableFlag == true -> true

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