<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Daniel Irvine &#187; object-oriented-design</title>
	<atom:link href="http://danielirvine.com/blog/tag/object-oriented-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://danielirvine.com</link>
	<description>Code in a cold climate</description>
	<lastBuildDate>Mon, 18 Jul 2011 21:34:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Null parameter values are evil</title>
		<link>http://danielirvine.com/blog/2010/07/17/banishing-null-parameters-value/</link>
		<comments>http://danielirvine.com/blog/2010/07/17/banishing-null-parameters-value/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 13:37:07 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[object-oriented-design]]></category>

		<guid isPermaLink="false">http://danielirvine.com/blog/?p=53</guid>
		<description><![CDATA[<p>One of the ideas I suggested in my last post was, What if C#:</p> <p>prohibited passing null as method parameters</p> <p>In this post I&#8217;ll discuss why I feel it&#8217;s a good thing, and why it&#8217;s entirely feasible to do.</p> <p>So what the hell am I talking about when I say null parameter values are evil? [...]]]></description>
			<content:encoded><![CDATA[<p>One of the ideas I suggested in my last post was, <em>W</em><em>hat if C#</em>:</p>
<blockquote><p>prohibited passing null as method parameters</p></blockquote>
<p>In this post I&#8217;ll discuss why I feel it&#8217;s a good thing, and why it&#8217;s entirely feasible to do.</p>
<p>So what the hell am I talking about when I say <em>null parameter values are evil</em>?  First up, I&#8217;m not saying nulls are evil.  Sir Tony Hoare famously called null references the <a href="http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare">billion dollar mistake</a>.  He was talking about historically bad ideas at the time, and believes that we&#8217;d all be better off if &#8220;null&#8221; never existed.  I&#8217;m not convinced.  I feel nulls have a place and a special meaning, just like <em>void</em> has a meaning.</p>
<p>So I&#8217;m focusing on a smaller idea: null parameter passing.  I consider this an anti-pattern:</p>
<pre>R Method(A a, B b)
{
    if(a == null) throw ArgumentNullException("a");
    if(a == null) throw ArgumentNullException("b");

    // ...
}</pre>
<p><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: small;"><span style="line-height: 18px; white-space: pre;"><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: small;"><span style="line-height: 19px; white-space: normal;">There are a few reasons why I&#8217;m not happy with this.</span></span></span></span></p>
<ul>
<li><span style="font-size: small;">We rely on human code reviews for ensuring this (anti)pattern is always followed.  That means there&#8217;s a risk it&#8217;ll be missed.</span></li>
<li><span style="font-size: small;">It&#8217;s boilerplate.  Our code would be much cleaner and easier to read if we didn&#8217;t have to follow this.</span></li>
<li><span style="font-size: small;">This is just one way of a variety for null checking: for example, we can also use</span></li>
</ul>
<pre style="padding-left: 60px;">Debug.Assert(a != null, String.Format(Messages.CannotBeNull, "a"));</pre>
<p>So I&#8217;m suggesting we move the job of generating this code to the compiler, so that the above example code  can now be written as</p>
<p><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">R Method(A a, B b)<br />
</span><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">{</span></p>
<pre>    // a and b are definitely not null if this point is reached</pre>
<pre>    // ...
}</pre>
<p>Calling code looks like this:</p>
<pre>a = null;
b = null; 
var r = obj.Method(a, b);  // throws an ArgumentNullException</pre>
<p>So, it&#8217;s technically possible&#8211;but the remaining question is this: <strong>are there any valid use cases for null method parameter values</strong>?  The answer as far as I can tell is no.  The most obvious use for null parameters is with method overloading:</p>
<pre>R Method(A a, B b)
{
    // do stuff
}

R Method(A a)
{
    return Method(a, null);
}</pre>
<p>This can be refactored as:</p>
<pre>R Method(A a, B b)
{
    DoSomethingWithA(a);
    DoSomethingWithB(b);
    DoSomethingWithAandB(a, b);
}

R Method(A a)
{
   DoSomethingWithA(a);
   // since a is passed in alone, we can assume that its value
   // is independent of B, so DoSomethingWithAandB() does not
   // need to be called, or can be called with b's previous
   // value. 
}</pre>
<p>This is actually a neater design anyway, because it requires splitting out the independent processing of the variables.  I&#8217;ve come across one situation that&#8217;s a bit ugly: when you need to call 3rd party libraries that accept and expect null parameters.  For example:</p>
<p><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">public R OtherMethod(T t)<br />
</span><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">{<br />
</span><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;"> _thirdPartyLibrary.OtherMethod(t);   // what to do if null is a valid value?<br />
</span><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">} </span></p>
<p>This is especially difficult if the connection to the third party library requires set up and disposing, for example:</p>
<p><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">public R OtherMethod(T t)</span></p>
<pre>{
    using(var thirdPartyLibrary = new ThirdPartyLibrary())
    {
        thirdPartyLibrary.OtherMethod(t);
    }
}</pre>
<p>This would necessarily become:</p>
<pre>public R OtherMethod(T t)
{
    using(var thirdPartyLibrary = new ThirdPartyLibrary())
    {
        thirdPartyLibrary.OtherMethod(t);
    }
}</pre>
<pre>// this overload is used for null values
public R OtherMethod()
{
    using( var thirdPartyLibrary = new ThirdPartyLibrary())
    {
        thirdPartyLibrary.OtherMethod(null);   // passing null is fine
                                               // because this component
                                               // wasn't built with our
                                               // amazing compilation technique
    }
}</pre>
<p>Yes, it&#8217;s our old friend <em>repeated code</em> &#8211; but I&#8217;d happily put up with this for the sake of the extra precondition checking I&#8217;d get for free.</p>
<p>How to implement this is the next question: one option would be a form of IL injection, like aspect-oriented programming.  Another option would be an entirely new compiler, with not just this language modification, but all the other changes I suggested in my previous post.   That would be a cool research project!</p>
<p>If you&#8217;re still following along, there are a couple of obvious questions that come up at this point:</p>
<ol>
<li>What about .NET 4 code contracts?</li>
<li>What about F#?</li>
</ol>
<p>Both valid points, and yes both solve this issue, but unfortunately I don&#8217;t like either solution.  Code contracts is again more boilerplate code &#8211; it just shifts the form of the code written.  I believe that for software engineering to take the next step, we need to move towards less code and more component-based design.  F#, on the other hand, solves it by dissallowing nulls entirely, but I think F# is an ugly language.  Spec# was on the right track, but that&#8217;s really what code contracts have become in C#.</p>
<p>I&#8217;m always interested in projects making the leap to non-nullable types.  I think null really helps, in particular <em>returning</em> null from methods, either as the return value or an out/ref parameter.  The <a href="http://msdn.microsoft.com/en-us/library/bb347013.aspx">TryGetValue pattern</a> is an awesome example of this.  It&#8217;s a useful idiom because you can do a Has and a Get in one atomic operation.  You <em>need</em> null in this case&#8211;it just doesn&#8217;t make sense to return any value other than null, if Has is false.  Of course, proponents of non-nullable types would argue that there are other ways of achieving the same thing&#8211;but that&#8217;s a discussion for another day&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://danielirvine.com/blog/2010/07/17/banishing-null-parameters-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What if&#8230;.?</title>
		<link>http://danielirvine.com/blog/2010/07/15/what-if/</link>
		<comments>http://danielirvine.com/blog/2010/07/15/what-if/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 07:06:46 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[object-oriented-design]]></category>

		<guid isPermaLink="false">http://danielirvine.com/blog/?p=47</guid>
		<description><![CDATA[<p>What if C#&#8230;.</p> had no properties? had only private fields? fields were readonly by default? had no mutable static fields? had no static methods or static classes? prohibited passing null as method parameters? integrated specs/test with code, in the same file? only allowed one class per file? prohibited nested classes? had syntax distinguishing between class [...]]]></description>
			<content:encoded><![CDATA[<p>What if C#&#8230;.</p>
<ul>
<li>had no properties?</li>
<li>had only private fields?</li>
<li>fields were readonly by default?</li>
<li>had no mutable static fields?</li>
<li>had no static methods or static classes?</li>
<li>prohibited passing null as method parameters?</li>
<li>integrated specs/test with code, in the same file?</li>
<li>only allowed one class per file?</li>
<li>prohibited nested classes?</li>
<li>had syntax distinguishing between class dependencies and notifications with special syntax?</li>
<li>enforced command-query separation from methods?</li>
</ul>
<p>Some of these ideas are almost unworkable, some are fundamental to the cause and some are just &#8220;my style&#8221;.  In a future post I&#8217;ll go over each in detail and see how they can be implemented&#8211;maybe I&#8217;ll just hack a few FxCop rules together and be done with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://danielirvine.com/blog/2010/07/15/what-if/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

