Back to all reviewers

Use null validation utilities

netty/netty
Based on 6 comments
Java

Consistently use utility methods like `ObjectUtil.checkNotNull()` or `Objects.requireNonNull()` to validate that parameters are not null. When assigning parameters to instance variables, combine the null check with the assignment for cleaner, more maintainable code:

Null Handling Java

Reviewer Prompt

Consistently use utility methods like ObjectUtil.checkNotNull() or Objects.requireNonNull() to validate that parameters are not null. When assigning parameters to instance variables, combine the null check with the assignment for cleaner, more maintainable code:

// Instead of:
public DohRecordEncoder(InetSocketAddress dohServer, boolean useHttpPost, String uri) {
    if (dohServer == null) {
        throw new NullPointerException("dohServer");
    }
    this.dohServer = dohServer;
    this.useHttpPost = useHttpPost;
    if (uri == null) {
        throw new NullPointerException("uri");
    }
    this.uri = uri;
}

// Do this:
public DohRecordEncoder(InetSocketAddress dohServer, boolean useHttpPost, String uri) {
    this.dohServer = ObjectUtil.checkNotNull(dohServer, "dohServer");
    this.useHttpPost = useHttpPost;
    this.uri = ObjectUtil.checkNotNull(uri, "uri");
}

This approach not only prevents NullPointerExceptions but also provides clear error messages, making debugging easier. For method parameters that should never be null, perform the check at the beginning of the method. When initializing components in a class, add appropriate null checks to the initialization or configuration methods rather than adding conditional null checks throughout the code.

6
Comments Analyzed
Java
Primary Language
Null Handling
Category

Source Discussions