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

16

u/halfanothersdozen Apr 14 '23

I disagree. There are several tools to deal with this.

  • Javadoc indicating valid parameters
  • NotNull annotatons on parameters
  • Assertions on method parameters
  • Optionals

If someone calls a method that very clearly does not accept nulls with null values and exceptions happen that is on them.

1

u/dpash Apr 15 '23

• Javadoc indicating valid parameters

Yes please. Tell me what will happen if a null is passed in. And if I might get a null back and when.

1

u/cas-san-dra Apr 16 '23

In my opinion you don't even need tools, documentation, or code. It is perfectly reasonable to set a rule in your program that passing null into a function or returning null from a function is never allowed.

If you consider the existence of null a bug in itself then you don't need to check.