logoalt Hacker News

gentlewateryesterday at 2:24 PM9 repliesview on HN

It’s still missing null safety, right? Which means it’s still a hard no for me.


Replies

msgilliganyesterday at 3:13 PM

The ecosystem has (at long last) standardized on JSpecify (https://jspecify.dev) for nullability annotations. JSpecify allows you to annotate a package or module with `@NullMarked` and your IDE and build (via ErrorProne+NullAway, typically) will check for null safety.

If you develop a library in Java and use it from Kotlin, the built-in Kotlin null-safety will recognize the JSpecify annotations on the library.

Null-restricted types are on the roadmap. See: https://openjdk.org/jeps/8303099

munksbeeryesterday at 5:37 PM

We have a java monorepo of relatively large size and sophistication, driving our entire fintech, and I haven't seen a NPE for years.

Use NullAway and it basically makes the problem go away. Our application won't build if it detects a potential NPE.

show 1 reply
winridyesterday at 2:45 PM

I have a couple 50k+ loc java projects written entirely by LLMs at this point that have never thrown an NPE.

show 1 reply
LelouBilyesterday at 2:27 PM

You can either use Kotlin then, or simply use java and nullability annotations, they have good support in both IDEs and analysis tools

show 1 reply
marginalia_nuyesterday at 2:25 PM

Have you tried not returning null or constructing incomplete objects?

show 3 replies
ivan_gammelyesterday at 3:15 PM

It is really not a big deal nowadays, the problem of the same scale as having index out of bounds error (no language has good defence against this, yet it is not a catastrophe).

show 1 reply
wavemodeyesterday at 3:13 PM

Nullability annotations + tooling makes this a non-issue in practice.

show 1 reply
PaulHouleyesterday at 4:25 PM

Really?

Unlike C it is trivial to catch a NullPointerException and confine the crash to the unit of work. And unlike C you are not talking about insanely dangerous pointers, you're just talking about an NPE.

I'll admit it's a hassle when something wasn't initialized properly and then you get a null pointer exception at some unrelated code much later. It's not always easy to debug. Catastrophic? No!

There are a lot of third party tools that can check for null safety and a lot of work is being done to make Java's initialization safer but also a little more flexible, there is

https://openjdk.org/jeps/8303099

and there are all sorts of practical answers. Nulls in Java are low on my list of annoyances, way behind front end programmers who pepper my CSS files with "!important" because they don't know about precedence (though maybe they think my .clazz.clazz.clazz selector is brain dead!)

well_ackshuallyyesterday at 2:43 PM

Any serious project will be using NullAway and annotating everything (or, indeed, using Kotlin).

Otherwise, yeah, you're still in for a world of pain.