<?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>Haugerns Development Escapades &#187; best practice</title>
	<atom:link href="http://www.haugern.net/blog/category/best-practice/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haugern.net/blog</link>
	<description>Morten Haug on development and other curiosities</description>
	<lastBuildDate>Sun, 31 May 2009 21:56:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>I Have a Dream</title>
		<link>http://www.haugern.net/blog/i-have-a-dream/</link>
		<comments>http://www.haugern.net/blog/i-have-a-dream/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 19:52:35 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[agile]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/i-have-a-dream/</guid>
		<description><![CDATA[The usual suspects
Whenever I’m presenting anything, I try to start off showing the “Top 20 replies from a programmer when their programs doesn’t work”. In case you’ve never seen it, here it is:

It works on my machine. 
Where were you when the program blew up? 
Why do you want to do it that way? 
You [...]]]></description>
			<content:encoded><![CDATA[<h3>The usual suspects</h3>
<p>Whenever I’m presenting anything, I try to start off showing the “Top 20 replies from a programmer when their programs doesn’t work”. In case you’ve never seen it, here it is:</p>
<ol>
<li>It works on my machine. </li>
<li>Where were you when the program blew up? </li>
<li>Why do you want to do it that way? </li>
<li>You can’t use that version on your system. </li>
<li>Even though it doesn’t work, how does it feel? </li>
<li>Did you check for a virus on your system? </li>
<li>Somebody must have changed my code. </li>
<li>It works, but it hasn’t been tested. </li>
<li>THIS can’t be the source of THAT. </li>
<li>I can’t test everything! </li>
<li>It’s just some unlucky coincidence. </li>
<li>You must have the wrong version. </li>
<li>I haven’t touched that module in weeks! </li>
<li>There is something funky in your data. </li>
<li>What did you type in wrong to get it to crash? </li>
<li>It must be a hardware problem. </li>
<li>How is that possible? </li>
<li>It worked yesterday. </li>
<li>It’s never done that before. </li>
<li>That’s weird… </li>
</ol>
<p>I’m not quite sure who wrote it originally, but I was given a paper copy several years ago. I must admit I had a good laugh at the time, as I could relate to most of the replies.</p>
<p>So whenever I’m presenting this list, there’s a giggle and a great deal of nodding and smiling going on in the audience. Then I go on to present something that will alleviate one or more items in this list.</p>
<p><strong>But it’s not a laughing matter! It’s the sad state of our beloved profession we’re laughing at. It’s not supposed to be like this.</strong></p>
<h3>The Dream</h3>
<p>When I’m presenting the list in 10 years from now, there’s not a giggle. Not a smile. <strong>Just silent shame of times past or loud laughter at good ol’ days.</strong> Hopefully no one doesn’t even recognize the items on the list.</p>
<p>I might have a different kind of list. But not this one. We have made progress.</p>
<h3>What to do next</h3>
<p>So I propose this list as the metric for how mature your development organization is. Same rules apply as in <a href="http://naraku.net/2008/02/07/code-reviews-ala-wtf-metric/" target="_blank">the wtf code review metric</a>. Less is better. Drop the <a href="http://www.joelonsoftware.com/articles/fog0000000043.html" target="_blank">Joel test</a>. Drop the <a href="http://jeffsutherland.com/scrum/2008/08/nokia-test-where-did-it-come-from.html" target="_blank">Nokia test</a>. Drop test X. Use this. <strong>Now!</strong></p>
<p>The worst part of this is that most of these are solved problems. Test Driven Development, Continuous Integration, and Pair Programming have existed a decade. Their poorer, but still useful cousins, Unit Testing, Automatic Builds, and Code Reviews have been around the block even longer. Iterative and Incremental development, Miniature Milestones, and Review and Adapt has been grown ups for a while as well.</p>
<p><strong>So there is no excuse. The answers are at your doorstep. But it takes discipline. And time. </strong></p>
<p>But you’re not alone. I’m off trying to do my part. Our mission is clear. Will you do yours?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/i-have-a-dream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get those Guards up</title>
		<link>http://www.haugern.net/blog/get-those-guards-up/</link>
		<comments>http://www.haugern.net/blog/get-those-guards-up/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 21:45:24 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[best practice]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[oo principles]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/get-those-guards-up/</guid>
		<description><![CDATA[I often find code which have several nested conditionals like this:


public void FunctionWithoutGuardClauses(Person person)
{
&#160;&#160;&#160; if (person != null)
&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (person.Phone != null)
&#160;&#160;&#160;&#160;&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; person.Phone.Call();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160;&#160;&#160;&#160;&#160; else
&#160;&#160;&#160;&#160;&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new ArgumentNullException();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160; }
&#160;&#160;&#160; else
&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new ArgumentNullException();
&#160;&#160;&#160; }
}

According to Steve McConnell in Code Complete 2 it is just fine with regards to putting the [...]]]></description>
			<content:encoded><![CDATA[<p>I often find code which have several nested conditionals like this:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red255\green128\blue0;\red0\green0\blue0;\red255\green255\blue255;\red128\green128\blue64;\red43\green145\blue175;}??\fs24 \cf1\cb2\highlight2 public\cf3  \cf1 void\cf3  \cf4 FunctionWithoutGuardClauses\cf3 (\cf5 Person\cf3  \cf4 person\cf3 )\par ??\{\par ??    \cf1 if\cf3  (\cf4 person\cf3  != \cf1 null\cf3 )\par ??    \{\par ??        \cf1 if\cf3  (\cf4 person\cf3 .\cf4 Phone\cf3  != \cf1 null\cf3 )\par ??        \{\par ??            \cf4 person\cf3 .\cf4 Phone\cf3 .\cf4 Call\cf3 ();\par ??        \}\par ??        \cf1 else\par ??\cf3         \{\par ??            \cf1 throw\cf3  \cf1 new\cf3  \cf5 ArgumentNullException\cf3 ();\par ??        \}\par ??    \}\par ??    \cf1 else\par ??\cf3     \{\par ??        \cf1 throw\cf3  \cf1 new\cf3  \cf5 ArgumentNullException\cf3 ();\par ??    \}\par ??\}\par ??}<br />
-->
<div style="font-size: small; background: black; color: white; font-family: consolas">
<p style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">FunctionWithoutGuardClauses</span>(<span style="color: #2b91af">Person</span> <span style="color: #808040">person</span>)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">if</span> (<span style="color: #808040">person</span> != <span style="color: #ff8000">null</span>)</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">if</span> (<span style="color: #808040">person</span>.<span style="color: #808040">Phone</span> != <span style="color: #ff8000">null</span>)</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #808040">person</span>.<span style="color: #808040">Phone</span>.<span style="color: #808040">Call</span>();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">else</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">throw</span> <span style="color: #ff8000">new</span> <span style="color: #2b91af">ArgumentNullException</span>();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">else</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">throw</span> <span style="color: #ff8000">new</span> <span style="color: #2b91af">ArgumentNullException</span>();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">}<a href="http://www.haugern.net/blog/wp-content/windowslivewriterdevelopmenttoolboxguardclauses-1159fknight4-2.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 5px 5px 25px; border-right-width: 0px" height="240" alt="knight4" src="http://www.haugern.net/blog/wp-content/windowslivewriterdevelopmenttoolboxguardclauses-1159fknight4-thumb.gif" width="126" align="right" border="0"></a></p>
</div>
<p>According to <a href="http://www.cc2e.com/" target="_blank">Steve McConnell in Code Complete 2</a> it is just fine with regards to putting the most probable clause first. But it is also <a href="http://c2.com/cgi/wiki?ArrowAntiPattern" target="_blank"></a></a>an <a href="http://en.wikipedia.org/wiki/Anti-pattern" target="_blank">anti-pattern</a> in this case; <a href="http://c2.com/cgi/wiki?ArrowAntiPattern" target="_blank">arrow code</a>. With even more nesting it gets worse, and it starts to get nightmarish to follow the possible execution paths.</p>
<p>Oh, and see how I blatantly disregard the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter" target="_blank">Law of Demeter</a>!</p>
<h3>Introducing Guard Clauses</h3>
<p>A guard clause is a conditional which constitutes our first line of defence in our methods. It can break control flow in an elegant matter, so the only thing left to follow is the valid execution path.</p>
<p>There is also <strong>nothing wrong with more than one return path</strong> in our methods, as long as it is as clean as it gets with guard clauses. <a href="http://www.codinghorror.com/blog/archives/000486.html" target="_blank">Atwood seems to think so as well</a>, so I&#8217;m really home safe here ;-p</p>
<p>So lets see how the above code can be <a href="http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html" target="_blank">refactored with guard clauses</a>:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red255\green128\blue0;\red0\green0\blue0;\red255\green255\blue255;\red128\green128\blue64;\red43\green145\blue175;}??\fs24 \cf1\cb2\highlight2 public\cf3  \cf1 void\cf3  \cf4 FunctionWithGuardClauses\cf3 (\cf5 Person\cf3  \cf4 person\cf3 )\par ??\{\par ??    \cf1 if\cf3  (\cf4 person\cf3  == \cf1 null\cf3  || \cf4 person\cf3 .\cf4 Phone\cf3  == \cf1 null\cf3 )\par ??        \cf1 throw\cf3  \cf1 new\cf3  \cf5 ArgumentNullException\cf3 ();\par ??\par ??    \cf4 person\cf3 .\cf4 Phone\cf3 .\cf4 Call\cf3 ();\par ??\}\par ??}<br />
-->
<div style="font-size: small; background: black; color: white; font-family: consolas">
<p style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">FunctionWithGuardClauses</span>(<span style="color: #2b91af">Person</span> <span style="color: #808040">person</span>)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">if</span> (<span style="color: #808040">person</span> == <span style="color: #ff8000">null</span> || <span style="color: #808040">person</span>.<span style="color: #808040">Phone</span> == <span style="color: #ff8000">null</span>)</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff8000">throw</span> <span style="color: #ff8000">new</span> <span style="color: #2b91af">ArgumentNullException</span>();</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #808040">person</span>.<span style="color: #808040">Phone</span>.<span style="color: #808040">Call</span>();</p>
<p style="margin: 0px">}</p>
</div>
<p>So what are you waiting for, get those guards up and tear those nested nightmares apart!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/get-those-guards-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.Net Collection&lt;T&gt; vs. List&lt;T&gt;</title>
		<link>http://www.haugern.net/blog/net-collectiont-vs-listt/</link>
		<comments>http://www.haugern.net/blog/net-collectiont-vs-listt/#comments</comments>
		<pubDate>Sun, 02 Dec 2007 21:21:16 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[best practice]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[dotnet]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/net-collectiont-vs-listt/</guid>
		<description><![CDATA[Recently, we have gotten a couple of &#8220;fresh out of school&#8221; employees, and here the other day we went through the code of one of our applications and explained how it was built (at least how was supposed to be or &#8220;do as I tell you, don&#8217;t do&#160;as I do&#8221;).
Our O/R-mapper returns&#160;a generic Collection&#60;T&#62; when [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, we have gotten a couple of &#8220;fresh out of school&#8221; employees, and here the other day we went through the code of one of our applications and explained how it was built (at least how was supposed to be or &#8220;do as I tell you, don&#8217;t do&nbsp;as I do&#8221;).</p>
<p>Our O/R-mapper returns&nbsp;a generic Collection&lt;T&gt; when it is asked for a list / collection of some objects, and is consistent in doing so. The question however, was why that wasn&#8217;t a List&lt;T&gt;.</p>
<p>I had to admit I hadn&#8217;t dug into the material properly, so the question kindof got left there in open air. My bad excuse was of course I had been mostly doing managment stuff lately, and so was my companion presenter as well.</p>
<p>So to make a not so long story short, I had to dig into the collections that is found in .NET, and I explain them to myself for later reference, beginning with the aforementioned Collection&lt;T&gt; and List&lt;T&gt;.</p>
<h3>The difference</h3>
<p>Just to sum it up, Collection&lt;T&gt; is made for extensibility and List&lt;T&gt; is made for performance. See <a href="http://blogs.msdn.com/kcwalina/archive/2005/09/26/474010.aspx">this</a> and <a href="http://blogs.msdn.com/fxcop/archive/2006/04/27/585476.aspx">this</a>&nbsp;from the FxCop guys. </p>
<p>For a full blown explanation, a great <a href="http://blogs.msdn.com/kcwalina/archive/2005/09/23/Collections.aspx">reference is also found here</a>.</p>
<p><strong>Use List&lt;T&gt; for all your heavy lifting internally, and expose a Collection&lt;T&gt; in your public API.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/net-collectiont-vs-listt/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Allways qualify your insert statements</title>
		<link>http://www.haugern.net/blog/allways-qualify-your-insert-statements/</link>
		<comments>http://www.haugern.net/blog/allways-qualify-your-insert-statements/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 20:30:38 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[best practice]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/allways-qualify-your-insert-statements/</guid>
		<description><![CDATA[An absolute rule for your database insert scripts and stored procedures should be to qualify all field names.
An example could be the following schema:

Where you would have a script saying:
INSERT INTO FAMILY VALUES ('Bob','Dolly','Bill')

Then, you could see the disaster inserting a new field named Girlfriend in between You and your Mom (in more than one way [...]]]></description>
			<content:encoded><![CDATA[<p>An absolute rule for your database insert scripts and stored procedures should be to qualify all field names.</p>
<p>An example could be the following schema:</p>
<p><a atomicselection="true" href="http://www.haugern.net/blog/wp-content/windowslivewriterallwaysqualifyyourinsertstatements-c3d9image-1.png"><img border="0" width="276" src="http://www.haugern.net/blog/wp-content/windowslivewriterallwaysqualifyyourinsertstatements-c3d9image-thumb-1.png" alt="image" height="64" style="width: 276px; height: 64px; border: 0px" /></a></p>
<p>Where you would have a script saying:</p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px">INSERT <span style="color: #0000ff">INTO</span> FAMILY <span style="color: #0000ff">VALUES</span> (<span style="color: #006080">'Bob'</span>,<span style="color: #006080">'Dolly'</span>,<span style="color: #006080">'Bill'</span>)</pre>
<p><br/><br />
Then, you could see the disaster inserting a new field named Girlfriend in between You and your Mom (in more than one way <img src='http://www.haugern.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> ).</p>
<p>Then you have a schema like this:</p>
<p><a atomicselection="true" href="http://www.haugern.net/blog/wp-content/windowslivewriterallwaysqualifyyourinsertstatements-c3d9image-2.png"><img border="0" width="240" src="http://www.haugern.net/blog/wp-content/windowslivewriterallwaysqualifyyourinsertstatements-c3d9image-thumb-2.png" alt="image" height="76" style="border: 0px" /></a></p>
<p>Suddenly after your next insert your Girlfriend is your Mom, and your Dad is your Mom!</p>
<p>Well, the one solution to rectify this mess is of course guiding your values into the correct fields like this:</p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 76.86%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; height: 16px; background-color: white; border-style: none; padding: 0px">INSERT <span style="color: #0000ff">INTO</span> FAMILY (You,Girlfriend,Mom,Dad)  

<span style="color: #0000ff">VALUES</span> (<span style="color: #006080">'Bob'</span>,<span style="color: #006080">'Julia'</span>,<span style="color: #006080">'Dolly'</span>,<span style="color: #006080">'Bill'</span>)</pre>
<p><br/><br />
The rationale is then as follows:</p>
<ul>
<li>This will assure that if you have changed the schema ie. <strong>moved or inserted</strong> new fields, the script will <strong>not fail</strong> or most importantly <strong>it will not insert data in the wrong fields</strong>.</li>
<li>It <strong>will fail</strong> if you have <strong>removed or replaced</strong> fields which assures that you will <strong>not get corrupted data</strong> from these changes either.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/allways-qualify-your-insert-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
