0

What's better to prevent the developer that my private method requires a notNull argument (Note that I am not using Spring framework)?

  1. Use the @NotNull annotation

    private void myBusinessMethod(@NotNull Object argument){
        //...
    }
    

    Which library I have to choose ? javax.validation.constraints.NotNull ?

  2. Use the assertion

    private void myBusinessMethod(Object argument){
        assert argument != null ;
        //...
    }
    
  3. throw an IllegalArgumentException

    private void myBusinessMethod(Object argument){
        if (argument == null){
            throw new IllegalArgumentException ("Argument can't be null") ? 
        }
        //...
    }
    

In the following link is recommended to use the assertions: http://www.javapractices.com/topic/TopicAction.do?Id=5

What do you think ?

Thank you

Aguid
  • 943
  • 2
  • 10
  • 24
  • 5
    Matter of opinion. But I would go with the `IllegalArgumentException` **and** documentation pointing out that the parameter is expected to not be null. – Federico klez Culloca Mar 26 '18 at 08:56
  • 1
    There are other options like Lombok. I personally prefer checking arguments and throwing exceptions, for instance with `Objects.requireNotNull(argument, "Argument must not be null.")`. – lexicore Mar 26 '18 at 09:00
  • 1
    See also: https://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use – lexicore Mar 26 '18 at 09:00
  • terrible question for points to be honest, a quick google search and you can find your answer. Would never up vote this in a million years.. – Cowboy Farnz Mar 26 '18 at 09:03
  • @lexicore Objects.requireNotNull(argument, "Argument must not be null.") is similar to 3 option. it will throw NullPointerException in the running environment. I don't prefer this because it add an unnecessary check to my code. Normally all the checks done once in the non private methods. – Aguid Mar 26 '18 at 09:04
  • 1
    @CowboyFarnz "a quick google search and you can find your answer" - why don't you educate us then if it's so easy? The question is not trivial at all. – lexicore Mar 26 '18 at 09:06
  • 1
    @lexicore [when to use an assertion vs exception](https://stackoverflow.com/questions/1957645/when-to-use-an-assertion-and-when-to-use-an-exception), there you go, viewed over 38,000 times and using annotation as NOTED, is a matter of preference. – Cowboy Farnz Mar 26 '18 at 09:08
  • @CowboyFarnz for sure I googled before posting my question, but I didn't find an affirmative response. The best thing I found is that : Objects.requireNotNull(argument, "Argument must not be null.") But I stay preferring use NotNull annotation because it is dedicated for validation simple and elegant . – Aguid Mar 26 '18 at 09:09
  • @Aguid Correct, it is like the 3rd option. A pretty valid way to protect your code from being called with wrong parameters. Isn't it your objective? – lexicore Mar 26 '18 at 09:09
  • 1
    @CowboyFarnz Thank you. This should have been your comment from the very start. – lexicore Mar 26 '18 at 09:10
  • @lexicore Exactly. My goal is to prevent the user to quickly know which valid argument can use in his call of the private method. – Aguid Mar 26 '18 at 09:12

1 Answers1

1

@NotNull is just an annotation and does nothing by itself. You can make use of Hibernate Validator library that provides and implementation of annotation processor to enforce the meaning of @NotNull. But more often than not, it suffices to have this annotation (for documentation) in combination with thorws IllegalArgumentException. In addition, the resource pointed out by @lexicore (in the comments) should help.

Prashant
  • 4,775
  • 3
  • 28
  • 47