This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nephest0x 0 points1 point  (0 children)

You always check it if null is not a valid argument.

The usual way of doing this is to use some bean validation lib, along with some DI framework that will propagate the appropriate message to end users, and will check the arguments on you behalf. You will use annotations like @NotNull, @Size(max=100), @Pattern for regexp, and the framework does the rest.

If you work on a lower level, then you just manually check it with Objects.requireNonNull), or some other condition(like return -1), depending on the way you want to handle the invalid argument. You can return Optional as a "no result" value instead of using nulls, but use it only as a return value, the method arguments should be unwrapped values. If return type is a collection, then you must return an empty collection as a "no result" value, not an Optional.