Why do people keep saying "kotlin is null safe, oh my god it's so safe, you don't have to worry about null's...". when all you have to do is add a question mark and BAM null-city!

  • 2
    Sounds simar to the new null annotation for C#8.

    But doing this will give you compiler warnings/errors when passing a possible null value, right?
  • 6
    Because it's null-safe by default, and the ? operator simply turns off this null safety to allow you to interface with legacy / non-type safe code. That doesn't make it "null-unsafe", in the same way casting doesn't make a language "type-unsafe".

    If adding this operator isn't something you should allow in your codebase, you can easily create a linter rule and/or pick this up in code review.
  • 12
    "I have a lock in my door, but if I leave it unlocked everyone can enter, therefore this lock is not safe."
  • 2
    Because when you put that question mark you are explicitly saying it can be null, it gives you ways to deal with it and the compiler will prevent stuoid mistakes. It works more like javas Optional<T>.
    Its null safe, not null free
  • 0
    Also people don't keep saying this, it's been like a year since i heard someone use this as an argument for kotlin. (i mean you can use @nullable and @nonnull with java and a good linter).
  • 0
    I can see Kotlin was trying to do something to avoid all those null checks and NPEs but to me it just appears all they have done is introduce a lot of fluff for dealing with nulls. When I see people defend it, perfect example is @gronostaj lock analogy, it's not really a good defence (I honestly mean no offence). If you're designing a safe lock which won't let people in then don't add potential design flaws! Whether or not you use the '?' your design still has to take into consideration when a reference may be null.
  • 1
    But Kotlin _does_ force you to handle nulls explicitly (with the exception of interaction with Java code). If a value is nullable (uses ?), you have to handle the null case every time you're using it where a non-nullable counterpart is expected. This completely solves the NPE problem known from Java which was caused by types implicitly accepting nulls which could not be handled properly.

    It's actually documented pretty well here: https://kotlinlang.org/docs/...

    There are only 4 cases where NPEs are possible in Kotlin:

    - throwing them yourself (obviously)

    - interacting with Java (unavoidable, since you don't know if value passed to Java is supposed to be nullable; also all values coming from Java are potentially nullable but maybe not)

    - explicitly saying "screw you Kotlin, I want to be null-unsafe here and if something goes wrong it's not your fault"

    - the only _actual_ reason for NPEs in pure Kotlin: messed up object initialization
  • 0
    if (nfcAdapter == null || !nfcAdapter?.isEnabled!!) {

    soooooooooooooooooooo ugly :(
  • 0
    My Kotlin skills are rusty, but IIRC after the null check the compiler treats the value as non-nullable.
  • 0
    Only if it's immutable
  • 0
    I'd just like to add, I'm loving some of the other syntactic sugar. Shame java not catching on quick enough.
  • 1
    @TedTrippin bit late, but !(nfcAdapter?.isEnabled ?: false)
  • 0
    @gitreflog thats nicer, thx :)
Add Comment