Let’s say for an instance I have this function:
function isValid($premise){
if($premise == 'foo')
{
return true;
}
return false;
}
As you can see the else statement was omitted, else it was immediately followed by the return statement. Why do some people omit them? Is it a bad programming practice?
The same code will be generated with or without the missing
else. It’s a matter of style and preference.Personally I would tend to omit the
elseonly when checking for certain conditions at the start of a long function.I do not consider it bad practice. There are situations where it makes for clearer code.