I’m working on a new codebase and I’m wondering, since all non-optional parameters in my code throw an IllegalArgumentException when passed null, does it even make sense to use annotations like @NonNull? Or is it better to expect programmers know I always use Optional to indicate not passing in any value is a valid action here? (Of course assuming I also say that in javadoc, etc.)
-
1@NonNull can be used by your IDE, your exception not.user180100– user1801002017-09-07 17:08:43 +00:00Commented Sep 7, 2017 at 17:08
-
4Many think that Optional should not be used for parameters.Andy Turner– Andy Turner2017-09-07 17:08:46 +00:00Commented Sep 7, 2017 at 17:08
Add a comment
|
2 Answers
You can pass a null to a parameter that expects an Optional so you could argue that @NonNull is still useful.
Personally I think @NonNull becomes redudant because if you call a method that expects an Optional you would pass Optional.empty() and never null when the value is absent. Keep in mind that java conventions discorages the use of Optional in parameters.
I disagree with this convention as Optional already conveys the meaning that the underlying method is prepared to deal with the absence of a value.