<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Return Values</title>
	<atom:link href="http://daveyshafik.com/archives/479-return-values.html/feed" rel="self" type="application/rss+xml" />
	<link>http://daveyshafik.com/archives/479-return-values.html</link>
	<description>As close to my brain as you can safely get...</description>
	<lastBuildDate>Sat, 13 Feb 2010 05:32:28 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: till</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-35</link>
		<dc:creator>till</dc:creator>
		<pubDate>Wed, 04 Feb 2009 12:06:48 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-35</guid>
		<description>Davey - great idea for a serious, please keep it coming!

Though, if you are trying to establish a best practice guide, then e.g. teach people the fundamentals of return too (e.g. that it allows you to exit early and allows you to avoid if/elseif/else structures), because people will just take your example code, copy it and not apply brain. :-)

By the way, in general I&#039;d also suggest to raise an exception. But people often forget that try/catch can be expensive too. IMHO, it always depends on the type of error. I often dis-regard exceptions if they can happen and the code they come from is not crucial to operating.</description>
		<content:encoded><![CDATA[<p>Davey &#8211; great idea for a serious, please keep it coming!</p>
<p>Though, if you are trying to establish a best practice guide, then e.g. teach people the fundamentals of return too (e.g. that it allows you to exit early and allows you to avoid if/elseif/else structures), because people will just take your example code, copy it and not apply brain. <img src='http://daveyshafik.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>By the way, in general I&#8217;d also suggest to raise an exception. But people often forget that try/catch can be expensive too. IMHO, it always depends on the type of error. I often dis-regard exceptions if they can happen and the code they come from is not crucial to operating.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Return values &#8211; PHP in Action</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-29</link>
		<dc:creator>Return values &#8211; PHP in Action</dc:creator>
		<pubDate>Tue, 03 Feb 2009 22:12:57 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-29</guid>
		<description>[...] Davey Shafik discusses return values from functions. In the specific case of a function that returns values from a database, he wants to return false on error and an empty array if the data set is empty. He also has a reason for that: [...]</description>
		<content:encoded><![CDATA[<p>[...] Davey Shafik discusses return values from functions. In the specific case of a function that returns values from a database, he wants to return false on error and an empty array if the data set is empty. He also has a reason for that: [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-28</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Tue, 03 Feb 2009 17:57:40 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-28</guid>
		<description>Definitely an Exception for an unexpected error...</description>
		<content:encoded><![CDATA[<p>Definitely an Exception for an unexpected error&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OnyxRaven</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-27</link>
		<dc:creator>OnyxRaven</dc:creator>
		<pubDate>Tue, 03 Feb 2009 15:38:32 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-27</guid>
		<description>Best API practice for a database error?  an exception.</description>
		<content:encoded><![CDATA[<p>Best API practice for a database error?  an exception.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ilia Jerebtsov</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-26</link>
		<dc:creator>Ilia Jerebtsov</dc:creator>
		<pubDate>Tue, 03 Feb 2009 15:21:16 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-26</guid>
		<description>This is why we have null and exceptions.

&lt;code&gt;
function getData(){
  if (error()) {
    throw new Exception(&#039;Error message&#039;);
  }
  
  // only if the result is a single item and not a list
  // if it&#039;s a list, myData() should already be an empty array.
  if (noData()) { 
    return null; 
  } 

  return myData();    
}

try {
  if ($data = getData()) {
    foreach ($data as $row) {
      // Show data    
    }
  }
  else {
    echo &quot;No data found.&quot;;
  }
}
catch (Exception $ex) {
  myLog(&quot;Something Bad Happened!&quot;);
}
&lt;/code&gt;

Cleaner, no shoehorning of magic values, and you can actually report more than one error condition.</description>
		<content:encoded><![CDATA[<p>This is why we have null and exceptions.</p>
<p><code><br />
function getData(){<br />
  if (error()) {<br />
    throw new Exception('Error message');<br />
  }</p>
<p>  // only if the result is a single item and not a list<br />
  // if it's a list, myData() should already be an empty array.<br />
  if (noData()) {<br />
    return null;<br />
  } </p>
<p>  return myData();<br />
}</p>
<p>try {<br />
  if ($data = getData()) {<br />
    foreach ($data as $row) {<br />
      // Show data<br />
    }<br />
  }<br />
  else {<br />
    echo "No data found.";<br />
  }<br />
}<br />
catch (Exception $ex) {<br />
  myLog("Something Bad Happened!");<br />
}<br />
</code></p>
<p>Cleaner, no shoehorning of magic values, and you can actually report more than one error condition.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthew Weier O'Phinney</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-25</link>
		<dc:creator>Matthew Weier O'Phinney</dc:creator>
		<pubDate>Tue, 03 Feb 2009 14:57:47 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-25</guid>
		<description>Why an empty array? That makes the assumption that you&#039;re returning an array as the result. I would expect a null value makes semantic sense when you simply get no results -- it&#039;s the absence of a result, basically.</description>
		<content:encoded><![CDATA[<p>Why an empty array? That makes the assumption that you&#8217;re returning an array as the result. I would expect a null value makes semantic sense when you simply get no results &#8212; it&#8217;s the absence of a result, basically.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Savage</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-24</link>
		<dc:creator>Brandon Savage</dc:creator>
		<pubDate>Tue, 03 Feb 2009 14:40:40 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-24</guid>
		<description>Typically I only return false on error conditions as well. One thing that I also try and do is when an error condition is encountered, it raises a warning or a notice (depending on the severity) so I can see when my functions are failing and why.I only do this when something is *not* supposed to fail though. I don&#039;t do this when an error condition might have happened  in the course of running the program (like bad data being entered); only when something should never happen (like the database server failing).</description>
		<content:encoded><![CDATA[<p>Typically I only return false on error conditions as well. One thing that I also try and do is when an error condition is encountered, it raises a warning or a notice (depending on the severity) so I can see when my functions are failing and why.I only do this when something is *not* supposed to fail though. I don&#8217;t do this when an error condition might have happened  in the course of running the program (like bad data being entered); only when something should never happen (like the database server failing).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loïc Hoguin</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-23</link>
		<dc:creator>Loïc Hoguin</dc:creator>
		<pubDate>Tue, 03 Feb 2009 14:36:29 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-23</guid>
		<description>Better practice would be to throw an exception on error. Code becomes much more readable and less error-prone if you don&#039;t have to check for the return value for every single function calls. Instead you catch what you need to catch and ignore the rest. Uncatched exceptions can then be handled by a custom error handler. Returning an error value/code/message is doing you no good.

If I remember correctly, the book &quot;Code Complete&quot; explains thoroughly this practice and its benefits.</description>
		<content:encoded><![CDATA[<p>Better practice would be to throw an exception on error. Code becomes much more readable and less error-prone if you don&#8217;t have to check for the return value for every single function calls. Instead you catch what you need to catch and ignore the rest. Uncatched exceptions can then be handled by a custom error handler. Returning an error value/code/message is doing you no good.</p>
<p>If I remember correctly, the book &#8220;Code Complete&#8221; explains thoroughly this practice and its benefits.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cblin</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-22</link>
		<dc:creator>cblin</dc:creator>
		<pubDate>Tue, 03 Feb 2009 13:49:45 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-22</guid>
		<description>IMHO, non sense.

The best way to deal with an UNEXPECTED error is an exception.

In your example, you say to the user &quot;No data found&quot; whereas you mean something like &quot;the datasource is not availalble&quot; and THIS make a big difference.

Just my 2 cents,</description>
		<content:encoded><![CDATA[<p>IMHO, non sense.</p>
<p>The best way to deal with an UNEXPECTED error is an exception.</p>
<p>In your example, you say to the user &#8220;No data found&#8221; whereas you mean something like &#8220;the datasource is not availalble&#8221; and THIS make a big difference.</p>
<p>Just my 2 cents,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Larry Kagan</title>
		<link>http://daveyshafik.com/archives/479-return-values.html/comment-page-1#comment-20</link>
		<dc:creator>Larry Kagan</dc:creator>
		<pubDate>Tue, 03 Feb 2009 13:08:41 +0000</pubDate>
		<guid isPermaLink="false">http://pixelated-dreams.com/?p=479#comment-20</guid>
		<description>Most of my existing DB models follow a similar method of returning data except that boolean false is returned only if an error occurred.  An empty array or string is returned if no data is found.  Thus $data === &#039;&#039; allows me to tell the user no records found while $data === false tells me to trigger a text message to my phone or log entry because of an error and politely tells the user that an error occurred and he should try again in a few minutes.  

However, as of the past year or two, any error conditions throw exceptions.  So I can avoid the awkward &#039;@return mixed Array on success, false otherwise&#039; phpDoc comment.

Just my 2 cents.</description>
		<content:encoded><![CDATA[<p>Most of my existing DB models follow a similar method of returning data except that boolean false is returned only if an error occurred.  An empty array or string is returned if no data is found.  Thus $data === &#8221; allows me to tell the user no records found while $data === false tells me to trigger a text message to my phone or log entry because of an error and politely tells the user that an error occurred and he should try again in a few minutes.  </p>
<p>However, as of the past year or two, any error conditions throw exceptions.  So I can avoid the awkward &#8216;@return mixed Array on success, false otherwise&#8217; phpDoc comment.</p>
<p>Just my 2 cents.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
