Also I have this question which I hope you could answer: when should I use nullable types in Kotlin? Should you always use nullable types or just when they are needed? I am a bit confused. Thank you.
i
itnoles
03/02/2021, 6:26 AM
Just as needed
m
Matteo Mirk
03/02/2021, 10:52 AM
Try to avoid nullable types if you can, and use null only if it makes sense in the domain youโre representing. For example, is 0 or โโ and acceptable value for your variable? Does it represent a meaningful case in your domain? If yes, you can avoid nulls, otherwise use them along with null-safe operators.
Also when dealing with collections always prefer an empty one instead of a null.
c
Colton Idle
03/03/2021, 4:07 AM
You might be interested in reading about "The billion dollar mistake"
I call it my billion-dollar mistake. It was the invention of the null reference in 1965.
Recently when kotlin came out there was a few good articles basically saying "the billion dollar mistake wasn't the invention of the null reference, but the real mistake was omitting it from the type system"
With kotlin, we can represent nullability in the type system which is great because our compiler can be our friend and find mistakes for us.
Modeling your data correctly can be hard. Sometimes there are good reasons something could legitimately be null. Either way... I think modeling nullability is an art not a science.
๐ 1
t
therealbluepandabear
03/03/2021, 5:12 AM
@Colton Idle Interesting perspective ๐
m
Matteo Mirk
03/03/2021, 9:16 AM
In Kotlin I try to avoid nulls as much as I can, despite its facilities, segregating null-handling only to edge points where Integrating with Java code if needed