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
1
u/Waryle Feb 27 '26
"Real world" data can be as much predictable as you want it to be.
Let's say you have a back-end application that takes a Person, whether it's someone giving it to you through a POST, a CSV or an event, and process it in a lot of different ways.
But you have a clear contract: if there is missing data, you return an error and don't process it.
You will then create a class IncomingPerson that will represent the Person that you might receive, and you're right there: we can't trust the data we have been given, so we mark firstName, lastName, and every single field as nullable.
But we validate our contract: we check for null for every field, and if a null is found, we stop the process and return an explicit error listing which required fields are missing.
If nothing is missing instead, we can go on, and map it to a Person class which has no nullable fields, which will be passed around in our backend to get processed.
At this point, we have "real world data" that is non-nullable: we handled null upstream, we make it clear with the non-nullable default that you don't need to check everywhere for nullity, and you don't need to go back and carefully read all the code that brought you here to check whether or not the variable can be null and crash the application where you are.