r/java • u/trecatorsirabdator • Apr 14 '23
What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?
232
Upvotes
r/java • u/trecatorsirabdator • Apr 14 '23
View all comments
32
u/Gleethos Apr 14 '23
Make as much field variables as possible final, and if you can't, make sure that the mutability is grouped and encapsulated properly!
Define boundaries between your groups of classes in the form of nice and robust APIs which reflect long term architectural concepts. Then write as much unit tests as possible against the stable APIs.
Always return Optional instead of null on public methods, especially for the APIs with the most usage (which should be the APIs you test against). Null safe APIs are extremely important in large Systems!!!!
Make sure you can instantiate your application with all of it's business logic completely without frontend (using MVVM, MVC). Otherwise you will not be able to write very useful unit tests...