What do you prefer and why? :one: `nullableBoolean...
# announcements
r
What do you prefer and why? 1️⃣
nullableBoolean == true
2️⃣
nullableBoolean ?: false
2️⃣ 2
⚖️ 1
1️⃣ 21
3️⃣ 1
l
Depends
r
In my case I’m checking for
nullableList?.contains(x) == true
but I usually don’t really know which one to choose in any case
i
you can create fun like this
Copy code
fun Boolean?.safe() { return this ?: false }
But it applicable not everywhere
l
== true
is what the IDE often suggests, but historically, I used
?: false
I don't like the naming of that extension function above.
r
I have no warning in IDEA with both solutions and have intentions to go from of to the other in both directions… IDEA doesn’t seem to show a preference
i
Maybe better fun like
Copy code
fun Boolean?.safe(default: () -> Boolean) { return this ?: default() }
But it almost the same as elvis operator
😕 1
l
If you put such an expresstion in an
if
condition, I think the IDE will suggest
== true
@ribesg
r
@iamthevoid this kinda hides the default value anyway and adds unneeded complexity
@louiscad true
n
Here it obviously comes up in an "immediate" context so it's fine, but IMHO nullable booleans should not be allowed to linger. Believe it or not some people accept them as arguments into functions, etc.
It's kind of funny, when I looked at the two options in a vacuum, I prefer
nullableBoolean == true
, but when I look at your case involving a nullable list and
?.
I prefer
?: false
So I think it just depends.
someNullableObject?.memberFunc() ?: default
is just a common pattern in Kotlin, generally, so I feel like it is applicable here
r
This is my full real use case. No if condition
Copy code
override fun hasSourcingRole() =
    authTokenService.getTokenRoles()?.contains("ROLE_SOURCING") == true
Maybe coding conventions could cover such case 🙂
@Nir I agree it can be a code smell, but sometimes it can be useful. For example in my case I could let the null value pass as a “I don’t know” value, for example if I don’t know the user yet. Here an anonymous user doesn’t have the role, so returning a non-nullable boolean is ok, but I don’t feel like it should be a hard “`Boolean?` is bad” rule
n
@ribesg IMHO it's a pretty hard rule. If you want to do that, just have an enum with 3 levels, TRUE, FALSE, and UNKNOWN.
r
Oh I hate the idea of an enum with values TRUE and FALSE so much
n
It's just making something that's potentially confusing, much more explicit
you can have a function that maps a nullable boolean to said enum of course, that way you just convert to the enum immediately when you get it, and pass it in that form
r
Yeah but this kinda feels like java-is-far-too-much-verbose explicit to me
n
it's not really that much more explicit though, at the use site
Copy code
if (foo == true)
vs
if (foo == Tribool.TRUE)
sorry, I meant "not that much more verbose"
r
You mean
if (foo)
. That’s so much more verbose, like triple the amount of letters 😛
Actually I do have models with
Boolean?
in them and it’s perfectly fine for me. For example, we have something where the user can either accept or refuse to consent to something. We store that as a
Boolean?
,
null
means we didn’t ask yet. I have no problem with that
Maybe I didn’t write any Java in 10 years and no longer fear
null
enough? 😄
☝🏼 1
n
if (foo)
doesn't compile
It's not about fearing null, it's just about being explicit. The value could be missing for many different reasons, and later on as the application gets more complex you may suddenly find that it may not be filled out for 2 different reasons and you care which one
Either, for example, they didn't fill out the form where that question is asked yet, or they did fill it out, but chose to leave it blank
and now you have to change your nullable bool to an enum anyway, which is much more headache than adding another enumeration
r
There is no possibility for the value to be missing in this case.
null
cannot mean anything else than “we never got an answer to that question”. I’m not writing forms like it’s the 90's, I’m mutating states and there is no mutator able to set this value to
null
n
agree to disagree
t
tbh in that case I would also go for an enum
ACCEPTED, REFUSED, NOT_ASKED
, then in real life I would probably opt for
Boolean?
😛 laziness always wins (I always disliked tri-state boolean also when I was mostly coding java, but mostly because in java you never know when something is or is not nullable, in kotlin you do. that small
?
makes all the difference in accepting nullable)
a
How about
Copy code
fun Boolean?.orFalse() = this ?: false
similarly to the existing
Copy code
@kotlin.internal.InlineOnly
public inline fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()
👑 1
👍 3
youtrack 2
l
I like the naming of that one!