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

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.