Best practice for returning in PHP function/method -
i refactoring extensive codebase overtime. in long run going develop whole system in classes in mean time using opportunity refine php skills , improve of legacy code use across several hundred websites.
i have read conflicting articles on time how best return data custom function, debate falls 2 categories, concerned best technical practice , concerned ease of reading , presentation.
i interesting in opinions (with elaboration) on consider best practice when returning custom php function.
i undecided of following better standard follow using basic theoretical function example;
approach a.
populating return variable , returning @ end of function:
<?php function theoreticalfunction( $var ) { $return = ''; if( $something > $somethingelse ){ $return = true; }else{ $return = false; } return $return; } ?>
approach b.
returning @ each endpoint:
<?php function theoreticalfunction( $var ) { if( $something > $somethingelse ){ return true; }else{ return false; } } ?>
a possible duplicate have been what php best practice using functions return true or false? not limited true or false despite basic example above.
i have looked through psr guidelines didn't see (but may have missed please feel free point me psr reference :) ).
extending original question:
is method used return different depending on expected/desired output type?
does method change depending on use of procedural or object oriented programming methods? question shows, object orientation brings in own eccentricities further extend possible formatting/presentation options best practices returns methods in php
please try clear in explanations, interested in why choose preferred method , what, if anything, made choose on method.
there people arguing single exit points in functions (only 1 return
@ end), , others argue fail/return early. it's matter of opinion , readability/comprehensibility on case-by-case basis. there hardly objective technical answer.
the reality it's not can prescribed dogmatically. algorithms better expressed , others work better b.
in specific case neither "best"; code should written as:
return $something > $somethingelse;
that serve example there's no such thing applicable rule.
Comments
Post a Comment