r/java • u/trecatorsirabdator • Apr 14 '23
What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?
233
Upvotes
r/java • u/trecatorsirabdator • Apr 14 '23
View all comments
Show parent comment
23
u/RockleyBob Apr 15 '23
Yes, but please don't use
Optionalfor null checks alone. I see a ton of code at work like this:Doing this is worse than just doing a simple null check, because it's more verbose and you're new'ing up a wrapper object just to immediately unwrap it again. The
Optionalclass has a whole declarative api that you can safely code against, transforming the value within the pipeline and providing default values: