<?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: Returning &#8216;null&#8217; Considered Dishonest</title>
	<atom:link href="http://andypalmer.com/2008/08/returning-null-considered-dishonest/feed/" rel="self" type="application/rss+xml" />
	<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/</link>
	<description>Views on software, technology, consulting and business process</description>
	<lastBuildDate>Fri, 16 Mar 2012 19:19:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Andy Palmer</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-1132</link>
		<dc:creator>Andy Palmer</dc:creator>
		<pubDate>Fri, 16 Mar 2012 19:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-1132</guid>
		<description>I disagree (strongly) with the idea of exceptions being exceptional. They&#039;re actually a half-arsed implementation of &lt;a href=&quot;http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html&quot; rel=&quot;nofollow&quot;&gt;Lisp conditions&lt;/a&gt; (see &lt;a href=&quot;http://en.wikipedia.org/wiki/Greenspun&#039;s_tenth_rule&quot; rel=&quot;nofollow&quot;&gt;Greenspun&#039;s tenth rule&lt;/a&gt;)

I use exceptions when there is no point in the current logic path continuing. In the VendingMachine example, my friend &lt;a href=&quot;http://lunivore.com&quot; rel=&quot;nofollow&quot;&gt;Liz&lt;/a&gt; was able to write a Swing GUI that caught the exception and displayed it as an error message on the LCD display. Quite a result!To do that with return flags, I&#039;d have to check the return flag, and then probably return a return flag to my caller, who&#039;d have to return a return flag to his caller... and so on and so on.

