0

The effective java makes it clear that an assert be used to verify parameters of a private function. If a method is a public method then the method should throw a NPE if a null is an invalid parameter.

Eg:

public void foo (Sting str) {
   char[] ch = str.toCharArray();
}

In the code above, we dont need an explicit check to ensure str is null, or not as null.toCharArray wil l throw the NPE

But what if the code changes to something like this:

   public void foo (String str) {
      List<String> strList = new ArrayList<String>();
      bar (str, strList);
   }

   private void bar(String str, strList) {
        assert strList != null;
        char[] ch = str.toCharArray();
        ... ... .. 
   }

In such a code it falls on private function bar to throw a NPE. According to effective java a private code should only verify via an assert. Will the following code be considered a better practice than code above ?

 public void foo (String str) {
          if (str == null)  throw NPE;
          List<String> strList = new ArrayList<String>();
          bar (str, strList);
       }

       private void bar(String str, strList) {
            assert str != null;
            assert strList != null;
            char[] ch = str.toCharArray();
            ... ... .. 
       }
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
  • 1
    I have never seen `assert` in production code. – Sotirios Delimanolis Nov 29 '13 at 03:44
  • @SotiriosDelimanolis do u suggest NPE be thrown by private function ? – JavaDeveloper Nov 29 '13 at 03:51
  • Do the validation in the calling code. – Sotirios Delimanolis Nov 29 '13 at 03:54
  • The general rule is that, for an public interface, the NPE will inevitably be thrown (if the parm is indeed null). It does not matter whether it's thrown by the outermost level method or something a layer or two down, so long as the lower method understands it's "duty" to raise the exception. And there is certainly nothing that says a private method *can't* raise an exception (especially given that most NPEs will be raised implicitly). (All that said, it's best to raise do all validation at the outermost level, if only to assure that it's done correctly and consistently.) – Hot Licks Nov 29 '13 at 03:54
  • @JavaDeveloper I don't think that throwing a NullPointerException explicitly is a good idea or a recommended practice. Your code should have checks in place to avoid this scenario altogether. – IgorGanapolsky Feb 24 '14 at 17:25

1 Answers1

2

In your public method foo, you have the following

if (str == null) throw new NullPointerException;

This is often called guard clauses and is generally more readable than just letting a null pointer exception be thrown by not including anything.

Regarding assert, it generally as @Sotirios mentions is not used in production code. Check out this answer for additional information: Java assertions underused.

There is definitely some subjectivity here which is better, but from a readability viewpoint, the guard clause in the public method appears to more readable. The assert provides no additional value especially if that private method is only called from that one public method. The assert would never trigger.

Community
  • 1
  • 1
tjg184
  • 4,508
  • 1
  • 27
  • 54