https://kotlinlang.org logo
Title
k

Kenneth

04/23/2020, 2:28 PM
Is using
!!
something that should be avoided?
:yes: 13
🇳🇴 2
e

Evan R.

04/23/2020, 2:30 PM
Generally, yeah. You should be handling nullable values whenever possible, either with safe calls (
?.
), the elvis operator (
?:
) or even throwing an exception if necessary
👍 1
s

spand

04/23/2020, 2:31 PM
Yes. It is a low level primitive. If you dont want to handle the null case you should use a higher level one like Map.getValue tha will throw with a much better description than NullPointerException.
👍 2
And if a custom case then I often do:
val notNullableVal = nullableVal ?: error("Descriptive error message here")
👍 1
r

Roger Home-Martinsen

04/23/2020, 2:54 PM
You typically use safe calls with a scope block
b

Brendan Weinstein

04/23/2020, 2:57 PM
?:
and
?.
often push the error somewhere downstream that may make less sense later on when you hear bug reports. When you launch an improperly configured experiment and find out you missed a month of business insight, you'll wish that something had crashed during the rollout to catch your attention. Logging non-fatal exceptions is great, but most teams I have been on end up with too much noise to do a good job picking up signal. A blend of these two positions is to crash on developer build variants and log a non-fatal in production.
👍 1
🤨 1
m

Milan Hruban

04/23/2020, 4:50 PM
I am surprised that no one has mentioned that you can replace every
!!
with
checkNotNull/requireNotNull
and supply an informative message
😆 2
💯 2
s

spand

04/23/2020, 5:34 PM
I must say I strongly dislike those for the single reason I have no idea which is throwing which exception and its even longer than
?: error("Yo")