What is the reasoning behind this? I don't think i...
# intellij
r
What is the reasoning behind this? I don't think it makes it clearer. If i have a nullable boolean, then
?: false
implies "default to false". I find this more readable than
== ...
.
4
k
I would agree with you if it wasn't already a commonly understood idiom and an official guideline to use
== true
.
r
I was wondering the same, actually... I also prefer
?: false
. I found this discussion where someone suspected it had something to do with the operator precedence rules (
==
being lower than
?:
), although I could also not come up with a good example for this yet. Another possible reason I can think of is that it might produce more optimal byte code? (equality check instead of branching), although this might be something the compiler is smart enough to optimize by itself...
👍 1
c
imo ?: false more clearly communicates that it is about nullability. == true looks redundant.
👍 1
r
Hi! Indeed, the official style guide recommends using equality to check nullable booleans However, in IntelliJ, we have inspections to support both styles of such comparisons. If you and/or your team prefer some particular style over the other, you can change the default settings for those inspections and save them in the repository, so that it is applied across the team (I will post the actual name of the relevant inspection later, sorry)
👍 1
thank you color 1
c
the official style guide mentions only conditional statements, there == true makes more sense (but ?: false still communicates better that its about nullability imo) for the case of your screenshot i don’t think the statement from the official guideline applies.
👍 2
e
the
Boolean?
type is inhabited by 3 values:
null
,
false
, and
true
. using
==
makes it obvious which one is being singled out
there's nothing special about
null
when it's just another value that's properly tracked by the type system
r
Yeah, but the intention of the developer here is not to single out a boolean, but to take a nullable boolean or default to another value, hence the elvis operator use, to make it obvious of what the fallback is 🙂
e
there are 8 possible pure
(Boolean?) -> Boolean
, which are
Copy code
{ false }
{ true }
{ it == true }
{ it == false }
{ it == null }
{ it != true }
{ it != false }
{ it != null }
you can't express most of them with
?:
c
but isn’t that also a reason why ?: is better? you don’t really want to open that can of worms unless you really need it.
👍 1
e
Copy code
{ it ?: true }
{ it ?: false }
{ !(it ?: true) }
{ !(it ?: false) }
is not an improvement
r
You are looking it from a different perspective. In some cases, I want to convey the meaning that "take this nullable, or default to this one". In those cases, using the elvis operator makes it clearer, at least in my eyes. I would never user !{it ?: false} for example, because I never want the code to say "take the opposite of the value or it's default of false".
e
I find such cases come up pretty often, e.g.
Copy code
Model(
    editable = props["readonly"]?.toBoolean() != true,
)