Not null arguments in Java private methods

Gualtiero Testa

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…

View original post 264 more words