Whether null counts as true or false depends on th...
# language-proposals
n
Whether null counts as true or false depends on the context. If I have a nullable bool, I use the Elvis operator:`if (state?.isValid ?: false) ...` or
if (state?.isValid ?: true) ...
g
Style guides from JB recommends to use
nullableBool == true
and
nullabelBool == false
instead
But I like to use elvis operator for local variables to make them non-nullable. It’s easier to understand what’s going:
Copy code
val isValid = state?.isValid ?: false
if (isValid) …
n
Do you have a link to that styleguide?
n
I think that gets confusing when you deal with negated conditions.
1
E.g.
if (maybeState != false)
because the null case is obscured
While using an elvis operator makes that clear
Oh well… they’re only guidelines and IntelliJ can automatically switch code between different styles with the same meaning
g
Yes, I agree that
maybeState != false
can be confusing, so just declare non-nullable local variable with elvis operation is reasonable alternative in this situation
but for simplest:
maybeState == true
is clear enough for me