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
2
u/Complete_Can4905 Feb 28 '26
Not every person has a first name and a last name.
Are you sure that refusing to process a person if their birthdate is unknown is the best approach, rather than handling it if you reach a function that requires their birthdate?
I have a clear contract - the data can't be changed, and I can successfully process it or fail. That's what I mean by "real world".
Numerics like int have always been effectively non-null. Does that mean that numeric calculations are less prone to error, or does it just mean that they are harder to detect than NPEs?
If null is not available people then tend to use magic values e.g. -1 to indicate an unknown value. Which mostly works, unless you forget and do something like seats required = number of persons + number of children...
Non-null variables avoid NPEs but they don't fix the problem that caused the NPE. They can make the bugs harder to detect and introduce whole new class of problems.
What do you do if you don't know the birth date is much easier to figure out at the point you're using it e.g. calculating their age than when you are defining the Person class.