Which do you prefer between: ```isRunning = result...
# codereview
c
Which do you prefer between:
Copy code
isRunning = result != null && result.isActive
Copy code
isRunning = !(result == null || !result.isActive)
3️⃣ 1
1️⃣ 15
e
Copy code
isRunning = result?.isActive == true
πŸ‘ 4
πŸ‘€ 1
βž• 7
j
I personally find the nullable boolean checks with
== true
slightly less readable than the clear null check + regular boolean check. In general I don't find that saving a few characters is worth this slight readability drop.
Most of the time I'd rather refactor so there is no null in the first place anyway πŸ˜„
βž• 6
e
YMMV? I find the
?.
version easier to read than the more complex boolean expression. there's three states:
null
,
false
, and
true
, and you can clearly see which ones pass
πŸ‘Œ 1
j
Fair enough. I guess it depends on preference or past experience. I usually find that the 3 states are not in the same dimension: there is a missing info state on one side, and the true/false states on the other. So it feels wrong to me to compare them in a single expression. I prefer to see that there was a conscious decision to handle the "missing value" state here too.
πŸ‘ 1
e
https://kotlinlang.org/docs/coding-conventions.html#nullable-boolean-values-in-conditions indicates a preference towards using
== true
or
== false
with `null`able
Boolean
too
j
It does. I just disagree with it πŸ˜„
πŸ’œ 2
s
isRunning = result?.isActive ?: false
I prefer to nip nullability asap, but if unable to prior – like stating the result when null with the king elvis.
βž• 2
j
Yep I find that version clearer