Let's say I have a date column on my db. Some rows are null. To what should I transform them after querying the data? Like, 0000-00-00?
On the same note, if a method could return null, I must check for null upon function call. But if I don't use null, I may have to use exceptions. Wouldn't I need to catch them as well? Is it better to check for exceptions?
or install a code analysis tool like sonar qube on your ci and make a build fail or not mergable to master if quality gates are not met. this way even the laziest programmer that tries to use an unchechecked .get will have to adapt to code policy or quit
Bad idea. Once corporate finds out they can get numbers out of that thing it's all over for you. Now you need 95% unit test coverage on an app that's 50% configuration.
Like ParfaitMassive said, or more generically you make sure nothing escapes method as null. Especially straightforward if you get a litany of strings you make sure none of the strings ending up in list to be returned are nulls.
A sentinel that has the semantic behaviour you desire. Sometimes such a sentinel is not available. Often nullis right on such a case, but crucially that is because the desired behaviour is an exception.
If some code goes: is date X between A and B? Do you want the code to eval to false (then use 0000-01-01), or throw because the question shouldn't have been asked? Then null.
Sloppy null is where you e.g. have a null stringref and treat it the same as you would an empty String.
If a method could return null then check for that right away, don't let the null linger (not in a local var and definitely not in a field). When writing APIs, try to avoid the need. Example: map.getOrDefault. Or in dB models, don't write:
SELECT birthDate FROM ....
if (bd!= null && bd.isAfter(now minusYears (18)) ...
Instead SELECT the expression directly with DATEDIFF in the SQL.
Maybe a new class representing the null value, like NullDate or EmptyDate? If it extends Date you could use it like a normal Date object. Validation or sorting would also be possible.
5
u/Arthurpmrs Apr 14 '23
Like what? (Genuine question, this aways bugs me)
Let's say I have a date column on my db. Some rows are null. To what should I transform them after querying the data? Like, 0000-00-00?
On the same note, if a method could return null, I must check for null upon function call. But if I don't use null, I may have to use exceptions. Wouldn't I need to catch them as well? Is it better to check for exceptions?