Earlier quoted context omitted.
Don't people use @NonNull everywhere now? It's been a few years since I've programmed in Java but even then I feel like that was common practice.
Yes, it's always been possible to check for nulls at runtime. Personally I use notNull(..) over @NonNull since it actually fires when you expect it to (as opposed to whether your framework dispatcher interceptor trigger decided to invoke it)
Retrofitting null-safety onto Java at Meta
41–50 of 230 posts
Re: Retrofitting null-safety onto Java at Meta
#42Or use Kotlin, it's near perfectly interoperable with Java and the acclamation period is measured in weeks if you're a Java dev
Re: Retrofitting null-safety onto Java at Meta
#43Just give a real Optional type at a language level. It’s clearly possible in other JVM languages.
Re: Retrofitting null-safety onto Java at Meta
#44It's so sad that Java 8 had the chance to really fix the null problem, but gave us only the half-assed `java.util.Optional `. Rather than implementing optional values at the language level, it's just another class tossed into the JRE. This is perfectly legal code, where the optional wrapper itself is null: Optional getMiddleName() { return null; }
I love this solution. Now you have two kinds of nulls.
Re: Retrofitting null-safety onto Java at Meta
#45At the same time, we have adopted Kotlin as well. Interestingly, most of our developers prefer to stay on Java... not because they love Java, but because of familiarity and that they seem to think it's "good enough".
Anyway, we haven't had a large rate of NPEs for a very long time and when one actually happens in the wild we're extremely surprised!
That said: looking forward to the specification[1] that's being cooked up as Java absolutely should have had this a long time ago (as mentioned in the post).
Re: Retrofitting null-safety onto Java at Meta
#46It's so sad that Java 8 had the chance to really fix the null problem, but gave us only the half-assed `java.util.Optional `. Rather than implementing optional values at the language level, it's just another class tossed into the JRE. This is perfectly legal code, where the optional wrapper itself is null: Optional getMiddleName() { return null; }
What do you mean by had a chance? That chance isn't gone. This area is under investigation, and when we have a solution we like, we'll implement it. We can't address all issues at once. Optional isn't half-assed because it was never envisioned as a general solution to the null problem. It's simply an interface that's useful in streams and other similar cases for method return values, and it does the job it was intend…
Re: Retrofitting null-safety onto Java at Meta
#47Or use Kotlin, it's near perfectly interoperable with Java and the acclamation period is measured in weeks if you're a Java dev
Re: Retrofitting null-safety onto Java at Meta
#48Related work: https://devblogs.microsoft.com/dotnet/nullable-reference-typ... C# has made it possible to gradually roll out stricter nullability checking as well. The static analysis gets integrated into the regular language analyzers. Incremental migration is the only way to go.
Re: Retrofitting null-safety onto Java at Meta
#49This is just covering up design problems. NPEs show you were you have design deficiencies. If you have getAccount().getContact().getPhoneNumber() and contact is null, you'll get an NPE. The question shouldn't be: "How do I shove the NPE under the rug for the next 1337 coder to deal with?", the question should be: "How did I initialize an Account without a Contact?"
Do you not know why Kotlin exists or why dart added null safety or why C# added null safety? Or were they also wrong in their choices?
Re: Retrofitting null-safety onto Java at Meta
#50OK, then i realize GoF design patterns lack the single most important pattern: Option data structure.
Nobody ever claimed that GoF is a complete set of patterns. It is just a subset of patterns from infinite variety of possible solutions to problems.