Null Safety: Kotlin vs. Java
blog.frankel.ch
blog.frankel.ch
Never is a strong statement. The java team spends a good amount of time thinking on how they could bring Nullability into the type system. It’s certainly possible we could get it as apart of Project Valhalla - https://mail.openjdk.org/pipermail/valhalla-spec-experts/202...
Aside from future language changes that could bring direct null safety, there are practical tools like NullAway that essentially give us everything Kotlin has.
Especially considering that the author doesn't explicitly mention Option monad, for-comprehension and all the right generalizations solving the problem properly.
Which is so mature that JetBrains is using Rust instead for their Fleet backend.
Only on Android has Kotlin an advantage, thanks to Google pushing it as a replacement.
That doesn't make a lot of sense.
And I have worked in Kotlin and hate it - and I'm saying this!
But Kotlin has other stability issues. Owing to that, Android Studio is actually the least stable IDE I've ever used, with frequent dead locks. With both threads and coroutines and different forms of synchronization that may or may not work with both it's quite difficult to use correctly I suppose.
You can also just avoid returning nulls and not have this problem at all. I think unless you're dealing with code that was written close to 20 years ago, or was written by someone living under a rock for as long, nulls will rarely be a problem. Putting tons of effort into building strong compiler guarantees that prevents a problem that at best is hypothetical seems like more effort than it is worth.
E.g. "being able to run your service locally and meaningfully" is something that's more important to me than no-nulls.
There are other choices that can be used to the same effect, such as exceptions or sealed classes. Of course, you can still use Optional<T>. But with null safety, a nullable return type is often most elegant and it's idiomatic in Kotlin.
That would actually be idiomatic Java behavior.
But with REST, I think you should return different status codes and different types entirely if you're returning an error. Not try to communicate through the presence of absence of fields. You should definitely not return half-complete representations when something half-worked. Either it's complete and guaranteed to be fully populated, or it's not returned at all.
e.g.
HTTP 200: { "message": "hi mom!" }
HTTP 404: { "error": "you came to the wrong neighborhood!" }