Also I have this question which I hope you could a...
# getting-started
t
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
Just as needed
m
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
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
@Colton Idle Interesting perspective 🙂
m
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