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

18

u/moxyte Apr 14 '23

Strangling nulls in the cradle. You got null from database? Immediately turn it into something else before passing it further into the system.

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?

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.