<?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>{(something&#124;&#124;other).soft()} &#187; Development</title>
	<atom:link href="http://www.somethingorothersoft.com/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.somethingorothersoft.com</link>
	<description>About Software Development And Other Stuff By Igor Zevaka</description>
	<lastBuildDate>Mon, 25 Jul 2011 00:03:47 +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>A few things you might not know about System.Nullable</title>
		<link>http://www.somethingorothersoft.com/2010/05/29/a-few-things-you-might-not-know-about-system-nullable/</link>
		<comments>http://www.somethingorothersoft.com/2010/05/29/a-few-things-you-might-not-know-about-system-nullable/#comments</comments>
		<pubDate>Sat, 29 May 2010 13:59:24 +0000</pubDate>
		<dc:creator>igor</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.somethingorothersoft.com/?p=385</guid>
		<description><![CDATA[There is plenty of information about Nullable&#60;T&#62; type out there and how to use it. There are, however a few tidbits that are not quite as obvious at first glance. Nullable is a value type It&#8217;s a struct that can be null. The only reason why int? intVal = null; compiles is because C# compiler <a href="http://www.somethingorothersoft.com/2010/05/29/a-few-things-you-might-not-know-about-system-nullable/" rel="bookmark" title="Read on...">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>There is plenty of information about <code>Nullable&lt;T&gt;</code> type out there and how to use it. There are, however a few tidbits that are not quite as obvious at first glance.</p>

<h1>Nullable<T> is a value type</h1>

<p>It&#8217;s a struct that can be null. The only reason why <code>int? intVal = null;</code> compiles is because C# compiler supports nullable types. You <strong>cannot</strong> define the same kind of struct yourself.</p>

<p>The decision to make <code>Nullable&lt;T&gt;</code> a value type actually makes sense if you think about it. Calling its member methods and properties will always succeed, i.e. NullReferenceException will never be thrown. This gurantees that <code>HasValue</code> never throws a NullReferenceException.</p>

<h1>Comparison to null</h1>

<p>Under the hood, the compiler, in fact translates comparison to null to a call to HasValue. The below MSIL demonstrates that:</p>

<div>
<pre class="brush: csharp; title: ; notranslate">
    .locals init ([0] valuetype [mscorlib]System.Nullable`1&lt;int32&gt; 'value',
           [1] bool hasvalue,
           [2] bool hasvalue2)

    //int? value = null;
    IL_0001:  ldloca.s   'value'
    IL_0003:  initobj    valuetype [mscorlib]System.Nullable`1&lt;int32&gt;
    //bool hasvalue = value.HasValue;
    IL_0009:  ldloca.s   'value'
    IL_000b:  call       instance bool valuetype [mscorlib]System.Nullable`1&lt;int32&gt;::get_HasValue()
    IL_0010:  stloc.1
    //bool hasvalue2 = value != null;
    IL_0011:  ldloca.s   'value' //loads a variable onto evaluation stack
    IL_0013:  call       instance bool valuetype [mscorlib]System.Nullable`1&lt;int32&gt;::get_HasValue() //calls HasValue
    IL_001b:  stloc.2 //Stores the result in location 2 (hasvalue2)
</pre>
</div>

<p>As you can see the generated IL for <code>hasvalue = value.HasValue</code> and <code>hasvalue2 = value != null</code> are identical.</p>

<h1>null Assignment</h1>

<p>Also from above code you can see that assigning null to a nullable value is the same as calling the parameterless contructor (which all value types have).</p>

<h1>Ternary operator</h1>

<p>C# compiler also does special things with ternary operator for nullable types. The following code compiles:</p>

<div>
<pre class="brush: csharp; title: ; notranslate">
int? nullableIntVal = 4;
int intVal = nullableIntVal ?? -1;
</pre>
</div>

<p>The above is just syntactic sugar for calling <code>HasValue</code> and if it&#8217;s true calling GetValueOrDefault(). The compiler actually generates a temporary variable to call <code>HasValue</code> off in both Release and Debug modes. Not sure why that is. Below code illustrates the equivalent C# code to what the compiler generates for ternary operator.</p>

<div>
<pre class="brush: csharp; title: ; notranslate">
int? nullableIntVal = 4;
int? temp = nullableIntVal;
int intVal = temp.HasValue ?? temp.GetValueOrDefault() : -1;
</pre>
</div>

<h1>Summary</h1>

<p><code>Nullable&lt;T&gt;</code> is a special value type that has support from C# compiler to allow the following:</p>

<ul>
<li><code>null</code> assignment  &#8211; under the hood: calling defualt constructor.</li>
<li><code>null</code> comparison &#8211; under the hood: calling <code>HasValue</code> property.</li>
<li>ternary operator <code>??</code> &#8211; under the hood: calling <code>HasValue</code> and conditionally <code>GetValueOrDefault</code>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.somethingorothersoft.com/2010/05/29/a-few-things-you-might-not-know-about-system-nullable/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Test Driven Development</title>
		<link>http://www.somethingorothersoft.com/2009/08/01/test-driven-development/</link>
		<comments>http://www.somethingorothersoft.com/2009/08/01/test-driven-development/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 05:14:20 +0000</pubDate>
		<dc:creator>igor</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.somethingorothersoft.com/?p=37</guid>
		<description><![CDATA[I heard a lot of buzz about test driven development. All that time I thought that all that means is writing unit tests. Then I found out about writing tests before writing code. Meh, I thought, no big deal, a bit fussy if you ask me, it still hasn&#8217;t changed my view on the whole <a href="http://www.somethingorothersoft.com/2009/08/01/test-driven-development/" rel="bookmark" title="Read on...">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I heard a lot of buzz about test driven development. All that time I thought that all that means is writing unit tests. Then I found out about writing tests before writing code. Meh, I thought, no big deal, a bit fussy if you ask me, it still hasn&#8217;t changed my view on the whole thing.</p>

<p>Then recently I saw some unit tests in a Microsoft article talking about using TDD to create web controls using Model-View-Presenter paradigm. The article itself can be found here <a href="http://msdn.microsoft.com/en-us/magazine/cc188690.aspx">http://msdn.microsoft.com/en-us/magazine/cc188690.aspx</a>.</p>

<p>Its crazy! You write twice as much code! Not only that, you have to use this framework to create mock objects and test methods on the interfaces before even writing them (tests before code, doh).</p>

<p>Having said that, I am willing to give TDD for really just one reason. And the reason is that writing tests that independently test the two halves (e.g. View and Presenter) makes you think upfront about the design of the two interfaces a bit more. This, in turn, precipitates the right design faster, without having to write a lot of either UI or business logic code.</p>

<p>The long  term advantage is quite obvious and is comes from both Separation Of Concerns principles (whatever shape they come in &#8211; MVP, MVC, MVV, MVA) and having unit tests. In some time, if there is a need to rewrite the implementation of any part of the system, the interfaces between them are loosely coupled and testable. The latter, in my opinion, is even more important than the former. Having a testable interface will aid finding the problems in the new implementation more than a &#8220;beautifully&#8221; designed one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.somethingorothersoft.com/2009/08/01/test-driven-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

