php - Standards for exiting a function -
possible duplicate:
why should function have 1 exit-point?
as cs student have had beaten head there should 1 exit point in function, @ end.
eg. this:
function foo() { //do stuff here if($bar) { $out = false; } else { $out = true; } return $out; } not:
function foo() { //do stuff here if($bar) { return false; } return true; } however have seen second type of exiting used quite in other peoples code in php, , in core code frameworks (like kohana have been using lately). method of exiting function considered okay in php standards?
edit: can see why have been told not can easier track problems in function 1 exit point, other times can see why should allowed other problems better solved or tracked in functions multiple exit points.
edit 2: added "do stuff here" comments code example make people happy
i've used latter route, since have declare $out , have 1 more variable in existence. in retrospect, it's boolean -- it's not doing harm. first route cleaner, depending on context of code.
it comes down consistency. long have system, determining when time use route 1 or route 2, you're doing great.
Comments
Post a Comment