<?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: Do Not Litter Your Code With Null Checks</title>
	<atom:link href="http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/</link>
	<description></description>
	<lastBuildDate>Sat, 13 Mar 2010 20:52:38 -0600</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Mike G</title>
		<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/comment-page-1/#comment-38903</link>
		<dc:creator>Mike G</dc:creator>
		<pubDate>Fri, 05 Dec 2008 19:51:33 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/?p=1647#comment-38903</guid>
		<description>Too many developers use exceptions as part of the normal program flow.  They are not designed to be that, they should be used when there is something wrong.

Some methods should return null.  Null can be a valid answer depending on what you are doing.  I have seen code where the only way I could find out if an object was invalid was to try a method, and the method would work or throw an exception.  I had to code a try / catch around the method to trap the silly return value.</description>
		<content:encoded><![CDATA[<p>Too many developers use exceptions as part of the normal program flow.  They are not designed to be that, they should be used when there is something wrong.</p>
<p>Some methods should return null.  Null can be a valid answer depending on what you are doing.  I have seen code where the only way I could find out if an object was invalid was to try a method, and the method would work or throw an exception.  I had to code a try / catch around the method to trap the silly return value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Py</title>
		<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/comment-page-1/#comment-38717</link>
		<dc:creator>Steve Py</dc:creator>
		<pubDate>Wed, 03 Dec 2008 22:33:08 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/?p=1647#comment-38717</guid>
		<description>Just keep in mind that exceptions are just that. The exception, they should not be the norm so any performance concerns around generating stack traces etc. should be m00t. Exceptions should not be used as an indicator of success or failure, that&#039;s what the return value is for. (Which goes into all kinds of fiery territory about when to use return values vs. out parameters but I&#039;m sure that&#039;s been discussed here before.) Simply put for tracking down intermittent issues a useful tool is Break on All errors. As soon as you start using exceptions common-place to indicate &quot;failure&quot;, that tool is basically useless. But that digresses from Null checks...

Having worked on projects where nearly 1/3 of all &quot;bugs&quot; revolve around going through call stacks tracking down root causes for &quot;Object reference not set to an instance of an object.&quot; I&#039;ve come to the conclusion that #null is a twisted, evil time-waster.</description>
		<content:encoded><![CDATA[<p>Just keep in mind that exceptions are just that. The exception, they should not be the norm so any performance concerns around generating stack traces etc. should be m00t. Exceptions should not be used as an indicator of success or failure, that&#8217;s what the return value is for. (Which goes into all kinds of fiery territory about when to use return values vs. out parameters but I&#8217;m sure that&#8217;s been discussed here before.) Simply put for tracking down intermittent issues a useful tool is Break on All errors. As soon as you start using exceptions common-place to indicate &#8220;failure&#8221;, that tool is basically useless. But that digresses from Null checks&#8230;</p>
<p>Having worked on projects where nearly 1/3 of all &#8220;bugs&#8221; revolve around going through call stacks tracking down root causes for &#8220;Object reference not set to an instance of an object.&#8221; I&#8217;ve come to the conclusion that #null is a twisted, evil time-waster.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Fauber</title>
		<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/comment-page-1/#comment-38708</link>
		<dc:creator>David Fauber</dc:creator>
		<pubDate>Wed, 03 Dec 2008 20:14:05 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/?p=1647#comment-38708</guid>
		<description>&quot;I’m not entirely sure what the author really means with this, after all there are probably plenty of developers who consider throwing exceptions when something is wrong instead of letting things blow up as ‘coding defensively’. But if he’s talking about the ‘defensive coding’ variant which often entails returning error codes or null references to avoid exceptions, and then checking for those return values all over your consuming code, then i could not disagree more.&quot;

I think you&#039;re arguing vs a straw man here as that would not be what I&#039;d take code defensively to mean.  Exceptions are high overhead because you&#039;re generating a stack trace, and this can easily tie up resources if you&#039;re in production and for some unforeseen reason that exception raising code starts getting pounded, but anyway, that&#039;s a whole other topic.

Coding defensively I would take to mean checking for nulls where having a null passed in would indicate an error state, then throwing the exception immediately.  Not returning error codes or nulls which need to be checked for.  This is in contrast to not doing said checking, and having try/catch blocks around every place where you&#039;re calling the method.</description>
		<content:encoded><![CDATA[<p>&#8220;I’m not entirely sure what the author really means with this, after all there are probably plenty of developers who consider throwing exceptions when something is wrong instead of letting things blow up as ‘coding defensively’. But if he’s talking about the ‘defensive coding’ variant which often entails returning error codes or null references to avoid exceptions, and then checking for those return values all over your consuming code, then i could not disagree more.&#8221;</p>
<p>I think you&#8217;re arguing vs a straw man here as that would not be what I&#8217;d take code defensively to mean.  Exceptions are high overhead because you&#8217;re generating a stack trace, and this can easily tie up resources if you&#8217;re in production and for some unforeseen reason that exception raising code starts getting pounded, but anyway, that&#8217;s a whole other topic.</p>
<p>Coding defensively I would take to mean checking for nulls where having a null passed in would indicate an error state, then throwing the exception immediately.  Not returning error codes or nulls which need to be checked for.  This is in contrast to not doing said checking, and having try/catch blocks around every place where you&#8217;re calling the method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dew Drop - December 3, 2008 &#124; Alvin Ashcraft's Morning Dew</title>
		<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/comment-page-1/#comment-38680</link>
		<dc:creator>Dew Drop - December 3, 2008 &#124; Alvin Ashcraft's Morning Dew</dc:creator>
		<pubDate>Wed, 03 Dec 2008 14:34:40 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/?p=1647#comment-38680</guid>
		<description>[...] Do Not Litter Your Code with Null Checks (Davy Brion) [...]</description>
		<content:encoded><![CDATA[<p>[...] Do Not Litter Your Code with Null Checks (Davy Brion) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Starr</title>
		<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/comment-page-1/#comment-38617</link>
		<dc:creator>David Starr</dc:creator>
		<pubDate>Tue, 02 Dec 2008 23:11:28 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/?p=1647#comment-38617</guid>
		<description>This is a craft issue and one of balance. Code your classes/methods to behave as you would like them to behave as the caller, IMO.

TDD helps this tremendously, but is most commonly the happy path. I think this is a great argument for testing exception cases in Unit Tests and simply being deliberate about the behavior of your SUT.</description>
		<content:encoded><![CDATA[<p>This is a craft issue and one of balance. Code your classes/methods to behave as you would like them to behave as the caller, IMO.</p>
<p>TDD helps this tremendously, but is most commonly the happy path. I think this is a great argument for testing exception cases in Unit Tests and simply being deliberate about the behavior of your SUT.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jdn</title>
		<link>http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/comment-page-1/#comment-38607</link>
		<dc:creator>jdn</dc:creator>
		<pubDate>Tue, 02 Dec 2008 21:25:42 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/?p=1647#comment-38607</guid>
		<description>&quot; Hurray! We can go home now! Umm… how bout ‘no you can’t and you deserve an elbow to the face’?&quot;

LOL.  I&#039;ll have to remember that one.</description>
		<content:encoded><![CDATA[<p>&#8221; Hurray! We can go home now! Umm… how bout ‘no you can’t and you deserve an elbow to the face’?&#8221;</p>
<p>LOL.  I&#8217;ll have to remember that one.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
