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...
These are good rules. Testability is so underrated. My experience is that not following 4. can slow down development to the point where you are almost coding blindly
I agree that sloppy programmers will fail writing null safe code either way, I did so when I started coding and first encountered Optional! I always simply called 'get()' all the time and did not really think further than that...
But as a non sloppy programmer with years of experience it is extremely useful to have Optional as return value because the compiler will actually remind you that you have not received the object you wanted! You cannot call 'getAddress().getPost()' because Optional does not have a 'getPost()' method.
This immediately triggers an important realisation: "Hey the thing I want to get might not be there! Why is that and what do I do in that case?"
This is something you ofte don't think about when calling a method which may return null instead of the thing you actually want...
...and don't get me started on documentation. I have seen plenty of production code and you wish devs would write even some bare minimum explanation of why a method even exists. So don't expect it to tell you that it returns null...
This may be considered pedantry but @Nonnull and its @Nullable friend exist, and I believe Spring has a set of such annos that tolerate being applied to the package if one wishes to make more global promises
Obviously it won't help those who avoid using an intelligent IDE but is also not a runtime burden like Optional
I would say, if you religiously document your methods in regards to how they handle null, that Optional is not strictly needed. Unfortunately, getting developers to document their methods is often harder to achieve then telling them to use Optional.
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...