<?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; oo principles</title>
	<atom:link href="http://www.haugern.net/blog/category/oo-principles/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>MSDN Live with Steve Ballmer keynote</title>
		<link>http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/</link>
		<comments>http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 19:05:04 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[conferences]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[oo principles]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/</guid>
		<description><![CDATA[MSDN Live has toured Norway this fall, with a handful of presentations for .NET developers. I attended the last one in Oslo on Tuesday, where Steve Ballmer made the keynote!
Most developers have seen the developers, developers, developers talk, and we had high expectations for this keynote. He was quite enthusiastic in Oslo as well, telling [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.haugern.net/blog/wp-content/windowslivewritermsdnlivewithsteveballmerkeynote-115e2ballmer-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="254" alt="ballmer" src="http://www.haugern.net/blog/wp-content/windowslivewritermsdnlivewithsteveballmerkeynote-115e2ballmer-thumb.jpg" width="263" align="left" border="0" /></a>MSDN Live has toured Norway this fall, with a handful of presentations for .NET developers. I attended the last one in Oslo on Tuesday, where Steve Ballmer made the keynote!</p>
<p>Most developers have seen the <a href="http://www.youtube.com/watch?v=KMU0tzLwhbE">developers, developers, developers</a> talk, and we had high expectations for this keynote. He was quite enthusiastic in Oslo as well, telling us about things he thinks will influence our industry in the foreseeable future. <a href="http://www.youtube.com/results?search_query=ballmer+oslo&amp;search_type=&amp;aq=f">This search at Youtube seems to find the whole show</a>.</p>
<p>The rest of the day I attended the main track, starting out with a Silverlight 2 presentation from <a href="http://blog.goeran.no/">G&#248;ran Hansen</a>. While Silverlight 1 was mostly about animations and media, I didn&#8217;t give much attention. But the second version looks promising, with the CLR and a subset of the .NET libraries present. What I miss though, is maybe some background about the fact that running .NET WinForms applications in IE in object-tags has been possible a long time (without the automatic deployment of the framework). Props however to MS for finally supporting more than Windows &amp; IE only! G&#248;ran did a great job presenting, I really enjoyed his presence on stage.</p>
<p>Next up was another presentation from G&#248;ran, which gave the audience an introduction to <a href="http://msdn.microsoft.com/en-us/library/cc488545.aspx">ASP.NET Dynamic Data</a>. For those of us who attended <a href="http://www.haugern.net/blog/norwegian-developer-conference-2008-review/">NDC2008</a> and <a href="http://www.hanselman.com/blog/">Scott Hanselmans</a> keynote there, it was plain repetition. The only difference might be that this was showing the final bits released with SP1 of the framework, and Scott showing us some earlier version. It seems like a great technology to get you up and running very quick, but I wonder how many pages you end up with in the CustomPage directory after a while. Even though the topic didn&#8217;t tickle my fancy, G&#248;ran did a great job on this one as well.</p>
<p>Then it was time for lunch, rather late according to my stomach. But it tasted good, and there seemed to be enough to fill up all us starving geeks. As always during the breaks, old colleagues and friends sneak up on you and says hello. Which I really do appreciate!</p>
<p>After the break, <a href="http://andersnoras.com/blogs/anoras/default.aspx">Anders Nor&#229;s</a> gave a talk about SOA. More specifically about how we should start thinking along the lines of <a href="http://en.wikipedia.org/wiki/Enterprise_Service_Bus">enterprise service buses</a> and leave our old crusty webservices in the dust. I really do concur with a lot his ideas, and I&#8217;m always happy to see code with binsor-love. I also liked his onion-architecture, which resembles my standard layered architecture (no, not the &quot;standard standard&quot;). Hmmm, looks like another blog post (again).</p>
<p>&quot;Debugging your debugging habits&quot; was up next with Rune Zakariassen from Microsoft. He showed us some tips &amp; tricks for your debugging needs in Visual Studio, and presented a step-by-step recipe on how you best can find bugs in your code. I thought it was a nice presentation, even though my only real takeaway was the tracepoint functionality in VS. I&#8217;ve been a sucker for Debug.Writeline when I don&#8217;t want to impact the run of things stepping through, but this gives me an even less intrusive way keeping track of how the program executes.</p>
<p>Finally, G&#248;ran entered the stage for the last time this day. He showed us how WPF databinding can be used with a variant of Model-View-Presenter called Presentation Model. The emphasis was really on this pattern, and how it helps attain <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">SoC</a>. He also mentioned <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">DIP</a> with the short version &quot;Program to an interface, not an implementation&quot;. A good thing can&#8217;t be said enough so here is the original:</p>
<blockquote><p>High level modules should not depend upon low level modules. Both should depend upon abstractions.</p>
<p>Abstractions should not depend upon details. Details should depend upon abstractions.</p>
</blockquote>
<p>All things considered, yet another great talk from G&#248;ran, keep up the good work and preach on about those design patterns and OO principles!</p>
<p>Yet again, great work by <a href="http://blogs.msdn.com/grothaug/default.aspx">Rune @ Microsoft</a> arranging this event. Until next time, I&#8217;m on twitter at <a href="http://twitter.com/haugern">http://twitter.com/haugern</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/feed/</wfw:commentRss>
		<slash:comments>1</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>
	</channel>
</rss>
