<?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>Sat, 14 Aug 2010 20:02:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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>
	<item>
		<title>By: Kris</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-15</link>
		<dc:creator>Kris</dc:creator>
		<pubDate>Mon, 11 Aug 2008 12:32:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-15</guid>
		<description>@Andy&lt;br/&gt;&lt;br/&gt;The blinkNotEnoughMoneyLight() would live in the vending machine. I use it to bring up the point of how the code is intended to react to being able to give out a drink - blinking a light to the user indicating more money is needed.&lt;br/&gt;&lt;br/&gt;There is no contract that there should always be a drink from a vending machine. There is probably a business rule that says that once the needed money has been given. The value of null would only given out when that precondition is violated, which would only happen from programmer error.</description>
		<content:encoded><![CDATA[<p>@Andy</p>
<p>The blinkNotEnoughMoneyLight() would live in the vending machine. I use it to bring up the point of how the code is intended to react to being able to give out a drink &#8211; blinking a light to the user indicating more money is needed.</p>
<p>There is no contract that there should always be a drink from a vending machine. There is probably a business rule that says that once the needed money has been given. The value of null would only given out when that precondition is violated, which would only happen from programmer error.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ben.biddington</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-14</link>
		<dc:creator>ben.biddington</dc:creator>
		<pubDate>Mon, 11 Aug 2008 09:36:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-14</guid>
		<description>And provided are informed how to avoid NotEnoughMoneyException, then they need not use try/catch as control flow.</description>
		<content:encoded><![CDATA[<p>And provided are informed how to avoid NotEnoughMoneyException, then they need not use try/catch as control flow.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-13</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Mon, 11 Aug 2008 08:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=9#comment-13</guid>
		<description>@Kris: Where does your sample code live? It seems that it&#039;s in the vending machine, in which case, how does blinkNotEnoughMoneyLight() keep to the contract of returning a Drink?&lt;br/&gt;If it&#039;s in the client code, then &quot;Tell, Don&#039;t Ask&quot; says that we should not base what we ask of the called object on the state of that object. That&#039;s a decision for that object to make. Let&#039;s just ask it and see what we get.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;@eckes: I&#039;m thinking that we might move to a DrinksChute object, which is a collection, but that&#039;s not the way it is at the moment&lt;br/&gt;&lt;br/&gt;@jeremy: Null Object is a possibility, but it depends on the planned usage of the returned object. In this case, the Drink has no behaviour, so it doesn&#039;t make sense to have a NullDrink. If we can&#039;t return a real Drink, I want to know why&lt;br/&gt;&lt;br/&gt;@mark: These exceptions are unchecked :-) I&#039;ll post some thoughts on that soon.&lt;br/&gt;Null Object is useful where we are expecting some behaviour from the returned object rather than just existence.&lt;br/&gt;A quick example that I thought of would be a NullTransaction on a BankAccount. This would have no effect on the balance of the account, but may be used to make a note in the transaction history.&lt;br/&gt;&lt;br/&gt;@sarah: I see where you&#039;re coming from, and there are many possible workarounds, but I have asked the VendingMachine for a Drink. I don&#039;t want my clients to have to worry about DrinkResponses that may or may not have a drink, which then has to be extracted. The DrinkChute collection would be acceptable, but it&#039;s not the simplest thing in this example.&lt;br/&gt;&lt;br/&gt;@all: This is a contrived example, but the exceptions aren&#039;t really being used for business logic. They are unchecked, so there is no reason to catch them unless you want to handle the situation. The system fails fast with a clear reason as to why and where the problem occured.&lt;br/&gt;&lt;br/&gt;Obviously, this is all just my opinion :-)&lt;br/&gt;I&#039;ll defend my choice of using Exceptions with this:&lt;br/&gt;The client code needs a Drink to proceed, it makes no sense to continue running if we do not receive a drink. If we use any of the other solutions, we still have to check to see if it&#039;s ok to proceed (did we get our drink?). With the Exception route, the application stops and tells you why, without additional burden on the client.</description>
		<content:encoded><![CDATA[<p>@Kris: Where does your sample code live? It seems that it&#8217;s in the vending machine, in which case, how does blinkNotEnoughMoneyLight() keep to the contract of returning a Drink?<br />If it&#8217;s in the client code, then &#8220;Tell, Don&#8217;t Ask&#8221; says that we should not base what we ask of the called object on the state of that object. That&#8217;s a decision for that object to make. Let&#8217;s just ask it and see what we get.</p>
<p>@eckes: I&#8217;m thinking that we might move to a DrinksChute object, which is a collection, but that&#8217;s not the way it is at the moment</p>
<p>@jeremy: Null Object is a possibility, but it depends on the planned usage of the returned object. In this case, the Drink has no behaviour, so it doesn&#8217;t make sense to have a NullDrink. If we can&#8217;t return a real Drink, I want to know why</p>
<p>@mark: These exceptions are unchecked <img src='http://andypalmer.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  I&#8217;ll post some thoughts on that soon.<br />Null Object is useful where we are expecting some behaviour from the returned object rather than just existence.<br />A quick example that I thought of would be a NullTransaction on a BankAccount. This would have no effect on the balance of the account, but may be used to make a note in the transaction history.</p>
<p>@sarah: I see where you&#8217;re coming from, and there are many possible workarounds, but I have asked the VendingMachine for a Drink. I don&#8217;t want my clients to have to worry about DrinkResponses that may or may not have a drink, which then has to be extracted. The DrinkChute collection would be acceptable, but it&#8217;s not the simplest thing in this example.</p>
<p>@all: This is a contrived example, but the exceptions aren&#8217;t really being used for business logic. They are unchecked, so there is no reason to catch them unless you want to handle the situation. The system fails fast with a clear reason as to why and where the problem occured.</p>
<p>Obviously, this is all just my opinion <img src='http://andypalmer.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> <br />I&#8217;ll defend my choice of using Exceptions with this:<br />The client code needs a Drink to proceed, it makes no sense to continue running if we do not receive a drink. If we use any of the other solutions, we still have to check to see if it&#8217;s ok to proceed (did we get our drink?). With the Exception route, the application stops and tells you why, without additional burden on the client.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
