Not null arguments in Java private methods

After the article on not null arguments on public methods, let’s talk about class private methods.

Checking the public method arguments is necessary to guarantee that methods callers are respecting the class API contract.

On the contrary, private methods are not visible to outside code so there is no need the detect contracts violation.

So, can we skip to check nullity of our private methods arguments ?

The answer is no.

Private methods are called by other methods from the same class so we (not class users) are responsible to guarantee that the methods are invoked correctly.

If we pass a null argument to a private method which need a non null argument, this is a bug in our code and we should fix it.

Joshua Bloch, in his famous Effective Java book, suggest to check private methods arguments using assertion.

This is correct approach because assertion are usually enabled during development and test, while we are debugging our code, and disabled in production code.

Let me make small example:

private void metSimple(final String arg) {
    System.out.println(arg.length());
}

The String argument arg of this method cannot be null. Best approach is to check the argument at the begin of the method body:

private void metChecked(final String arg) {
    assert arg != null;
    System.out.println(arg.length());
}

During the devolpment of the class (with assertions enabled) which uses the method, the assert will check nullity of the argument. Assertion error will help us to debug and improve our code.

To have better support from the IDE, we can also use Nonnull annotation (see this article for more details) like as follows:

private void metAnnotatedChecked(@javax.annotation.Nonnull final String arg) {
    assert arg != null;
    System.out.println(arg.length());
}

This is the preferred solution: assert check run at execution time while Nonnull annotation guides the IDE to detect improper method calls.

Using NetBeans 7.3 we have following situation:
NetBeans 7.3

NetBeans detects wrong method calls at lines 14 and 15 thanks to annotations while it does not find problems on lines 12 and 13.

NetBeans is also reporting an “Unnecessary test for null – the expression is never null”) on the assert line.This is a false message because the annotation cannot guarantee that arg method is never null; I have opened a bug report.

Update: this has been fixed on NetBeans 7.4

Using IntelliJ IDEA 12.1 Community Edition we have the following:

IntelliJ

Curiously the two IDEs have the same behavior:

  • same correct warnings (lines 14 and 15)
  • same missed warnings (lines 12 and 13)
  • same “bug” on line 32

Most probably there is some code sharing between the tools.

Eclipse users: please look at the comments on the my article on public methods.

Advertisements

One thought on “Not null arguments in Java private methods

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s