Is using `!!` something that should be avoided?
# getting-started
k
Is using
!!
something that should be avoided?
๐Ÿšซ 2
๐Ÿ‘Œ 13
e
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
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:
Copy code
val notNullableVal = nullableVal ?: error("Descriptive error message here")
๐Ÿ‘ 1
r
You typically use safe calls with a scope block
b
?:
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
I am surprised that no one has mentioned that you can replace every
!!
with
checkNotNull/requireNotNull
and supply an informative message
๐Ÿ’ฏ 2
๐Ÿ˜† 2
s
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")