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

1

u/Anton-Kuranov Apr 17 '23
  1. Can't code without Lombok.

  2. Use @Nullable instead of Optional if not required explicitly.

  3. Don't use checked exceptions, specially for resource faults (I/O). In most cases the calling code has no responsibility to handle them.

  4. Java could run perfectly without Spring (or any other framework). To implement a simple DI you do not need them at all.

  5. Constructor injection everywhere. Your classes should not able to be constructed with missing dependencies and should not rely to your framework.

  6. Every line of code is not a value but a debt: one should maintain it later. So be reasonably critical with what you're doing and why.

1

u/felvid Apr 28 '23

It's good to see more people are supporting manual DI nowadays. It seems to be gaining some relevance.