Also, out parameters, by their very nature require mutable state... Not for me, thanks.
</description>
		<content:encoded><![CDATA[<p>I disagree (strongly) with the idea of exceptions being exceptional. They&#8217;re actually a half-arsed implementation of <a href="http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html?referer=');">Lisp conditions</a> (see <a href="http://en.wikipedia.org/wiki/Greenspun's_tenth_rule" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Greenspun_s_tenth_rule?referer=');">Greenspun&#8217;s tenth rule</a>)</p>
<p>I use exceptions when there is no point in the current logic path continuing. In the VendingMachine example, my friend <a href="http://lunivore.com" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/lunivore.com?referer=');">Liz</a> was able to write a Swing GUI that caught the exception and displayed it as an error message on the LCD display. Quite a result!To do that with return flags, I&#8217;d have to check the return flag, and then probably return a return flag to my caller, who&#8217;d have to return a return flag to his caller&#8230; and so on and so on.</p>
<p>Also, out parameters, by their very nature require mutable state&#8230; Not for me, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dragonlips</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-1131</link>
		<dc:creator>dragonlips</dc:creator>
		<pubDate>Fri, 16 Mar 2012 18:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-1131</guid>
		<description>programming by exception for &quot;normal business logic&quot; is a bad idea.

This is a prime example of where a result flag + out params is appropriate.

bool GiveMeaDrink(out Drink)

So now you know if it&#039;s safe or not off one call. If you need to know why give some way to get that info. But really you&#039;re looking into the state of the vending machine at that point and it doesn&#039;t really need to tell you since you already know you were supposed to provide the adequate amount of money first.

You could argue that it&#039;s better to check state first then call. However that&#039;s only partially successful since it would fail in a threaded environment.

however the point is Exceptions are &quot;exceptional&quot; (hense the name) not &quot;normal&quot; you didn&#039;t give me enough money so you can&#039;t have a drink is not exceptional in anyway. You just need to learn how to count your change better.</description>
		<content:encoded><![CDATA[<p>programming by exception for &#8220;normal business logic&#8221; is a bad idea.</p>
<p>This is a prime example of where a result flag + out params is appropriate.</p>
<p>bool GiveMeaDrink(out Drink)</p>
<p>So now you know if it&#8217;s safe or not off one call. If you need to know why give some way to get that info. But really you&#8217;re looking into the state of the vending machine at that point and it doesn&#8217;t really need to tell you since you already know you were supposed to provide the adequate amount of money first.</p>
<p>You could argue that it&#8217;s better to check state first then call. However that&#8217;s only partially successful since it would fail in a threaded environment.</p>
<p>however the point is Exceptions are &#8220;exceptional&#8221; (hense the name) not &#8220;normal&#8221; you didn&#8217;t give me enough money so you can&#8217;t have a drink is not exceptional in anyway. You just need to learn how to count your change better.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Returning Nulls is Dishonest &#124; Ramblings of a wanna be developer</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-1089</link>
		<dc:creator>Returning Nulls is Dishonest &#124; Ramblings of a wanna be developer</dc:creator>
		<pubDate>Thu, 03 Feb 2011 01:31:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-1089</guid>
		<description>[...] The post is here: http://andypalmer.com/2008/08/returning-null-considered-dishonest/ [...]</description>
		<content:encoded><![CDATA[<p>[...] The post is here: <a href="http://andypalmer.com/2008/08/returning-null-considered-dishonest/" rel="nofollow">http://andypalmer.com/2008/08/returning-null-considered-dishonest/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy Palmer &#187; Installing Eclipse</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-127</link>
		<dc:creator>Andy Palmer &#187; Installing Eclipse</dc:creator>
		<pubDate>Sun, 22 Feb 2009 22:26:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-127</guid>
		<description>[...] empty (apart from the comment) method for void types, or return null;. I&#8217;ve already discussed my thoughts on returning null, and I would rather my code failed if it hits an unimplemented method rather than continue in a [...]</description>
		<content:encoded><![CDATA[<p>[...] empty (apart from the comment) method for void types, or return null;. I&#8217;ve already discussed my thoughts on returning null, and I would rather my code failed if it hits an unimplemented method rather than continue in a [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamal Mavadat</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-23</link>
		<dc:creator>Jamal Mavadat</dc:creator>
		<pubDate>Mon, 08 Sep 2008 14:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-23</guid>
		<description>Try-get pattern is supposed to add a hybrid support!!! A solution to another problem...</description>
		<content:encoded><![CDATA[<p>Try-get pattern is supposed to add a hybrid support!!! A solution to another problem&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fredrik</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-22</link>
		<dc:creator>Fredrik</dc:creator>
		<pubDate>Mon, 08 Sep 2008 08:04:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-22</guid>
		<description>Calling Drink() when the vending machine has not yet received enough money would be an invalid operation for the current state of the vending machine - so f.ex throwing an InvalidOperationException would be OK here, imo. &lt;br/&gt;&lt;br/&gt;If you don&#039;t want an exception, you could use TryGet pattern:&lt;br/&gt;&lt;br/&gt;Drink drink;&lt;br/&gt;if(vendingMachine.TryGetDrink(out drink))&lt;br/&gt;{&lt;br/&gt;   // we got a drink!&lt;br/&gt;}</description>
		<content:encoded><![CDATA[<p>Calling Drink() when the vending machine has not yet received enough money would be an invalid operation for the current state of the vending machine &#8211; so f.ex throwing an InvalidOperationException would be OK here, imo. </p>
<p>If you don&#8217;t want an exception, you could use TryGet pattern:</p>
<p>Drink drink;<br />if(vendingMachine.TryGetDrink(out drink))<br />{<br />   // we got a drink!<br />}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamal Mavadat</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-20</link>
		<dc:creator>Jamal Mavadat</dc:creator>
		<pubDate>Fri, 15 Aug 2008 12:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-20</guid>
		<description>@Andy, you&#039;re most welcome matey, my pleasure for being here :)&lt;br/&gt;&lt;br/&gt;Andy said: &lt;i&gt;&quot;I want my code to fail fast. If I don&#039;t get a Drink, I want my code to tell me why.&quot;&lt;/i&gt; As said before, both approaches can co-exist, and now you described a situation in which you shouldn&#039;t be using null-refs! Perhaps solutions such as response-types (normal flow with additional info), or exceptions (exception or error flow optionally with yet more info).&lt;br/&gt;&lt;br/&gt;Examples for returning nulls (sure we have alternatives):&lt;br/&gt;1- getDefaultDrinkOrNull( ) if no-default-drink is considered a typical response and we prefer handling it in normal flow, fortunately designer has cleared all potential confusion by proper naming.&lt;br/&gt;2- in a linked-list I may like getNext( ) return next object or &lt;b&gt;null&lt;/b&gt; for the trail gifting more simplicity for iterations.&lt;br/&gt;&lt;br/&gt;I think the &quot;drink&quot; example is a bit too generic! We could better be discussing null issues by real-world scenarios. For example:&lt;br/&gt;A- If the Drink type is actually a Customer in a web scenario then returning response-types or exceptions are probably better approaches.&lt;br/&gt;B- In a getTempFile( ) : File I might go for exceptions if it cannot return a temp file!&lt;br/&gt;C- And for getActiveUser in a security service I might prefer returning null for indicating no-active-user message - however, I myself have designed security services in which active users were returned by response-types and some specific exceptions too. My mind wasn&#039;t changed, they were just proper solutions for their unique requirements.&lt;br/&gt;&lt;br/&gt;All I try to say is, we should NOT replace null-handling, BUT we may discuss a particular case in which null-handling is not the best.</description>
		<content:encoded><![CDATA[<p>@Andy, you&#8217;re most welcome matey, my pleasure for being here <img src='http://andypalmer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Andy said: <i>&#8220;I want my code to fail fast. If I don&#8217;t get a Drink, I want my code to tell me why.&#8221;</i> As said before, both approaches can co-exist, and now you described a situation in which you shouldn&#8217;t be using null-refs! Perhaps solutions such as response-types (normal flow with additional info), or exceptions (exception or error flow optionally with yet more info).</p>
<p>Examples for returning nulls (sure we have alternatives):<br />1- getDefaultDrinkOrNull( ) if no-default-drink is considered a typical response and we prefer handling it in normal flow, fortunately designer has cleared all potential confusion by proper naming.<br />2- in a linked-list I may like getNext( ) return next object or <b>null</b> for the trail gifting more simplicity for iterations.</p>
<p>I think the &#8220;drink&#8221; example is a bit too generic! We could better be discussing null issues by real-world scenarios. For example:<br />A- If the Drink type is actually a Customer in a web scenario then returning response-types or exceptions are probably better approaches.<br />B- In a getTempFile( ) : File I might go for exceptions if it cannot return a temp file!<br />C- And for getActiveUser in a security service I might prefer returning null for indicating no-active-user message &#8211; however, I myself have designed security services in which active users were returned by response-types and some specific exceptions too. My mind wasn&#8217;t changed, they were just proper solutions for their unique requirements.</p>
<p>All I try to say is, we should NOT replace null-handling, BUT we may discuss a particular case in which null-handling is not the best.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-19</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Thu, 14 Aug 2008 20:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-19</guid>
		<description>@Jamal, thanks for commenting.&lt;br/&gt;I want my code to fail fast. If I don&#039;t get a Drink, I want my code to tell me why.&lt;br/&gt;I don&#039;t want my client code, that is expecting to receive a Drink, to have to check that I didn&#039;t cheat him and give him a null instead :-)&lt;br/&gt;Can you give me an example of when a null is a valid response to something that is expecting a Drink object? That is, can you describe a situation where an object that needs a Drink to work with, would be happy with a null?</description>
		<content:encoded><![CDATA[<p>@Jamal, thanks for commenting.<br />I want my code to fail fast. If I don&#8217;t get a Drink, I want my code to tell me why.<br />I don&#8217;t want my client code, that is expecting to receive a Drink, to have to check that I didn&#8217;t cheat him and give him a null instead <img src='http://andypalmer.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> <br />Can you give me an example of when a null is a valid response to something that is expecting a Drink object? That is, can you describe a situation where an object that needs a Drink to work with, would be happy with a null?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamal Mavadat</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-18</link>
		<dc:creator>Jamal Mavadat</dc:creator>
		<pubDate>Thu, 14 Aug 2008 19:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-18</guid>
		<description>@Andy&lt;br/&gt;IMO classic programmers are usually implementing C-Style control flows! Programming languages with exception support however add more convenience and support to the case, BUT do not replace null-reference handling. Here we go (I know this isn&#039;t the perfect example):&lt;br/&gt;if(( machine = getActiveMachine()) == null) { ... }&lt;br/&gt;else if(( drink = getMeADrink() == null) { ... } ...&lt;br/&gt;In the above example we don&#039;t expect something &quot;exceptional&quot; and everything is checked sequentially branching should a particular condition triggers - this is supposed to be the normal flow.&lt;br/&gt;&lt;br/&gt;BUT exceptions are beautiful if it&#039;s really considered an &quot;exception&quot; and should pick an alternate flow instead of the regular normal flow. For instance, designer of openFile( ) method MIGHT decide to throw an exception if invalid filename supplied!&lt;br/&gt;&lt;br/&gt;Good design employs a mixture of both types; the point is to properly decide what should be considered the &quot;normal&quot; flow and what should be the &quot;exception&quot; or &quot;error&quot; flow! The rest is THE ART OF DESIGN... in summary I stand for proper use of null-handling and exception-flows, not eliminating any of them...</description>
		<content:encoded><![CDATA[<p>@Andy<br />IMO classic programmers are usually implementing C-Style control flows! Programming languages with exception support however add more convenience and support to the case, BUT do not replace null-reference handling. Here we go (I know this isn&#8217;t the perfect example):<br />if(( machine = getActiveMachine()) == null) { &#8230; }<br />else if(( drink = getMeADrink() == null) { &#8230; } &#8230;<br />In the above example we don&#8217;t expect something &#8220;exceptional&#8221; and everything is checked sequentially branching should a particular condition triggers &#8211; this is supposed to be the normal flow.</p>
<p>BUT exceptions are beautiful if it&#8217;s really considered an &#8220;exception&#8221; and should pick an alternate flow instead of the regular normal flow. For instance, designer of openFile( ) method MIGHT decide to throw an exception if invalid filename supplied!</p>
<p>Good design employs a mixture of both types; the point is to properly decide what should be considered the &#8220;normal&#8221; flow and what should be the &#8220;exception&#8221; or &#8220;error&#8221; flow! The rest is THE ART OF DESIGN&#8230; in summary I stand for proper use of null-handling and exception-flows, not eliminating any of them&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-16</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Mon, 11 Aug 2008 15:55:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-16</guid>
		<description>This post is making me thirsty!</description>
		<content:encoded><![CDATA[<p>This post is making me thirsty!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

