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
IMO,
StringUtils.isNotBlank(a)
is some form of validation only, so it should be invalidate()
method. And if that has to be checked every time you callvalidate()
method, then certainly it will make more sense to move it there.