<?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: Say &#8220;No&#8221; to &#8220;Null&#8221;</title>
	<atom:link href="http://elegantcode.com/2010/05/01/say-no-to-null/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2010/05/01/say-no-to-null/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=say-no-to-null</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 23:42:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-3/#comment-64288</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 15 Sep 2011 01:49:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-64288</guid>
		<description>Sir Charles Antony Richard Hoare, inventor of the null reference

http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake
</description>
		<content:encoded><![CDATA[<p>Sir Charles Antony Richard Hoare, inventor of the null reference</p>
<p><a href="http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake" rel="nofollow">http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Brings</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-3/#comment-57047</link>
		<dc:creator>Keith Brings</dc:creator>
		<pubDate>Wed, 12 May 2010 15:39:48 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-57047</guid>
		<description>Interesting.  

            I&#039;m not sure if I agree but I can see the merit in your argument. Do you also use SAL annotation to help verify that you code is not capable of passing null into your methods?</description>
		<content:encoded><![CDATA[<p>Interesting.  </p>
<p>            I&#8217;m not sure if I agree but I can see the merit in your argument. Do you also use SAL annotation to help verify that you code is not capable of passing null into your methods?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Sonmez</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-3/#comment-56841</link>
		<dc:creator>John Sonmez</dc:creator>
		<pubDate>Wed, 05 May 2010 20:40:31 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56841</guid>
		<description>&lt;a href=&quot;#comment-56809&quot; rel=&quot;nofollow&quot;&gt;@Chuck&lt;/a&gt; 
&lt;a href=&quot;#comment-56824&quot; rel=&quot;nofollow&quot;&gt;@Schmuli&lt;/a&gt;

Thanks Chuck, I appreciate the feedback.  I think you get my point exactly.  

Schmuli,

To answer you question, in my opinion the check belongs in the public facing methods.  Now, there is a difference between public methods and public facing methods.  If you have a class that you are using internally with public methods, I would lean towards practicing the reduction of use of nulls and checking there.  But if you truly have an external API beyond your control, you need to not only check the input for nulls, but treat the input as malicious, and do whatever protection is needed.  The place to do it is at the point of entrance.</description>
		<content:encoded><![CDATA[<p><a href="#comment-56809" rel="nofollow">@Chuck</a><br />
<a href="#comment-56824" rel="nofollow">@Schmuli</a></p>
<p>Thanks Chuck, I appreciate the feedback.  I think you get my point exactly.  </p>
<p>Schmuli,</p>
<p>To answer you question, in my opinion the check belongs in the public facing methods.  Now, there is a difference between public methods and public facing methods.  If you have a class that you are using internally with public methods, I would lean towards practicing the reduction of use of nulls and checking there.  But if you truly have an external API beyond your control, you need to not only check the input for nulls, but treat the input as malicious, and do whatever protection is needed.  The place to do it is at the point of entrance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Schmuli</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-3/#comment-56824</link>
		<dc:creator>Schmuli</dc:creator>
		<pubDate>Wed, 05 May 2010 17:43:36 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56824</guid>
		<description>There is a difference between passing an invalid null argument to a method and receiving a null return value from a method you called. Null arguments are checked in preconditions (like Code Contracts), and should therefore be checked and dealt with as early as possible. Null return values may be legitimate and generally need to be handled on a per case basis (although you still have empty lists and exceptions where relevant).
My question is: if I have an internal method which is called by a lot of different public-facing methods that don&#039;t care about the value (they are just passing it on), should the null-check still be in each public method, or can I put a single check in the internal method?</description>
		<content:encoded><![CDATA[<p>There is a difference between passing an invalid null argument to a method and receiving a null return value from a method you called. Null arguments are checked in preconditions (like Code Contracts), and should therefore be checked and dealt with as early as possible. Null return values may be legitimate and generally need to be handled on a per case basis (although you still have empty lists and exceptions where relevant).<br />
My question is: if I have an internal method which is called by a lot of different public-facing methods that don&#8217;t care about the value (they are just passing it on), should the null-check still be in each public method, or can I put a single check in the internal method?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chuck</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-3/#comment-56809</link>
		<dc:creator>Chuck</dc:creator>
		<pubDate>Wed, 05 May 2010 10:46:36 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56809</guid>
		<description>John - 

Timely article as I had just read Robert Martin say the same thing in his book &quot;Clean Code&quot; (which all developers should read). 

Both you and Bob did not say &quot;No Null&quot; (like the popular &quot;No SQL&quot;). I think that, through the comments, it has been clarified that we are talking about the objects created in code. Since a database is external, you probably should do some null checks (especially if you pass datasets around).

Of course, like anything, this will work only as well as your testing goes, especially the end-to-end testings. If Bob didn&#039;t follow the standards and passed in an uninitialized and that code makes it to production...the response will most likely be a null object check and Bob will probably never hear about it (unless of course, we decide to enforce the programming standards).

Enjoyed the article and will bring it up to my group today.

Chuck</description>
		<content:encoded><![CDATA[<p>John &#8211; </p>
<p>Timely article as I had just read Robert Martin say the same thing in his book &#8220;Clean Code&#8221; (which all developers should read). </p>
<p>Both you and Bob did not say &#8220;No Null&#8221; (like the popular &#8220;No SQL&#8221;). I think that, through the comments, it has been clarified that we are talking about the objects created in code. Since a database is external, you probably should do some null checks (especially if you pass datasets around).</p>
<p>Of course, like anything, this will work only as well as your testing goes, especially the end-to-end testings. If Bob didn&#8217;t follow the standards and passed in an uninitialized and that code makes it to production&#8230;the response will most likely be a null object check and Bob will probably never hear about it (unless of course, we decide to enforce the programming standards).</p>
<p>Enjoyed the article and will bring it up to my group today.</p>
<p>Chuck</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Sonmez</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-2/#comment-56804</link>
		<dc:creator>John Sonmez</dc:creator>
		<pubDate>Tue, 04 May 2010 21:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56804</guid>
		<description>&lt;a href=&quot;#comment-56803&quot; rel=&quot;nofollow&quot;&gt;@James B&lt;/a&gt; 
Hi James,

I think you are missing the point.

What I am suggesting is transferring the energy used to check for null in your codebase to preventing the passing of null into methods.

You are right you have to check for null somewhere, but in many instances you can &quot;check for null&quot; by writing your code in a such as way that null is not a possibility at all.

It is very hard to show examples that are not very long.

But the core of what I am saying here is that effort used to check for null is better spent preventing null.

The return on this investment is less code and better design.  Null tends to be a crutch.  Trying to prevent null usually leads to a better design.

There are public API points where you need to check for null, but once you pass those points, internally you can prevent null from making it further into your codebase.

Throwing ArgumentNullExceptions is a fairly popular answer, but the problem with that is it is a runtime prevention, which is not a prevention at all.  To the end-user a null reference exception is no different than an ArgumentNullException.  Either way they end up having the application crash.  (Unless you are going to catch ArgumentNullExceptions everywhere.)

If you tend toward immuatable objects, you eliminate the possibility of any of their fields being null.</description>
		<content:encoded><![CDATA[<p><a href="#comment-56803" rel="nofollow">@James B</a><br />
Hi James,</p>
<p>I think you are missing the point.</p>
<p>What I am suggesting is transferring the energy used to check for null in your codebase to preventing the passing of null into methods.</p>
<p>You are right you have to check for null somewhere, but in many instances you can &#8220;check for null&#8221; by writing your code in a such as way that null is not a possibility at all.</p>
<p>It is very hard to show examples that are not very long.</p>
<p>But the core of what I am saying here is that effort used to check for null is better spent preventing null.</p>
<p>The return on this investment is less code and better design.  Null tends to be a crutch.  Trying to prevent null usually leads to a better design.</p>
<p>There are public API points where you need to check for null, but once you pass those points, internally you can prevent null from making it further into your codebase.</p>
<p>Throwing ArgumentNullExceptions is a fairly popular answer, but the problem with that is it is a runtime prevention, which is not a prevention at all.  To the end-user a null reference exception is no different than an ArgumentNullException.  Either way they end up having the application crash.  (Unless you are going to catch ArgumentNullExceptions everywhere.)</p>
<p>If you tend toward immuatable objects, you eliminate the possibility of any of their fields being null.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James B</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-2/#comment-56803</link>
		<dc:creator>James B</dc:creator>
		<pubDate>Tue, 04 May 2010 21:03:51 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56803</guid>
		<description>I disagree with the no-to-null idea for a number of reasons. 

#1
I’ve got the ‘don’t trust the caller’ philosophy engrained too deeply in me.  


#2
There&#039;s no point in making an expensive call to a database, or Active Directory, when I know up front (by checking for null) that the call will fail.


#3
If I use that null value in the calls I make, say, to my Oracle database, I&#039;m going to generate OracleExceptions that I either need to handle, or let propagate to the caller.

If I have to handle them, I&#039;d rather test before-hand, so that I know an OracleException means a true issue calling the database.

Propagating them to the caller breaks encapsulation.  Now the caller has to handle OracleExceptions, and knows how I&#039;m handling my back end.  And if I switch to SQL Server?  Should the caller have to re-write all of his code to handle SQL Server exceptions?

Throwing an ArgumentNullException clearly tells the caller he can&#039;t pass null for the Username.


#4
Contrary to what the author claims, I DO know what a null value means.  If I&#039;m concatenating three strings, and one is null, I&#039;ve decided up front that either (a) a null value can&#039;t be concatenated, or (b) a null value translates to an empty string, which is clearly documented in my XML documentation, and therefore in the help text in the Object Browser.  

I will *always* know what a null value means, because as the designer of the method, I&#039;ve decided up front what it means.  And what I say it means is law.


#5
You have to be anal retentive, SOMEwhere… the argument that you aren’t checking for null in every method doesn’t hold water – you either have to check for null in the callee, or check for null in the caller.  What’s the difference?  It’s not acceptable in either one to only check some of the time.


I mean no disrespect, but this is one of the worst pieces of advice I&#039;ve seen offered in a long time, and all it comes across as is laziness.</description>
		<content:encoded><![CDATA[<p>I disagree with the no-to-null idea for a number of reasons. </p>
<p>#1<br />
I’ve got the ‘don’t trust the caller’ philosophy engrained too deeply in me.  </p>
<p>#2<br />
There&#8217;s no point in making an expensive call to a database, or Active Directory, when I know up front (by checking for null) that the call will fail.</p>
<p>#3<br />
If I use that null value in the calls I make, say, to my Oracle database, I&#8217;m going to generate OracleExceptions that I either need to handle, or let propagate to the caller.</p>
<p>If I have to handle them, I&#8217;d rather test before-hand, so that I know an OracleException means a true issue calling the database.</p>
<p>Propagating them to the caller breaks encapsulation.  Now the caller has to handle OracleExceptions, and knows how I&#8217;m handling my back end.  And if I switch to SQL Server?  Should the caller have to re-write all of his code to handle SQL Server exceptions?</p>
<p>Throwing an ArgumentNullException clearly tells the caller he can&#8217;t pass null for the Username.</p>
<p>#4<br />
Contrary to what the author claims, I DO know what a null value means.  If I&#8217;m concatenating three strings, and one is null, I&#8217;ve decided up front that either (a) a null value can&#8217;t be concatenated, or (b) a null value translates to an empty string, which is clearly documented in my XML documentation, and therefore in the help text in the Object Browser.  </p>
<p>I will *always* know what a null value means, because as the designer of the method, I&#8217;ve decided up front what it means.  And what I say it means is law.</p>
<p>#5<br />
You have to be anal retentive, SOMEwhere… the argument that you aren’t checking for null in every method doesn’t hold water – you either have to check for null in the callee, or check for null in the caller.  What’s the difference?  It’s not acceptable in either one to only check some of the time.</p>
<p>I mean no disrespect, but this is one of the worst pieces of advice I&#8217;ve seen offered in a long time, and all it comes across as is laziness.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peSHIr</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-2/#comment-56793</link>
		<dc:creator>peSHIr</dc:creator>
		<pubDate>Tue, 04 May 2010 09:12:37 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56793</guid>
		<description>&lt;a href=&quot;#comment-56781&quot; rel=&quot;nofollow&quot;&gt;@julia &lt;/a&gt; 

How about the three null checks you are not doing in &quot;ds.Tables[0].Rows[0][&quot;JOLInd&quot;].ToString()&quot;? In fact, the six null checks you&#039;re not doing, because you copy pasted that compound expression so you have it two times. The world is (almost always) more complex than you initially think...

&lt;a href=&quot;http://stackoverflow.com/questions/490420/favorite-clever-defensive-programming-best-practices/490766#490766&quot; rel=&quot;nofollow&quot;&gt;I prefer to check all arguments of public API methods and raising ArgumentExceptions on those as appropriate&lt;/a&gt; and then try to use (and check for) as little nulls as possible, for instance by using &lt;a href=&quot;http://stackoverflow.com/questions/490420/favorite-clever-defensive-programming-best-practices/490746#490746&quot; rel=&quot;nofollow&quot;&gt;sentinels&lt;/a&gt; where appropriate. Any problems regarding nulls should be found in unit test code or by software testers (through the applications top level exception handling).</description>
		<content:encoded><![CDATA[<p><a href="#comment-56781" rel="nofollow">@julia </a> </p>
<p>How about the three null checks you are not doing in &#8220;ds.Tables[0].Rows[0]["JOLInd"].ToString()&#8221;? In fact, the six null checks you&#8217;re not doing, because you copy pasted that compound expression so you have it two times. The world is (almost always) more complex than you initially think&#8230;</p>
<p><a href="http://stackoverflow.com/questions/490420/favorite-clever-defensive-programming-best-practices/490766#490766" rel="nofollow">I prefer to check all arguments of public API methods and raising ArgumentExceptions on those as appropriate</a> and then try to use (and check for) as little nulls as possible, for instance by using <a href="http://stackoverflow.com/questions/490420/favorite-clever-defensive-programming-best-practices/490746#490746" rel="nofollow">sentinels</a> where appropriate. Any problems regarding nulls should be found in unit test code or by software testers (through the applications top level exception handling).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: julia</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-2/#comment-56781</link>
		<dc:creator>julia</dc:creator>
		<pubDate>Tue, 04 May 2010 03:25:41 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56781</guid>
		<description>Hi, I&#039;m not an expert developer. 
I wanna know why we need to avoid null checking in our codes?
I usually used to check null values especially when I retrieve the result from database.
eg: JOLInd = (!string.IsNullOrEmpty(ds.Tables[0].Rows[0][&quot;JOLInd&quot;].ToString()) ? ds.Tables[0].Rows[0][&quot;JOLInd&quot;].ToString() : string.Empty);

checking null is really a bad practice?</description>
		<content:encoded><![CDATA[<p>Hi, I&#8217;m not an expert developer.<br />
I wanna know why we need to avoid null checking in our codes?<br />
I usually used to check null values especially when I retrieve the result from database.<br />
eg: JOLInd = (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["JOLInd"].ToString()) ? ds.Tables[0].Rows[0]["JOLInd"].ToString() : string.Empty);</p>
<p>checking null is really a bad practice?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Py</title>
		<link>http://elegantcode.com/2010/05/01/say-no-to-null/comment-page-2/#comment-56779</link>
		<dc:creator>Steve Py</dc:creator>
		<pubDate>Tue, 04 May 2010 02:34:24 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2010/05/01/say-no-to-null/#comment-56779</guid>
		<description>Dunno, you can coat a turd in icing-sugar but it still won&#039;t be a jelly donut. Granted, I hate NullReferenceException, but always returning a reference to an object that&#039;s been flagged as invalid, or returning a (potentially empty) list of objects is just masking one problem with others.

What if you forget to put the check in for .IsValid? If you return a list and 90% of the time you&#039;re dealing with 1 expected result you&#039;re going to have to remember to check if the list is empty or not because [0] will net you index out of range exceptions, and similar with Linq&#039;s &quot;First()&quot; unless you use &quot;FirstOrDefault()&quot; in which case you&#039;re exactly back where you started. I fact, I find I need to deal with #null more frequently with Linq since I often find myself using functions like FirstOrDefault().

Personally I favour to check required arguments and throw ArgumentNullExceptions with the parameter name. These are exceptions, they&#039;re going to need to be raised, recorded, and fixed, or at minimum, presented to the user in a meaningful way... No sugar-coating required. Though I definitely agree that code should always avoid using/returning #null wherever possible. #Null is here to stay, so just be sure to guard against nothing when you expect something.</description>
		<content:encoded><![CDATA[<p>Dunno, you can coat a turd in icing-sugar but it still won&#8217;t be a jelly donut. Granted, I hate NullReferenceException, but always returning a reference to an object that&#8217;s been flagged as invalid, or returning a (potentially empty) list of objects is just masking one problem with others.</p>
<p>What if you forget to put the check in for .IsValid? If you return a list and 90% of the time you&#8217;re dealing with 1 expected result you&#8217;re going to have to remember to check if the list is empty or not because [0] will net you index out of range exceptions, and similar with Linq&#8217;s &#8220;First()&#8221; unless you use &#8220;FirstOrDefault()&#8221; in which case you&#8217;re exactly back where you started. I fact, I find I need to deal with #null more frequently with Linq since I often find myself using functions like FirstOrDefault().</p>
<p>Personally I favour to check required arguments and throw ArgumentNullExceptions with the parameter name. These are exceptions, they&#8217;re going to need to be raised, recorded, and fixed, or at minimum, presented to the user in a meaningful way&#8230; No sugar-coating required. Though I definitely agree that code should always avoid using/returning #null wherever possible. #Null is here to stay, so just be sure to guard against nothing when you expect something.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

