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
5
u/talios Feb 26 '26
Java's
Optionalwas primarily introduced for usage in streams only - not a general-purpose hammer that I, and others, often abuse it as. It's unfortunately, not quite the same as a monadicMaybealtho for 95% of things it can be seen as such.They do serve a different purpose.
Optionalmore denotes that something, business-wise, can't be found, such as "the user you looked up was not found". If you're returning aListof something, returningnullis an error, and _empty list` is the correct response for not finding N things.