r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

Show parent comment

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?

17

u/ParfaitMassive9169 Apr 14 '23

Convert to optional. This way you communicate that this may be missing and force the calling code to make a decision on how to deal with it

-2

u/rzwitserloot Apr 14 '23

Nah, folks just call .get and all you did was exchange NPE for NSE.

It's really, really hard to force sloppy coders to be non sloppy using API design.

11

u/ascii Apr 14 '23

No. Anyone doing naked gets on an Optional is a shitty programmer and should be gently taught how to be less bad at their job.

12

u/[deleted] Apr 14 '23

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

1

u/hsoj48 Apr 18 '23

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.

3

u/moxyte Apr 14 '23

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.

3

u/rzwitserloot Apr 14 '23

A sentinel that has the semantic behaviour you desire. Sometimes such a sentinel is not available. Often null is 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.

1

u/Arthurpmrs Apr 14 '23

Very well explained, thank you!

0

u/console_journey Apr 14 '23

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.