https://kotlinlang.org logo
#codereview
Title
# codereview
c

CLOVIS

02/26/2024, 2:48 PM
Which do you prefer between:
Copy code
isRunning = result != null && result.isActive
Copy code
isRunning = !(result == null || !result.isActive)
3️⃣ 1
1️⃣ 14
e

ephemient

02/26/2024, 3:06 PM
Copy code
isRunning = result?.isActive == true
6
👍 4
👀 1
j

Joffrey

02/26/2024, 4:03 PM
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

ephemient

02/26/2024, 4:18 PM
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

Joffrey

02/26/2024, 4:21 PM
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

ephemient

02/26/2024, 4:23 PM
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

Joffrey

02/26/2024, 4:24 PM
It does. I just disagree with it 😄
💜 2
s

Shaun Burch

02/28/2024, 3:58 AM
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

Joffrey

02/28/2024, 8:39 AM
Yep I find that version clearer