<?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; c#</title>
	<atom:link href="http://danielirvine.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://danielirvine.com</link>
	<description>Code in a cold climate</description>
	<lastBuildDate>Thu, 17 May 2012 17:11:16 +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>The platform is changing (Part 1)</title>
		<link>http://danielirvine.com/blog/2010/10/16/the-platform-is-changing-part-1/</link>
		<comments>http://danielirvine.com/blog/2010/10/16/the-platform-is-changing-part-1/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 16:30:29 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://danielirvine.com/blog/?p=64</guid>
		<description><![CDATA[<p>A colleague and I were talking recently about where our careers are likely to lead us over the next few years.  We’re .NET developers, so our discussion revolved around the .NET platform and its market share.  The broader question of market share for both platforms and programming languages is an interesting topic, with a less [...]]]></description>
			<content:encoded><![CDATA[<p>A colleague and I were talking recently about where our careers are likely to lead us over the next few years.  We’re .NET developers, so our discussion revolved around the .NET platform and its market share.  The broader question of market share for both platforms and programming languages is an interesting topic, with a less certain answer than ever before: the year 2010 has seen the floodgates open on new consumer devices such as slate computers and high-performance smartphones, resulting in an explosion of new platforms and technologies.</p>
<p>Apple’s iOS. Android. Windows 7. Windows <em>Phone</em> 7. Symbian OS. WebOS. Linux. Azure. App Engine. Amazon EC2.  Oh, and not forgetting the World Wide Web.  These are the big platforms that developers can choose to develop for today.  Some of them have a much bigger market share than others, like Windows and iOS, but funnily enough these are also the platforms most at risk of disappearing.</p>
<p>There’s more than just market share to consider, too.  Technical quality is obviously a big driver: application developers want to work with smart tools and languages.  Company malevolence is a hot topic too: some of these platforms are controlled by companies that just aren’t nice.</p>
<p>Gartner says<a href="http://www.guardian.co.uk/business/2010/oct/12/microsoft-smartphone-rival-iphone-android"> 270m smartphones</a> and 20m tablet computers will be sold in 2010.  The industry analyst said recently that <a href="http://www.zdnet.co.uk/news/mobile-devices/2010/10/15/gartner-tablet-sales-to-hit-almost-20-million-this-year-40090550/">it expects 54.8 million tablet computers to be sold in 2011 and more than 208m in 2014</a>.  What I find intriguing is <em>which</em> tablet computers those will be, and <em>which </em>platforms they&#8217;ll be running.  Quite honestly, my money is on Windows and Android.  Not the iOS.</p>
<p>Gartner expects the iPad to keep its lead until 2013, but that’s only while the other platforms step up a gear.  Windows will gain market share through business users, and Android will gain market share through consumers faced with a huge volume of diverse Android devices.</p>
<p>That much is obvious.</p>
<p>I&#8217;m leaving why I’m not interested in iOS (or Apple products at all, for that matter) in another post, but today I’ll focus on .NET and its use.</p>
<p>C# is probably the most productive language ever written.  If you were to give a sizeable development project to both an expert C++ programmer and an expert C# programmer, the C# programmer would complete it in well under half the time it takes the C++ programmer.  There are various reasons for this, such as:</p>
<ul>
<li>Modern language constructs</li>
<li>Better documentation</li>
<li>Better tools, like Resharper</li>
</ul>
<p>Of course that’s all a matter of opinion <img src='http://danielirvine.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   But the future of .NET development is <em>not</em> on the Windows platform, because Windows itself is at risk of ebbing market share.  Even if you see a stable user base for your .NET application, more and more of those users will want to see online versions of your app, or versions that run inside your phone.  They’ll want to do on their mobile devices what they can do on their desktop PC.  There’s an easy way for .NET developers to help them achieve that, and that’s by using Silverlight.  Windows Phone 7 supports Silverlight apps, and so does Symbian.  It’s only a matter of time before there’s a Silverlight client for Android too.</p>
<p>For those of us who still have a need to develop desktop applications for Windows, Silverlight is a great way to “extend” our desktop apps onto new platforms.  We can continue to use C#.  We can continue to use the tools we’ve learned to love.  And we can satisfy our user’s needs quicker than if we’d had to redevelop our apps for an entirely new platform.</p>
<p>To wrap up this post, I believe .NET has a future ahead of it.  For those of us who earn a living by building .NET applications, we need to know Silverlight and start thinking of how to expand our apps onto the new platforms.  That’s how we’ll stay relevant to our users.</p>
<p>There&#8217;s loads left for me to say, but it&#8217;ll have to wait until the next part and beyond.  I want to talk about Apple and why I think they shouldn’t be supported.  I want to talk about my experience with building for the Google platform, and finally I want to talk about what&#8217;s in store for the web&#8230; hopefully I&#8217;ll have the time to write about those soon <img src='http://danielirvine.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://danielirvine.com/blog/2010/10/16/the-platform-is-changing-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

