HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Is checking for null redundant while using Contract Annotator from JetBrains?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
contractwhileannotatornullcheckingredundantjetbrainsforusingfrom

Problem

I'm curious whether checking for null is redundant in this concrete example.

public LoginJob(@NotNull String login, @NotNull String password) {
        super(new Params(Priority.NORMAL).requireNetwork().persist());
        Preconditions.checkNotNull(login, "login is null or empty");
        Preconditions.checkNotNull(password, "password is null or empty");

        this.login = login;
        this.password = password;
    }


Please notice I'm using Contract Annotator from JetBrains, so the User of a Class warned the LoginJob constructor can't take null values, it can't prevent him/her of doing so though.

Solution

You said it yourself in your question :


it can't prevent him/her of doing so though.

That settle it for me. You still have the responsibility to prevent that null value are pass to your classes. The annotation seems to be about preventing NullPointerException. So you should be checking for null values even if you use @NotNull.

Preconditions.checkNotNull(login, "login is null or empty");
    Preconditions.checkNotNull(password, "password is null or empty");


Your message or the method you use is lying. The method is called checkNotNull but you warn about empty String, this is two very different things. Either you should modify you message to inform that you're only checking null values or you could change/rename the method checkNotNull to better represent the empty check.

Code Snippets

Preconditions.checkNotNull(login, "login is null or empty");
    Preconditions.checkNotNull(password, "password is null or empty");

Context

StackExchange Code Review Q#54716, answer score: 5

Revisions (0)

No revisions yet.