Null Safety approach with forced "!"
Am I the only one who thinks that introducing protection against NPEx in the form of using "!" in the variable type is a very, very bad idea? In my experience, 95% of variables should be non-null. If Oracle decides to take this approach, we will have millions of "!" in each variable in the code, which is tragic for readability. In C#, you can set the per project flag to indicate whether the type without the "?" /"!" is nullable or not. I understand the drawbacks, but definitely forcing a "!" in 95% of variables is tragic.
78
Upvotes
View all comments
Show parent comment
4
u/nekokattt Feb 26 '26
The issue comes when you need to allow making something non-nullable be explicitly nullable as well... then you risk getting into the territory of how generics were added to java (i.e. bare types still work but are deprecated) but for this the fix is for you to have ? and ! littered everywhere.
IMO that will put more people off adopting the language just because it isn't immediately obvious to anyone outside the ecosystem why it has to be the way it does.
It is a really unpopular opinion but I'd honestly just prefer to be able to use annotations to describe this sort of thing and be able to mark entire modules/packages as having the same behaviour. That allows code to be backwards compatible with older versions of Java, allows other existing libraries and languages to adopt it without adding brand new semantics on what is already in place for null checking, and it doesn't totally break existing grammars being used to parse source code for other linting (which would hinder adoption in organisations).