News Feed
Sections




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

Brandon Savage:
Always Return Something
March 12, 2013 @ 10:49:55

In this post to his site Brandon Savage talks about "always returning something" from your methods and functions back to the calling script. He also suggests that null is not an option.

A few weeks ago, there was a discussion on Twitter about whether or not a method should always return a value, or whether or not null was a valid value to return. The answer to this question is a resounding no, a null value should never be returned. [...] For example, you check that a file you opened exists, or that a resource performed correctly before using it. But if you receive a null response, how do you test for this The answer is you can't

He notes that a "null" response is not only difficult to test but can lead to ambiguous handling as you're not sure where the error might be. He also includes a snippet of code showing how a null response could break a fluent interface if an instance of "$this" is not returned.

0 comments voice your opinion now!
return valid null method function value


blog comments powered by Disqus

Similar Posts

PHPMaster.com: Overriding Strategy Logic - The Template Method Pattern

Matthew Weir O'Phinney's Blog: exuberant ctags with PHP in Vim

DevShed: Tracking a Stack of Function Calls with the Xdebug Extension

Community News: Yahoo! Sends Serialized PHP Love

Luc de Brouwer's Blog: Stop waiting, start array dereferencing in PHP now!


Community Events

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


performance configure interview podcast symfony2 series api opinion example install laravel conference application library community php7 framework composer release introduction

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