0

Hi guys I was doing a little bit of reading and playing around with the kotlin language. I still can't figure out why would anybody use null checks over safe calls?

Safe calls would just return a null value if the value is null, this won't crash your app. Null checks on the other hand would raise a nullexceptionerror if the value is null. I have tried searching for the reason to use one over the other, but I can't find any resource about that.

Is there a reason to use safe calls over null checks and vice-versa?

Sergio
  • 27,326
  • 8
  • 128
  • 149
  • Have you read the section on Null safety in the Kotlin docs? See: https://kotlinlang.org/docs/null-safety.html#checking-for-null-in-conditions – lukas.j May 25 '22 at 16:09
  • 2
    What if you are sure the value is not null and would like to treat e.g. `String?` as just `String`? `!!` is essentially a cast operator and has similar uses as "normal" casting. Sometimes we know something about the object and this is not covered by the type system, so we need to cast. In practice, I rarely use `!!`. – broot May 25 '22 at 16:22
  • 1
    Sometimes you know for a fact something is not null and you don’t want to have to write the code to handle what to do if it’s not null. I rarely use it except when retrieving items from a Map that I know are present. – Tenfour04 May 25 '22 at 16:27
  • @Tenfour04 that use case is technically covered by `Map.getValue(K): V`. Although it's longer, it fails with a nicer error message if I recall correctly. – Joffrey May 25 '22 at 16:37
  • 1
    Crashing is not always a bad thing. If the invariants of your program are violated, continuing execution can lead to unexpected behavior and problems that are bigger than a simple crash. If it's "impossible" for a variable to be null, you can use `!!`, and then _even if you are wrong and it is null_ you will at least get a crash, a stacktrace, (hopefully) an error report... and then you can investigate the problem and find the _root_ cause (the thing that let an illegal null value into this part of your program). If you used `?` instead, the problem will exist silently forever. – Ben P. May 25 '22 at 18:31
  • 1
    `!!` isn't a **null check** - it's a **non-null assertion**. It's the opposite of a check, it's telling the compiler to look the other way! I'm pointing it out because so many people get the idea `!!` is a way to handle potential nulls, and it isn't. Always use safe calls, and the compiler will make sure you're taking care of those possible nulls. If you ever run into a situation where you **know** the compiler is *wrong* and a thing *cannot be null*, where you have the expertise and knowledge to say that for sure, then `!!` is a tool you can use. You'll probably regret using it at least once! – cactustictacs May 25 '22 at 19:13
  • “…the problem will exist silently forever.”  And that's worse than it might seem: it could be silently corrupting your data, or causing other subtle problems that you only find out about months later, when it's too late. Or it could be causing errors/crashes in an apparently unrelated part of the program, making it almost impossible to track down. So yes, spotting problems/violations ASAP is good — though `!!` isn't _always_ the best way to do that. – gidds May 25 '22 at 19:34

2 Answers2

3

Not-null assertion operator(!!) is used when you are completely certain that a variable is not null. It can be useful for example to avoid additional checks with if statement or let() function. If you have at least a little doubt use safe calls(.?).

Not-null assertion operator(!!) also used for interoperability with the Java language. For example if some Java library returns an object, Kotlin considers it as nullable, but if you are certain and library docs says that the object can't be null, so you can use Not-null assertion operator(!!).

Sergio
  • 27,326
  • 8
  • 128
  • 149
0

Suppose you are fetching some data with api call of Movie data (movie_name, language, length, subtitles), and you are showing that info in list, if variable life subtitles is not present or having null value, you can use movie?.subtitles to variable if its null or not before you assign it to any text view,

For !! Operator, Suppose you define one Multithreading task where one variable like reader is running, if that is null at runtime then you can throw runtime exception

Vishwajeet Barve
  • 547
  • 3
  • 10