News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Larry Garfield:
On empty return values
March 29, 2013 @ 09:15:59

Larry Garfield has posted some of his thoughts on return values and reminds you about consistent return types, regardless of the result.

Earlier today, I posted a brief tweet (isn't that redundant?) about return values in PHP (or really, any language). Originally it was about return values from functions (such an exciting topic, I know), but it ended up generating a fair bit of lively conversation, as well as a patch against Drupal 8. So lively, in fact, that I think it deserves more than 140 characters.

He proposes a new rule of thumb: "If your function returns a collection, its null value return must also be a collection." A more broad version of this might be: "make your return types consistent." It's all about predictability and the contracts you have between different parts of your code. If a user calls your method expecting to be able to loop over the results, they'll be disappointed with a "false". He talks some about using and throwing exceptions more effectively for error handling and answers several "but wait..." arguments for his return strategy.

0 comments voice your opinion now!
empty return values opinion contract exception expected


blog comments powered by Disqus

Similar Posts

Mike Purcell's Blog: Symfony - PHP - Possibly Forking 1.x so Invested Companies donít Lose Millions

Chris Hartjes' Blog: Fun with API's - FRAPI and django-tastypie

Larry Garfield: On empty return values

Richard Smaizys' Blog: Improve your code style with simple tips

Jacob Mather: How to act like you (maybe actually) care about your work


Community Events





Don't see your event here?
Let us know!


experience community developer opinion configure testing laravel interview list language install refactor podcast framework code unittest release introduction threedevsandamaybe series

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework