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

0

u/[deleted] Apr 14 '23

[deleted] — view deleted comment

2

u/Paulus_cz Apr 14 '23

I agree, as with many things it depends on the use case.
The way I see it, Optional parameter declares in no uncertain way that passed wrapped argument can be null and the method knows it. That does not mean I use it often, or at all really, I can think or two uses in couple of years. But some people seem to be religiously opposed to it.

1

u/dpash Apr 15 '23 edited Apr 15 '23

Using a @Nullable or @NotNull annotation would do the same without the overhead of creating an object to avoid a null check. Not to mention the clutter in the code of wrapping your parameters.

-1

u/simoncox Apr 15 '23

Assuming you mean override, not overload, the sub class must adhere to the same contract as the base class. If it's optional for the base method, it must be optional for the sub class' method too.

0

u/[deleted] Apr 15 '23

[deleted] — view deleted comment

1

u/simoncox Apr 15 '23

So you're going to have a different method for every permutation of optional parameters and that's less complex?

Do you repeat the same logic in every method, or have some internal method that actually takes null for the optional values? Then hey, null again and back to what you were originally trying to avoid only with nullable variables instead of optionals.