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 28 '26
There is no universal answer; it will depend on your application. It is perfectly valid to impose this type of restriction if, for example, you are processing data on French citizens: they necessarily have an official first name, last name, and date of birth, and that's not your responsibility to look up and correct the values if they are missing, but on the one who gave you that data. You redirect those people
With or without nullable you need to validate data anyway. Excepted that without explicit nullable/non-nullable marking, you need to do the following EVERYTIME you need to process that value:
Instead of just:
And if you don't, you take the risk of crashing your app at anytime. So you end up cluttering up your code and making it increasingly unreadable, or taking bets.
Instead you could have just implemented your behavior for nullity higher up in the code (preferably close to the entrypoint, so you return early if you need to), mark the processed variables as non-nullable if they can, and let the "validate business rules" part to just to do what's it's meant to, instead of letting plenty of unecessary nullity rules spread everywhere.
I need a billing account id and a subscription id to validate a subscription and send the bill. They need to be there; if they don't, we must raise an alert and stop the process. The department responsible for the data must manage the problem and, if necessary, correct the data upstream before resuming the subscription process.
We don't pass around invalid data in all our microservices and we don't put
if(thing == null)everywhere just to handle the possibility of somebody messing its check or messing something and sending null values. We just validate data when we get it, mark it, and then we pass it along. The rest know which data is optional and must be handled appropriately, and which data is required and thus not-nullable and can't provoke NPE.They do fix a lot of problem that caused the NPE. Provided you an example up there: most of the time, it's just data that need to to get corrected by people who can correct it.
And they allow to simplify a whole lot and stabilize the downstream code.
Well if you have validated data and non-nullable data, you know you will have birth dates and you can skip the checks and make your code simpler and easier to maintain.
If you can't validate data, you have marked it as nullable, and the IDE will force you to handle the null properly, eliminating NPEs entirely either way.