Consider the two following coding-styles:
Nested conditions:
if(is_numeric($user_id)) {
// .. do stuff
if(is_valid_user($user_id)) {
return foo($user_id);
} else {
return FALSE;
}
} else {
return FALSE;
}
vs. just simply stopping when something is wrong:
if(!is_numeric($user_id)) {
return FALSE;
}
// .. do stuff
if(!is_valid_user($user_id)) {
return FALSE;
}
return foo($user_id);
This is of course at least partially about taste; but what are these two different styles called?
When are one prefered over the other?
Are there other, perhaps cleaner, coding styles?
I generally go by the idea that the least nesting the easier something is to read. I prefer the second style for that reason. Of course it doesn’t matter what style you use, I would even change your second example slightly to make things even easier for me to read.
To me having the return statements on the right makes them stand out. Also, having the whole thing on one line helps me picture the statements being gates and easily spilts the code into sections … but that is just me.