If this has been answered elsewhere, I'd be happy just to follow a link.
Is it better programming practice to test the negative case first to fail quickly and keep the body of the function together OR to test the positive case first and have the negative case dangling at the end? Does it make a difference if the if-else statements are heavily nested? Example:
int foo(int x){
int status = 0; //initialize to success
if(x < 0){
std::cout << "Bad parameter\n";
status = -1;
}else{
/* execute the rest of the function */
}
return status;
}
OR
int foo(int x){
int status = 0;
if(x >= 0){
/* execute body of the function */
}else{
std::cout << "Bad parameter\n";
status = -1;
}
return status;
}