Java: What is the best approach to validate the method arguments for null

333 Views Asked by At

What is the best approach to validate the arguments for null while calling a method?

1) before making the method call

private void myMethod(String param1, String param2){
   String a = param2;
   if(StringUtils.isNotBlank(a)){   
      validate(a);
   }
}

private void validate(String a) {
    int temp = Integer.parseInt(a);
    if(a > 4){
          addError()
    }
}

2) inside the method once you receive the arguments

private void myMethod(String param1, String param2){
   String a = param2;
   validate(a);       
}

private void validate(String a) {
     if(StringUtils.isNotBlank(a)){
         int temp = Integer.parseInt(a);
         if(a > 4){
            addError()
         }
     }
}

OR Both

6

There are 6 best solutions below

0
On

IMO, StringUtils.isNotBlank(a) is some form of validation only, so it should be in validate() method. And if that has to be checked every time you call validate() method, then certainly it will make more sense to move it there.

0
On

If that error validation is useful everytime you call some method, it obviously should be done inside the method, or else you would have to do it everytime before calling that method. Just remember to comment the code, so you remember that later.

0
On

From the two approaches you give, the second is better.

Reason: DRY principle (Don't Repeat Yourself aka avoid code duplication).

The code in the first snipped duplicates (or triplicates, etc) the "check for blank" part of the code for every place validate() is called.

Also the responsibility of the consistency, the "not blank" invariant (a domain/business rule), belongs (is inherent) to the validate() method, so it should have the responsibility for taking care of it.

In practical terms: Consider the possibility of a change to this domain rule, if one needed to add some new check to it, in what case you'd have to change less (thus less error-prone)? Where the developer that'll do the change would expect to find such rule? In the validate() or scattered through the whole code base?

1
On

Would be inside the method throwing a java.lang.IllegalArgumentException. Like:

private void validate(String a) {
 if(StringUtils.isNotBlank(a)){
     int temp = Integer.parseInt(a);
     if(a > 4){
        addError()
     }
 }
else{
    throw new IllegalArgumentException("argument must not be null or blank");
}

}

0
On

Conceptually the validate() method should capable to identify that the passing parameter is null or not. So in your given scenario the approach 2 should be preferable.

0
On

What is the best approach to validate the arguments

IMO, your second approach is better one because it is not a task for which you have to provide a separate method, it is just a parameter validation which is part of same logic which you are going to implement in your method. and another thing to be considered is second approach will form higher redability of your code.

OR Both

NEVER, Choose one as per your coding practice and stick to it.

Other considerations for validation :

  1. Check parameters annotated with @Nonnull for null?
  2. You can use assertion for Private arguments.