jeudi 29 août 2019

In If else branches, what is the preferred approach?

I am writing a validation function, which checks a multitude of conditions and returns Success if none of the checks fail. I want to know, what would be the preferred way among two choices and why ?

private ResponseObj validationFunc1(ObjA objA, ObjB objB, ObjC objC){
ResponseObj responseObj = new ResponseObj();
if(condA){
   responseObj.setStatus("FAILURE");
   responseObj.setMessage("condA message");
   return responseObj;
} else if(condB){
   responseObj.setStatus("FAILURE");
   responseObj.setMessage("condB message");
   return responseObj;
} ....
...
}else if(condZ){
   responseObj.setStatus("FAILURE");
   responseObj.setMessage("condZ message");
   return responseObj;
}
responseObj.setStatus("SUCCESS");
responseObj.setMessage("Valid");
return responseObj;
}


private ResponseObj validationFunc2(ObjA objA, ObjB objB, ObjC objC){
if(condA){
   return new ResponseObj("FAILURE","condA message");
} else if(condB){
   return new ResponseObj("FAILURE","condB message");
} ....
...
}else if(condZ){
   return new ResponseObj("FAILURE","condZ message");
}
return new ResponseObj("SUCCESS","Valid");
}

Which of the above 2 functions would be preferred in production code ? If so, does one method has a performance gain over another ? Or, Am I just mistaken and the compiled code for both the functions will be same ?

Thanks in advance for your answers. If I have asked the same question again, I am very sorry. I did try my best to search for this.

Aucun commentaire:

Enregistrer un commentaire