Colton Idle

11/27/2019, 4:54 AM
Kinda feel silly but I can't wrap my head around people using different functions to essentially just do a null check. I still find
if (myItem != null) { //do something }
makes sense in kotlin as it does in java. Is using
really needed? Would someone say that my if statement is not idiomatic? I like to follow the 90 day rule. If I come back to the code in 90 days... which one will I understand? I feel that most would choose the if.
👍 2


11/27/2019, 5:01 AM
I wondered this too. There is a discussion in the forums that might be interesting to you (it was for me): As usual it seems to come down to preference. In that post there are many approaches including someones custom rolled helper

Matteo Mirk

11/27/2019, 11:57 AM
After some time spent looking at other people’s kotlin code I can tell there’s a tendency to overuse block functions to try and squeeze null checks and if/else in the same expression. Unfortunately this results in hard to read code, where a simple if would have sufficed. I noticed this trend being similar to the introduction of Optional in Java 8: lots of our production code became unreadable because they had to forcefully use Optional everytime! gosh… 🤦‍♂️

Colton Idle

11/27/2019, 8:21 PM
Thanks. I just ran into an issue where I was using
instead of
and got completely different behavior. Had no idea. 😄 Still taking some time to get used to all of these block functions.


11/28/2019, 7:06 PM
When you have a shared variable, eg a property, and you want to perform a null check, you need a local variable to ensure another thread doesnt change the variable. A ?.let{} is easier than introducing a local variable most of the time. Or when chaining a few calls. Otherwise I prefer simple ifs over the heavy use of those block functions.
👍 1