<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>CodeBetter.Com - Stuff you need to Code Better!</title><link>http://codebetter.com/blogs/</link><description>Stuff you need to code better!</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/CodeBetter" type="application/rss+xml" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><item><title>Meffifying Windows Azure</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/w1fZWNy29I0/meffifying-windows-azure.aspx</link><pubDate>Fri, 03 Jul 2009 17:09:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249654</guid><dc:creator>Glenn Block</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;&lt;a href="http://twitter.com/noopman"&gt;Magnus&lt;/a&gt; has been off doing some interesting work around integrating &lt;a href="http://mef.codeplex.com"&gt;MEF&lt;/a&gt; with &lt;a href="http://www.microsoft.com/azure"&gt;Windows Azure&lt;/a&gt;. The first question you might be asking is Why?&lt;/p&gt;
&lt;p&gt;In his words, he set out to build a template for Windows Azure templates that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;enables testability &lt;/li&gt;
&lt;li&gt;abstracts away storage&lt;/li&gt;
&lt;li&gt;is extensible and easy to evolve during development &lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
&lt;p&gt;In the post he shows how to take the RoleManger and expose it through MEF, thereby making it pluggable. He then creates a mock Role Manager for use in his unit tests, thus removing the dependency on all the Azure infrastructure.&lt;/p&gt;
&lt;p&gt;I am guessing this is the first of many posts to come on MEF and Azure. &lt;/p&gt;
&lt;p&gt;For more, check out Magnus post &lt;a href="http://blog.noop.se/archive/2009/07/03/windows-azure-plus-managed-extensibility-framework-mef-true.aspx"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/li&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249654" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/w1fZWNy29I0" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/MEF/default.aspx">MEF</category><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/azure/default.aspx">azure</category><feedburner:origLink>http://codebetter.com/blogs/glenn.block/archive/2009/07/03/meffifying-windows-azure.aspx</feedburner:origLink></item><item><title>Move The Chair</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/S9MkS1TEqa8/move-the-chair.aspx</link><pubDate>Fri, 03 Jul 2009 14:17:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249650</guid><dc:creator>Rod Paddock [MVP]</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rodpaddock/IMG_5F00_0133_5F00_3CAC13AC.jpg"&gt;&lt;img border="0" align="left" width="285" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rodpaddock/IMG_5F00_0133_5F00_thumb_5F00_536DEBE6.jpg" alt="IMG_0133" height="379" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;margin:0px 10px 0px 0px;border-right-width:0px;" title="IMG_0133" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Last Sunday I took my family to Red Robin (a family style restaurant) after church. I requested a table with a high chair for my&amp;nbsp; 3.5 yr old son. Upon bringing this high chair the hostess simply moved an adult chair to the side and put the high chair in its place. (See Image)&lt;/p&gt;
&lt;p&gt;This is where the fun began&amp;hellip;&lt;/p&gt;
&lt;p&gt;For the next hour or so I watched countless customers, servers and managers limbo their way between these two chairs. A lot of them did it repeatedly, some even carrying trays full of drinks and/or food. Finally I provided evidence to to my family that I am 100% full on geek. I got excited about the insight that this little experience game me (to the point of taking pictures)&lt;/p&gt;
&lt;p&gt;I was simply amazed that people would allow this &amp;ldquo;friction to continue&amp;rdquo; It was a simple problem in this case. Move the chair!&lt;/p&gt;
&lt;p&gt;I find it interesting that it&amp;rsquo;s built into our DNA incur friction in our lives without really addressing it. In real life its called denial. I know the chair was not that big a deal but it did open my eyes to our basic nature. Our basic nature follows through to software development as well.&lt;/p&gt;
&lt;p&gt;The example of the misplaced chair provides some interesting parallels when it comes to technical debt (which is a cause of software development friction) We all have technical debt in our applications and it&amp;rsquo;s in our nature to shimmy, shuffle, limbo, mambo, and shake around these issues&amp;nbsp; whenever possible. It takes a constant vigilance to address technical debt. We must be active in our quest to remove technical debt. We must take the time to do two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Recognize the debt. In our case: The chair. &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Remove the debt: In our case: Move the chair. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;rsquo;s it for this blog post. Just a simple observation brought to you by the staff and management of Red Robin :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249650" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/S9MkS1TEqa8" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/rodpaddock/archive/2009/07/03/move-the-chair.aspx</feedburner:origLink></item><item><title>HandleUnknownAction in ASP.NET MVC – Be Careful</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/Y50jJsbe3vk/handleunknownaction-in-asp-net-mvc-be-careful.aspx</link><pubDate>Thu, 02 Jul 2009 20:19:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249568</guid><dc:creator>David Hayden</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;One of my team members was showing me some code in HandleUnknownAction based upon sample code in the &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.handleunknownaction.aspx"&gt;online documentation&lt;/a&gt;. Per the documentation:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;The following example shows how to render views that do not have a matching ActionResult method. For example, if you have a Details.aspx view but no corresponding method exists that returns an ActionResult instance, the following example displays the Details view when a request to for the Details action is made on the controller. If there is no matching view, the error page displays a message.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="font-size:9pt;color:black;font-family:verdana;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#400080;"&gt;protected&lt;/span&gt; &lt;span style="color:#400080;"&gt;override&lt;/span&gt; &lt;span style="color:#400080;"&gt;void&lt;/span&gt; &lt;span style="color:#010001;"&gt;HandleUnknownAction&lt;/span&gt;(&lt;span style="color:#400080;"&gt;string&lt;/span&gt; &lt;span style="color:#010001;"&gt;actionName&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#400080;"&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#010001;"&gt;View&lt;/span&gt;(&lt;span style="color:#010001;"&gt;actionName&lt;/span&gt;)&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;ExecuteResult&lt;/span&gt;(&lt;span style="color:#010001;"&gt;ControllerContext&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#400080;"&gt;catch&lt;/span&gt; (&lt;span style="color:#305fb6;"&gt;InvalidOperationException&lt;/span&gt; &lt;span style="color:#010001;"&gt;ieox&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#010001;"&gt;ViewData&lt;/span&gt;[&lt;span style="font-weight:bold;color:#259241;"&gt;&amp;quot;error&amp;quot;&lt;/span&gt;] &lt;span style="color:#2e53d1;"&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;color:#259241;"&gt;&amp;quot;Unknown Action: \&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#2e53d1;"&gt;+&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#010001;"&gt;Server&lt;/span&gt;&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;HtmlEncode&lt;/span&gt;(&lt;span style="color:#010001;"&gt;actionName&lt;/span&gt;) &lt;span style="color:#2e53d1;"&gt;+&lt;/span&gt; &lt;span style="font-weight:bold;color:#259241;"&gt;&amp;quot;\&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#010001;"&gt;ViewData&lt;/span&gt;[&lt;span style="font-weight:bold;color:#259241;"&gt;&amp;quot;exMessage&amp;quot;&lt;/span&gt;] &lt;span style="color:#2e53d1;"&gt;=&lt;/span&gt; &lt;span style="color:#010001;"&gt;ieox&lt;/span&gt;&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;Message&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#400080;"&gt;this&lt;/span&gt;&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;View&lt;/span&gt;(&lt;span style="font-weight:bold;color:#259241;"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;)&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;ExecuteResult&lt;/span&gt;(&lt;span style="color:#400080;"&gt;this&lt;/span&gt;&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;ControllerContext&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The only problem with this example is that it does not take into account attributes, like the &lt;strong&gt;ActionMethodSelectorAttribute&lt;/strong&gt;, that can decide based on runtime information whether an action can fulfill a request. So, it may not be that the action does not exist, but that it is not supposed to respond to the request based on the context of the request.&lt;/p&gt;
&lt;p&gt;For example, if I use the AcceptVerbsAttribute, an example of ActionMethodSelectorAttribute, to do something as simple as this on my HomeController:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="font-size:9pt;color:black;font-family:verdana;"&gt;
&lt;p style="margin:0px;"&gt;[&lt;span style="color:#305fb6;"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;HttpVerbs&lt;/span&gt;&lt;span style="color:#2e53d1;"&gt;.&lt;/span&gt;&lt;span style="color:#010001;"&gt;Post&lt;/span&gt;)]&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#400080;"&gt;public&lt;/span&gt; &lt;span style="color:#305fb6;"&gt;ActionResult&lt;/span&gt; &lt;span style="color:#010001;"&gt;DisplayOnlyViaPost&lt;/span&gt;()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#400080;"&gt;return&lt;/span&gt; &lt;span style="color:#010001;"&gt;View&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The DisplayOnlyViaPost View will not be displayed via the DisplayOnlyViaPost Action when someone types the URL in a browser ( HTTP GET Request ) &amp;ndash; http://&amp;hellip;/Home/DisplayOnlyViaPost.&lt;/p&gt;
&lt;p&gt;Unfortunately, the HandleUnknownAction code above will kick in and notice that indeed the DisplayOnlyViaPost View does exist. Therefore it goes ahead and displays the view, undermining the use of our AcceptVerbsAttribute on the original action. Not good :)&lt;/p&gt;
&lt;p&gt;Just something I noticed that you may want to be careful of if you are using similar code in HandleUnknownAction.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://www.davidhayden.com/"&gt;David Hayden&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249568" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/Y50jJsbe3vk" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/david.hayden/archive/2009/07/02/handleunknownaction-in-asp-net-mvc-be-careful.aspx</feedburner:origLink></item><item><title>ASP.NET MVC Extensibility at Tampa MVC Developer Group</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/SfXPwZqtKEM/asp-net-mvc-extensibility-at-tampa-mvc-developer-group.aspx</link><pubDate>Wed, 01 Jul 2009 21:09:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249524</guid><dc:creator>David Hayden</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;The &lt;a href="http://www.tampadev.org/" target="_blank"&gt;Tampa ASP.NET MVC Developer Group&lt;/a&gt; is starting to kick it up a notch with this month&amp;rsquo;s meeting being about &lt;a href="http://www.tampadev.org/Meetings/Details/AspNetMvcExtensibility" target="_blank"&gt;ASP.NET MVC Extensibility&lt;/a&gt;. We have concluded our introductory topics and are now focusing on more intermediate topics. This month&amp;rsquo;s meeting will hit more advanced topics, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Creating Custom Controller Factories for Dependency Injection &lt;/li&gt;
&lt;li&gt;Using Custom View Engines &lt;/li&gt;
&lt;li&gt;Creating Custom ActionFilters for DataBinding, Validation, etc. &lt;/li&gt;
&lt;li&gt;Using Custom ControllerActionInvokers &lt;/li&gt;
&lt;li&gt;Developing Custom ActionResults for RSS Feeds, etc. &lt;/li&gt;
&lt;li&gt;Building HtmlHelpers for Productivity &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you are interested in building your own opinionated version of the ASP.NET MVC Framework and have it bend to your will, you don&amp;rsquo;t want to miss &lt;a href="http://www.tampadev.org/Meetings/Details/AspNetMvcExtensibility" target="_blank"&gt;this meeting&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In addition, we will also be going over last month&amp;rsquo;s homework assignment, which was to use the ASP.NET MVC AjaxHelpers to &amp;ldquo;ajaxify&amp;rdquo; an ASP.NET MVC Web Application. As a hint, you can find an answer in our last screencast, &lt;a href="http://www.tampadev.org/News/Details/MvcAjaxHelpersScreencast"&gt;ASP.NET MVC Ajax Helpers - Ajax.ActionLink and Ajax.BeginForm &amp;ndash; Screencast&lt;/a&gt; :)&lt;/p&gt;
&lt;p&gt;Those completing the homework assignment will be included in a random drawing of Steven Sanderson&amp;rsquo;s Awesome book, &lt;a href="http://davidhayden.com/blog/dave/archive/2009/05/11/ProASPNETMVCFrameworkBookReview.aspx" target="_blank"&gt;Pro ASP.NET MVC Framework&lt;/a&gt;. Absolutely love Steve&amp;rsquo;s book. Huge thanks to &lt;a href="http://www.apress.com/" target="_blank"&gt;Apress&lt;/a&gt; for sending us several copies to give away to our members.&lt;/p&gt;
&lt;p&gt;Starting in August we will be working on voluntary group projects to strengthen our team development skills using ASP.NET MVC. Again, huge thanks to two of our sponsors for making this happen: &lt;a href="http://serverintellect.com/" target="_blank"&gt;Server Intellect&lt;/a&gt; and &lt;a href="http://unfuddle.com/" target="_blank"&gt;Unfuddle&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;See you on Thursday, July 9 at the Microsoft Office in Tampa!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.davidhayden.com/" target="_blank"&gt;David Hayden&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249524" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/SfXPwZqtKEM" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/david.hayden/archive/2009/07/01/asp-net-mvc-extensibility-at-tampa-mvc-developer-group.aspx</feedburner:origLink></item><item><title>The Videos for NDC 2009 are online</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/ZFfY12crp5U/the-videos-for-ndc-2009-are-online.aspx</link><pubDate>Wed, 01 Jul 2009 16:16:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249515</guid><dc:creator>Jeremy D. Miller</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;The links to the videos recorded for NDC 2009 talks are online and linked in &lt;a href="http://www.ndc2009.no/agenda.aspx?cat=1071&amp;amp;id=1813&amp;amp;day=3726"&gt;the agenda pages here&lt;/a&gt;.&amp;nbsp; My talks are at:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://media01.smartcom.no/Microsite/start.aspx?eventid=4484"&gt;Lessons Learned from a Long Lived Codebase&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://media01.smartcom.no/Microsite/start.aspx?eventid=4464"&gt;Presentation Patterns for Composite Applications&lt;/a&gt; (I&amp;#39;d dearly appreciate feedback from folks on this talk)&lt;/li&gt;
&lt;li&gt;&lt;a href="http://media01.smartcom.no/Microsite/start.aspx?eventid=4532"&gt;Convention over Configuration applied to .NET&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://media01.smartcom.no/Microsite/start.aspx?eventid=4528"&gt;Software Design and Testability&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249515" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/ZFfY12crp5U" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/jeremy.miller/archive/2009/07/01/the-videos-for-ndc-2009-are-online.aspx</feedburner:origLink></item><item><title>Reporting against a domain model</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/AMUTdDPA-ys/reporting-against-a-domain-model.aspx</link><pubDate>Wed, 01 Jul 2009 08:44:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249499</guid><dc:creator>pvanooijen</dc:creator><slash:comments>10</slash:comments><description>&lt;p&gt;I have a love it /hate it relationship with reports. In many an application the reports are the main output. They are the prints which get filed or the invoices which have to ensure the ROI of the application. A good looking output rich with information does increase customer satisfaction. The downside is that fine tuning a report is tedious and requires quite different skills than coding. Building a system with a lot of reports can be pretty boring. So the tools better be good. &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2005/07/21/129605.aspx"&gt;Switching from Crystal Reports to sql server&amp;nbsp; reporting services&lt;/a&gt; (RS) was quite a relief. &lt;/p&gt;
&lt;p&gt;But&amp;nbsp; RS still has two drawbacks. First is that it accesses data using plain sql. My application is built around a domain model using nHibernate to hide all db details. The domain model includes calculated properties which are to be included in the report. The calculations are done in the domain object. It would be repetitive and prone to error to repeat them in sql or reporting expressions. It would be quite a relief to report straight from the domain model itself. &lt;/p&gt;
&lt;p&gt;The other drawback is that RS requires an ms sql server instance with installed reporting services. The actual data is just a sqlexpress instance and I don&amp;rsquo;t want to set up a full blown reporting services server.&lt;/p&gt;
&lt;p&gt;New in Visual Studio 2005 were client side reports (rdlc&amp;rsquo;s). They are very much like RS reports (rdl&amp;rsquo;s) but can work with any data source and don&amp;rsquo;t require a sql reporting server instance. To include these reports in an app two client side report viewers are include in the .NET framework, one for WinForms and one for asp.net. This looked like the best of both worlds so I decided to give the winforms version a try. &lt;/p&gt;
&lt;p&gt;For a good overview on the reports check the &lt;a href="http://www.gotreportviewer.com/"&gt;got reportviewer&lt;/a&gt; site. In name it supports VS 2008, but (some of ?) the samples are based on VS 2005. Moving to 2008 there has been a change in the namespace for the classes, after fixing the reference the samples run in VS 2008. How to work with a non sql datasource is covered but not that clear. Here I will describe my minimalistic approach to work with a domain model fed by nHibernate.&lt;/p&gt;
&lt;h3&gt;The domain model&lt;/h3&gt;
&lt;p&gt;The intent of the application is to print invoices. This class diagram describes the model&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep1_5F00_709A0A5B.png"&gt;&lt;img height="575" width="490" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep1_5F00_thumb_5F00_0E981850.png" alt="Rep1" border="0" title="Rep1" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;An invoice is for a contact, has a number of lines and an enumerated property. That is a number of values which contain a tax rate and have a clear name describing this value. Which gives the model additional value, something which would get lost when viewing the data as raw db data. An invoice has a reference to a contact and many Invoice lines. The (&lt;a href="http://wiki.fluentnhibernate.org/show/HomePage"&gt;fluent&lt;/a&gt;) nHibernate mappings express this.&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvoiceMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;ClassMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Invoice&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvoiceMap()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;Factuur&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Number).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;Nummer&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.OnDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;Dedato&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.BtwPercentage).CustomTypeIs&amp;lt;&lt;span style="color:#2b91af;"&gt;BtwPercentage&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Description).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;Inleiding&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasMany(x =&amp;gt; x.Lines).KeyColumnNames.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;idFactuur&amp;quot;&lt;/span&gt;).Cascade.All();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; References(x =&amp;gt; x.ForContact).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;idRelatie&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PrintIt).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;Printen&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;So we have a &lt;em&gt;customtype&lt;/em&gt;, a &lt;em&gt;hasmany &lt;/em&gt;and a &lt;em&gt;references&lt;/em&gt;. Let&amp;rsquo;s see how these are used in the report.&lt;/p&gt;
&lt;h3&gt;The report&lt;/h3&gt;
&lt;p&gt;There are several ways to include a report in an application. The default when adding&amp;nbsp; a new report (Add &amp;ndash;&amp;gt; New Item &amp;ndash;&amp;gt; Reporting &amp;ndash;&amp;gt; report) is embedded. Which is clear and easy in deployment. When you need more flexibility you can switch to rdlc file or (back to) server side report.&lt;/p&gt;
&lt;p&gt;The report needs a datasource. It will accept any datasource including an object datasource. &lt;/p&gt;
&lt;p&gt;Add a new object datasource to the project and pick the Invoice domain object. &lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep2_5F00_320496E8.png"&gt;&lt;img height="355" width="462" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep2_5F00_thumb_5F00_285C28B2.png" alt="Rep2" border="0" title="Rep2" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Just pick your POCO domain class. There is no need to set up any methods to get data, it is enough to have a model of the data.&lt;/p&gt;
&lt;p&gt;This object data source is the datasource for your report. Having done so the domain invoice is available in the report designer. (To be precise the datasource has to be attached to a List, see the &lt;a href="http://www.gotreportviewer.com/lists/index.html"&gt;reportviewer docs&lt;/a&gt; for more information on that.)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep3_5F00_3B30AC5C.png"&gt;&lt;img height="290" width="478" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep3_5F00_thumb_5F00_58565466.png" alt="Rep3" border="0" title="Rep3" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;A report is built from VB-like expressions. A simple one looks&amp;nbsp; like this:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;=Fields!Description.Value&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Which displays the plain content of a property. But expressions can be more complicated. The report designer has a good expression builder which even includes intellisense. The next expression uses both the meaningful name and the value of my tax enumeration property in a string&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;=&lt;span style="color:#a31515;"&gt;&amp;quot;BTW &amp;quot;&lt;/span&gt; &amp;amp; Fields!BtwPercentage.Value.ToString &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt;&amp;nbsp; &amp;amp; Fields!BtwPercentage.Value &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;%&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The result is a string like &amp;ldquo;&lt;em&gt;BTW Hoog 19%&amp;rdquo;.&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;Things get really interesting when displaying the contact information. In the domain model this expressed as&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;invoice.ForContact.AddressLine1&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;In the report the expression is quite similar&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;=Fields!ForContact.Value.AddressLine1&lt;/p&gt;
&lt;/div&gt;

&lt;h3&gt;The subreport&lt;/h3&gt;
&lt;p&gt;The lines in the invoice are a one to many relation. The invoice datasource has a &lt;em&gt;lines&lt;/em&gt; property but this property is not usable in the report. To display the lines I need a subreport. The subreport is based on invoice lines. After adding the &lt;em&gt;InvoiceLine&lt;/em&gt; class as a datasource the subreport is drawn. It is a simple table displaying the lines.&lt;/p&gt;
&lt;p&gt;A subreport is linked to its parent report using parameters. In my case an invoice and its lines are linked through a parameter named &lt;em&gt;FactuurNummer&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep4_5F00_4F1A1925.png"&gt;&lt;img height="400" width="520" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep4_5F00_thumb_5F00_33951722.png" alt="Rep4" border="0" title="Rep4" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;All of this works just as in &amp;ldquo;traditional&amp;rdquo; reports. In the main invoice report this parameter is assigned the value of the invoice number&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep5_5F00_2AC50ED6.png"&gt;&lt;img height="362" width="513" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep5_5F00_thumb_5F00_694A5C7C.png" alt="Rep5" border="0" title="Rep5" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Check the &lt;a href="http://www.gotreportviewer.com/masterdetail/index.html"&gt;gotreportviewer&lt;/a&gt; site for further information.&lt;/p&gt;
&lt;h3&gt;&lt;/h3&gt;
&lt;h3&gt;&lt;/h3&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Binding the data to the report&lt;/h3&gt;
&lt;p&gt;Now the reports are ready. The next step is to get some real life data into them. &lt;/p&gt;
&lt;p&gt;It takes code to bind real data to a report and display the result in the viewer. Also the subreport has to be fed with data. To wrap things up I have built a helper class which takes a reportviewer component and a list of invoices and wires them up.&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&lt;span style="color:blue;"&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvoiceReporter&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;internal&lt;/span&gt; InvoiceReporter(&lt;span style="color:#2b91af;"&gt;ReportViewer&lt;/span&gt; viewer, &lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Invoice&lt;/span&gt;&amp;gt; reportData)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _reportData = reportData;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _viewer = viewer;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Invoice&lt;/span&gt;&amp;gt; _reportData;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReportViewer&lt;/span&gt; _viewer;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; ShowReport()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _viewer.LocalReport.ReportEmbeddedResource = &lt;span style="color:#a31515;"&gt;&amp;quot;Gekko.Administratie.FactuurPrinten.Invoice.rdlc&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _viewer.LocalReport.DataSources.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReportDataSource&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Gekko_Administratie_DomainModel_Invoice&amp;quot;&lt;/span&gt;, _reportData));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _viewer.LocalReport.SubreportProcessing += SubreportProcessing;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _viewer.RefreshReport();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SubreportProcessing(&lt;span style="color:blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af;"&gt;SubreportProcessingEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; invoiceNumber = e.Parameters[&lt;span style="color:#a31515;"&gt;&amp;quot;FactuurNummer&amp;quot;&lt;/span&gt;].Values[0];&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; invoice = _reportData.First(fakt =&amp;gt; fakt.Number == invoiceNumber);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.DataSources.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReportDataSource&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Gekko_Administratie_DomainModel_InvoiceLine&amp;quot;&lt;/span&gt;, invoice.Lines));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;The code requires some explanation. The constructor should be clear, it receives viewer and data.&lt;/p&gt;
&lt;p&gt;The ShowReport method does the real work. The first line loads the report in the viewer. The second line loads the report data. This line is tricky. The name of the &lt;em&gt;ReportDataSource&lt;/em&gt; &lt;strong&gt;has&lt;/strong&gt; to match exactly the name of the datasource class used as model. The Invoice class name is &lt;em&gt;Gekko.Administratie.DomainModel.Invoice&lt;/em&gt; which gets formatted as &lt;em&gt;Gekko_Administratie_DomainModel_Invoice&lt;/em&gt;. This required magic string is not clear from any documentation, it took me quite some time to find out how essential it was. &lt;/p&gt;
&lt;p&gt;All these datasource properties in the report definition are confusing. The model of the data is saved in the report definition. Changing the datasource&amp;nbsp; to another &amp;ldquo;dataset&amp;rdquo; will also lead to errors at run time. The report will complain about the former datasources. To get rid of the error messages you have to check the report defintion by hand. The report definition file (.rdlc) is plain xml. The last part contains dataset definitions. What helps is just deleting the unused nodes by hand.&lt;/p&gt;
&lt;p&gt;Upon rendering a subreport an event is fired. In the third line of &lt;em&gt;ShowReport&lt;/em&gt; a handler is attached. This &lt;em&gt;SubReportProcessing&lt;/em&gt; method feeds the subreport with data. From the parameters the number of the invoice is read. Which is used to find the corresponding invoice. The datasource for the subreport is set just like the datasource of the main report. Again spelling the name of the reportdatasource right is essential. The data is the Lines property. Which is an IList of InvoiceLines.&lt;/p&gt;
&lt;h3&gt;&lt;/h3&gt;
&lt;h3&gt;Putting it all together&lt;/h3&gt;
&lt;p&gt;With all pieces in place it&amp;rsquo;s time to get some results. I have a windows form with a reportviewer on it. &lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; repo = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvoiceRepository&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; reporter = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvoiceReporter&lt;/span&gt;(reportViewer1, repo.ListPrintableInvoices());&lt;/p&gt;
&lt;p style="margin:0px;"&gt;reporter.ShowReport();&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;This reads the invoices from the repository and sends them with the reportviewer to the helper class.&lt;/p&gt;
&lt;p&gt;There is one more thing to watch. The repository is an nHibernate repository. By default nHibernate lazy loads related objects, like the &lt;em&gt;Contact&lt;/em&gt; and the &lt;em&gt;Lines&lt;/em&gt; of the &lt;em&gt;Invoice&lt;/em&gt;. The contact and the invoicelines will not be read from the database until they are actually used. This default setting has some serious drawbacks. &lt;/p&gt;
&lt;p&gt;In the first place the reportviewer&amp;lsquo;s datareader does not operate very well (at all ?) with nHibernate&amp;rsquo;s proxies. In the report all the contact fields will contain error messages. The subreport on the&amp;nbsp; lines will work. But still the lines are not read from the database until rendering the sub-report. So for this lazy loading to work the report has to be run against a dataset with an open dataconnection. That&amp;rsquo;s bad, I try do do as little as possible, and most likely not presenting a report, with an open connection. A far better scenario would be to fetch all data in one go, close the connection to the database and render the report after that. This can be done by eagerly loading the Contact and Lines properties and disposing of the session before returning the data.&lt;/p&gt;
&lt;p&gt;In the nHibernate API the fetch mode can be set on a property level. The code in the repository takes care of fetching all data in one big snapshot. &lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&lt;span style="color:blue;"&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Invoice&lt;/span&gt;&amp;gt; ListPrintableInvoices()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;using&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; session = &lt;span style="color:#2b91af;"&gt;SessionFactory&lt;/span&gt;.GetFactory.OpenSession())&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = session.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Invoice&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.AddOrder(&lt;span style="color:#2b91af;"&gt;Order&lt;/span&gt;.Asc(&lt;span style="color:#a31515;"&gt;&amp;quot;Number&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;PrintIt&amp;quot;&lt;/span&gt;, &lt;span style="color:blue;"&gt;true&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetFetchMode(&lt;span style="color:#a31515;"&gt;&amp;quot;Lines&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;FetchMode&lt;/span&gt;.Eager);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetFetchMode(&lt;span style="color:#a31515;"&gt;&amp;quot;ForContact&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;FetchMode&lt;/span&gt;.Eager);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.List&amp;lt;&lt;span style="color:#2b91af;"&gt;Invoice&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p style="margin:0px;"&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;The repository reads all data from the database and closes the connection. After that the collected data are input to the reportviewer. Which now works completely as hoped for. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep6_5F00_405F4773.png"&gt;&lt;img height="531" width="408" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Rep6_5F00_thumb_5F00_3384F155.png" alt="Rep6" border="0" title="Rep6" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have my report complete with printing and export functionality and am still working with my beloved domain objects.&lt;/p&gt;
&lt;p&gt;This way reporting can almost be pure fun.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249499" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/AMUTdDPA-ys" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/peter.van.ooijen/archive/tags/Data/default.aspx">Data</category><feedburner:origLink>http://codebetter.com/blogs/peter.van.ooijen/archive/2009/07/01/reporting-against-a-domain-model.aspx</feedburner:origLink></item><item><title>Check out Virtual ALT.NET</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/TUTl95ydjzw/check-out-virtual-alt-net.aspx</link><pubDate>Tue, 30 Jun 2009 23:14:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249472</guid><dc:creator>aaronjensen</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;Several months ago a few people started hosting a &lt;a href="http://www.zachariahyoung.com/zy/post/2009/01/Introduction-to-Virtual-ALTNET.aspx"&gt;Virtual ALT.NET&lt;/a&gt; meeting right here on the information super highway. I&amp;#39;ve been to a few and they&amp;#39;re always packed with information and good conversation. It&amp;#39;s a great way to learn new things or show off cool stuff you&amp;#39;ve been working on. If you haven&amp;#39;t been to one, I&amp;#39;d recommend you check one out. There are two great looking meetings hosted by Ryan Svihla coming up soon:&lt;/p&gt;  &lt;p&gt;Development with Castle Project with Ryan Svihla July 1 and July 8   &lt;br /&gt;Ryan will be doing a two part series on the Castle Project.&amp;#160; Mark your calendar for some Castle Project fun.    &lt;br /&gt;Ryan Svihla has been working as a C# developer Farm Bureau Bank in San Antonio since September 2007. Before that he worked as&amp;#160; a Consultant in Lincoln, NE for 3 years, where he had working experience with Php, some Perl, Python and of course C#.&amp;#160; Attemping Agile since early 2008 as an eager student with a focus on making programming more useful and relevant for the end user.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;IoC and Dip through Castle Windsor&lt;/b&gt;    &lt;br /&gt;Ever wonder what acronyms like IoC and Dip mean? If you know what they mean do you wonder why anyone would use them in code?&amp;#160; This talk aims to deal primarily with those two questions through the use of Castle Windsor IoC container.&amp;#160; Intermediate level C# material with a couple of more advanced demos at the end for fun and pleasure.    &lt;br /&gt;Central Daylight Time    &lt;br /&gt;Start Time: Web, July 1, 2009 8:00 PM UTC/GMT -5 hours    &lt;br /&gt;End Time: Web, July 1, 2009 10:00 PM UTC/GMT -5 hours    &lt;br /&gt;Attendee URL: &lt;a href="http://snipr.com/virtualaltnet"&gt;http://snipr.com/virtualaltnet&lt;/a&gt; (Live Meeting) &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;&lt;b&gt;Web Development with Castle Monorail, Active Record and Brail view engine&lt;/b&gt;    &lt;br /&gt;Have a look at the first popular MVC .Net based web framework. Also will be covering persistance with ActiveRecord, and view templates using Brail.&amp;#160; Bonus, will demo a plugin framework for building CMS like applications.    &lt;br /&gt;Central Daylight Time    &lt;br /&gt;Start Time: Web, July 8, 2009 8:00 PM UTC/GMT -5 hours    &lt;br /&gt;End Time: Web, July 8, 2009 10:00 PM UTC/GMT -5 hours    &lt;br /&gt;Attendee URL: &lt;a href="http://snipr.com/virtualaltnet"&gt;http://snipr.com/virtualaltnet&lt;/a&gt; (Live Meeting)&lt;/p&gt;  &lt;p&gt;Hope to see you all there!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249472" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/TUTl95ydjzw" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/alt.net/default.aspx">alt.net</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/van/default.aspx">van</category><feedburner:origLink>http://codebetter.com/blogs/aaron.jensen/archive/2009/06/30/check-out-virtual-alt-net.aspx</feedburner:origLink></item><item><title>Making your code easier to understand context/specification style unit tests</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/UdyMF2V3RKc/making-your-code-easier-to-understand-context-specification-style-unit-tests.aspx</link><pubDate>Mon, 29 Jun 2009 04:08:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249314</guid><dc:creator>Ian Cooper</dc:creator><slash:comments>4</slash:comments><description>&lt;p&gt;When we started our current project we did not use &lt;a href="http://stevenharman.net/blog/archive/2009/05/27/toward-a-better-use-of-context-specification.aspx"&gt;context/specification style testing&lt;/a&gt;, instead we used &lt;a href="http://xunitpatterns.com/Testcase%20Class%20per%20Class.html"&gt;testcase-per-class&lt;/a&gt; with a &lt;a href="http://xunitpatterns.com/Four%20Phase%20Test.html"&gt;four-phase test model&lt;/a&gt; (also known as &lt;a href="http://c2.com/cgi/wiki?ArrangeActAssert"&gt;arrange-act-assert&lt;/a&gt;). Although we followed &lt;a href="http://codebetter.com/blogs/ian_cooper/archive/2009/03/31/seizing-the-bdd-nettle.aspx"&gt;story-test driven development&lt;/a&gt; (STDD) we were not explicitly Behavior-Driven Development &lt;a href="http://dannorth.net/introducing-bdd"&gt;BDD &lt;/a&gt;when we set out. Over time I began to see the path between STDD and BDD we shifted toward a BDD approach.&lt;/p&gt;
&lt;p&gt;That is really background for what comes next. As part of this shift we started writing more context/specification style tests. We did not re-write our existing testcase-per-class fixtures (then and still now I would see the possibility of using multiple &lt;a href="http://xunitpatterns.com/Test%20Organization.html"&gt;test organisational patterns&lt;/a&gt; within a project; some of our tests still seem better with testcase-per-class). Recently we have had new developers join the team. One interesting, if anecdotal, observation has been that those developers find it easier to use tests as a source of documentation for what the software is doing when they are in the context/specification style. This is a useful observation, because the rest of the team is able to supply much of the context for testcase-per-class fixtures by virtue of having worked on the codebase and so do not see the lack so easily.&lt;/p&gt;
&lt;p&gt;To build a maintainable system you have to build one where the behavior can be understood. Tests offer this promise, but often we have found that tests failed us, because the tests were no easier to comprehend than the code under test. Context/specification style tests seem to be better on this account. Of course there is no silver bullet in software. You still have to put the effort into making your context/specification tests readable, but the form seems helpful toward achieving that goal.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll try to post more on my experiences with BDD style approaches as they come out. I would also recommend that anyone interested in the topic look at the&lt;a href="http://www.pragprog.com/titles/achbd/the-rspec-book"&gt; RSpec book&lt;/a&gt; from the Pragmatic Programmers which offers a great overview of the topic.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249314" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/UdyMF2V3RKc" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/ian_cooper/archive/tags/TDD/default.aspx">TDD</category><category domain="http://codebetter.com/blogs/ian_cooper/archive/tags/xUnit/default.aspx">xUnit</category><category domain="http://codebetter.com/blogs/ian_cooper/archive/tags/BDD/default.aspx">BDD</category><category domain="http://codebetter.com/blogs/ian_cooper/archive/tags/Behavior+Specification/default.aspx">Behavior Specification</category><category domain="http://codebetter.com/blogs/ian_cooper/archive/tags/ATDD/default.aspx">ATDD</category><category domain="http://codebetter.com/blogs/ian_cooper/archive/tags/STDD/default.aspx">STDD</category><feedburner:origLink>http://codebetter.com/blogs/ian_cooper/archive/2009/06/29/making-your-code-easier-to-understand-context-specification-style-unit-tests.aspx</feedburner:origLink></item><item><title>Fighting Fabricated Complexity</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/kHoLTk9Dj64/fighting-fabricated-complexity.aspx</link><pubDate>Sun, 28 Jun 2009 10:12:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249379</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>4</slash:comments><description>&lt;p&gt;






 
  
  
 




 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 





&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;During my interview on Code Metrics by
&lt;a target="_blank" href="http://www.hanselman.com/blog/HanselminutesPodcast163SoftwareMetricsWithPatrickSmacchia.aspx"&gt;Scott Hanselman&amp;rsquo;s on Software Metrics&lt;/a&gt;&lt;/span&gt;&lt;a href="http://www.hanselman.com/blog/HanselminutesPodcast163SoftwareMetricsWithPatrickSmacchia.aspx"&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/a&gt;&lt;span lang="EN-US"&gt;, Scott had a particularly relevant
remark.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Basically, while I was
explaining that long and complex methods are killing quality and should be
split into smaller methods, Scott asked me: &lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;padding-left:30px;"&gt;&lt;span lang="EN-US"&gt;&lt;i&gt;looking
at this big too complicated method and I break it up into smaller methods, &lt;b&gt;the complexity of the business problem is
still there&lt;/b&gt;, looking at my application I can say, this is no longer complex
from the method perspective, but the software itself, the way it is coupled
with other bits of code, may indicate other problem&amp;hellip;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Software complexity is a
subjective measure relative to the human cognition capacity. Something is
complex when it requires effort to be understood by a human. The fact is that
software complexity is a 2 dimensional measure. To understand a piece of code
one must understand both:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span lang="EN-US"&gt;what this piece of
code is supposed to do at run-time, the behavior of the code, this is the &lt;b&gt;business problem complexity&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span lang="EN-US"&gt;how the actual implementation
does achieve the business problem,&lt;span&gt;&amp;nbsp; &lt;/span&gt;what was the developer mental state while she wrote the code, this
is the &lt;b&gt;implementation complexity&lt;/b&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Business problem
complexity lies into the specification of the program and reducing it means
working on the behavior of the code itself. On the other hand, we are talking
of &lt;b&gt;fabricated complexity&lt;/b&gt; when it
comes to the complexity of the implementation: it is fabricated in the sense
that it can be reduced without altering the behavior of the code. As an
illustration here is a super/giant/complex method found inside the .Net
Framework implementation &lt;i&gt;System.Windows.Forms.DataGridView.GetClipboardContent()&lt;/i&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/LargeMethodSmall.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a target="_blank" href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/LargeMethodBig.jpg"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/LargeMethodSmall.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/LargeMethodBig.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;i&gt;&lt;span lang="EN-US"&gt;GetClipboardContent() &lt;/span&gt;&lt;/i&gt;&lt;span lang="EN-US"&gt;is made of&lt;i&gt; &lt;/i&gt;around 300 &lt;a target="_blank" href="http://www.ndepend.com/Metrics.aspx#NbLinesOfCode"&gt;Lines of
Code&lt;/a&gt;
and has a &lt;a target="_blank" href="http://www.ndepend.com/Metrics.aspx#ILCC"&gt;ILComplexity&lt;/a&gt;
equals to 192. &lt;i&gt;GetClipboardContent() &lt;/i&gt;does
not have drastic performance requirement. As a consequence I don&amp;rsquo;t see any
justification for not refactoring this massive method into smaller ones and
maybe even a small classes hierarchy that could help in implementing the enormous
switch/cases. Doing so would certainly discard a lot of fabricated complexity.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;Fighting Fabricated Complexity with Simple Code Metrics&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;The simplest way to limit
fabricated complexity is to abide by simple &lt;a target="_blank" href="http://www.ndepend.com/Metrics.aspx"&gt;code metrics&lt;/a&gt;
thresholds. This is why one of the default &lt;a target="_blank" href="http://www.ndepend.com/Features.aspx#CQL"&gt;CQL rule&lt;/a&gt;
proposed by &lt;a target="_blank" href="http://www.NDepend.com"&gt;NDepend&lt;/a&gt; is
the following one:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;// &amp;lt;Name&amp;gt;&lt;/span&gt;&lt;span style="background:lime none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;Quick summary of methods to
refactor&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;&amp;lt;/Name&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;WARN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;IF&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;Count&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;0&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;IN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;SELECT&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;TOP&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;10&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;METHODS&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;WHERE&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;NbLinesOfCode&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;30&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;// http://www.ndepend.com/Metrics.aspx#NbLinesOfCode&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;NbILInstructions&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;200&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;//
http://www.ndepend.com/Metrics.aspx#NbILInstructions&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;CyclomaticComplexity&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;20&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;// http://www.ndepend.com/Metrics.aspx#CC&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;ILCyclomaticComplexity&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;50&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;// http://www.ndepend.com/Metrics.aspx#ILCC&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;ILNestingDepth&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;4&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;//
http://www.ndepend.com/Metrics.aspx#ILNestingDepth&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;NbParameters&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;5&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;// http://www.ndepend.com/Metrics.aspx#NbParameters&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;"&gt;NbVariables&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;8&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;// http://www.ndepend.com/Metrics.aspx#NbVariables&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;NbOverloads&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;6&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; )&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;" lang="EN-US"&gt;//
http://www.ndepend.com/Metrics.aspx#NbOverloads&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;Fighting Fabricated Complexity with
Abstractions&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Another popular way to
limit fabricated complexity is to hide implementations behind some interfaces.
Even though &lt;a target="_blank" href="http://blogs.msdn.com/kcwalina/archive/2004/10/24/246947.aspx"&gt;interfaces are not contracts&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;,
an interface itself often conveys enough information to make its purpose
understandable. For example the &lt;a target="_blank" href="http://www.codeproject.com/KB/cs/idisposable.aspx"&gt;IDisposable pattern&lt;/a&gt; is a complex topic but still, the &lt;i&gt;IDisposable &lt;/i&gt;interface
present only one method and we at least understand that it is an
indication that some unmanaged resources must be released somehow. When
a piece of code relies on an interface, at code review
time, the interface relieves the developer from the burden of mastering
every
details of the implementation(s) hided by the interface. This is in
essence the&lt;a target="_blank" href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt; Liskov Subsitution
Principle&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;NDepend provides 2 ways to
asses if and where your program should use more abstractions. First NDepend
proposes the &lt;a target="_blank" href="http://articles.techrepublic.com.com/5100-10878_11-5035294.html"&gt;Robert C Martin metrics about Abstractness vs Instability&lt;/a&gt;.
The idea is that &lt;b&gt;the more a code element
of a program is popular, the more it should be abstract&lt;/b&gt;. Or in other words,
&lt;b&gt;avoid depending too much directly on implementations,
depend on abstractions instead&lt;/b&gt;. By popular code element I mean an assembly
(but the idea works also for namespaces and types) that is massively used by
other assemblies of the program. Abstractness of a group of types (like an
assembly) is the ratio NbAbstractTypes / NbTotalTypes. There are debates about
how these metrics should be computed and we plan in the future to make Robert C
Martin&amp;#39;s metrics more flexible but still, you get the point, it is not a good
idea to have concrete types very popular in your code base. This provokes some
Zones of Pains in your program, where changing the implementations can
potentially affect a large portion of the program. And implementations are
known to evolve more often than abstractions.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;The second way to assess
if more abstractions is needed is to rely on the &lt;a target="_blank" href="http://www.ndepend.com/Metrics.aspx#Level"&gt;Level metric&lt;/a&gt;.
I won&amp;rsquo;t detail here this metric and its usage because I have already done it in
the post &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/02/10/layering-the-level-metric-and-the-discourse-of-method.aspx"&gt;Layering, the Level metric and the Discourse of Method&lt;/a&gt;.
The idea here is that using more interfaces decrease the overall Level value of
code elements (classes/namespaces). Thus, if many classes and namespaces have a
high Level value (&amp;gt; 12), it means that you have a long stack of concrete
layers sitting above each others. Introducing abstractions is then a good idea
to split such long stack and benefit from the interface simplification describe
above.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;Fighting Fabricated Complexity with
Immutability&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;A common
source of fabricated complexity is mutable states. The human brain is not
properly wired to anticipate what is really happening at run-time in a program.
While reviewing a class, it is hard to imagine how many instances will
simultaneously exists at runtime and how the states of each these instances
will evolve over time. This is actually THE major source of problems when
dealing with a multi-threaded program. If a class is immutable (meaning if the
states of all its instances objects don&amp;rsquo;t change at runtime once the
constructor is called) its runtime behavior immediately becomes much easier to
grasp and as a bonus, one doesn&amp;rsquo;t need to synchronize access to immutable
objects. For more information, I wrote about &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx"&gt;the benefits of immutable types
and how NDepend can help in statically verifying immutability&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;Fighting Fabricated Complexity with minimal
Coupling&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;When trying
to re-engineer/understand/refactor a large piece of code (I mean something made
of dozens of classes like a big namespaces or an assembly), the difficulty is
directly proportional to the coupling between considered code elements. Both
these following graphs are made of dependencies between 23 classes, one with 53
edges and the other one with 175 edges: which one would you prefer to deal
with?&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/ComplexGraphSmall.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a target="_blank" href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/ComplexGraphBig.jpg"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/ComplexGraphSmall.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/ComplexGraphBig.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/SimpleGraphSmall.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a target="_blank" href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/SimpleGraphBig.jpg"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/SimpleGraphSmall.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/SimpleGraphBig.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;While &lt;a target="_blank" href="http://www.theserverside.net/tt/articles/showarticle.tss?id=ControllingDependencies"&gt;clear
componentization is certainly the best way to fight against entangled/spaghetti code&lt;/a&gt;,
keep in mind that using abstractions is also a good way to limit the
over coupling-overhead. Indeed, if an interface has N implementations, then relying
only on the interface is virtually like depending on the N underlying classes,
except that from the &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2009/02/01/understanding-code-static-vs-dynamic-dependencies.aspx"&gt;static dependency&lt;/a&gt;
point of view, you actually rely on only one type.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;" lang="EN-US"&gt;Could Fabricated Complexity be Measured?&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;There are plenty of other
sources of fabricated complexity and I estimate that the 4 quoted ones are
certainly the big 4 culprits. One could arguably appends also the ratio of code
coverage + the number of automatic tests in the list, since a clean tests suite
certainly forces the code to be better designed, simpler and more maintainable.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Interestingly enough, all
these potential sources of problem can be controlled through NDepend. Could we
find a formula that might process all these data to finally spit a number (a
score) to measure the Fabricated Complexity? This approach is implemented in
tools like &lt;a target="_blank" href="http://www.headwaysoftware.com"&gt;Struture101&lt;/a&gt;
in the Java world that comes with a dedicated &lt;a target="_blank" href="http://www.headwaysoftware.com/xs"&gt;XS metric&lt;/a&gt;
to measure fabricated complexity. Another example is the &lt;a target="_blank" href="http://blogs.msdn.com/fxcop/archive/2007/11/20/maintainability-index-range-and-meaning.aspx"&gt;maintainability index
range in VisualStudio&lt;/a&gt;
that spits a number in the range 0-100. This number is linearly computed from
potential source of fabricated complexity such as NbLinesOfCode, CyclomaticComplexity&amp;hellip;
and being lower than 20 is bad.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Such metrics are
certainly interesting but I have a few caveats. The inventors of these formulas
claim that in a certain range of value the code is crappy. The fact is that
the value spitted doesn&amp;rsquo;t have any associated dimension. If a method has 120
lines of code and a Cyclomatic Complexity of 50, does it helps to add 120+50
and says that 170 &lt;i&gt;something&lt;/i&gt;
(something here is undetermined) is a bad thing? From the 2 values (120 Loc and
50 CC), isn&amp;rsquo;t it already obvious that the method should be splitted? And what
about a method with 169 lines of code and no cyclomatic complexity (such as the
massive Windows Form &lt;i&gt;InitializeComponent()&lt;/i&gt;
method)? I don&amp;rsquo;t think that such a method is as bad as the previous one and
still they both measure 170 &lt;i&gt;something&lt;/i&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span lang="EN-US"&gt;Let&amp;#39;s take for example the more and more popular &lt;a target="_blank" href="http://www.artima.com/weblogs/viewpost.jsp?thread=215899"&gt;CRAP
metric&lt;/a&gt;
that helps to detect &lt;i&gt;crap code&lt;/i&gt;. The idea is that &lt;i&gt;crap code&lt;/i&gt; is complex methods and poorly covered by tests.The proposed formula is &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span lang="EN-US"&gt;&lt;b&gt;CRAP(m) = comp(m)^2 * (1 &amp;ndash; cov(m)/100)^3 + comp(m)&lt;/b&gt; &lt;/span&gt;&lt;span style="font-size:12pt;font-family:&amp;#39;Times New Roman&amp;#39;,&amp;#39;serif&amp;#39;;" lang="EN-US"&gt;where
&lt;b&gt;comp(m)&lt;/b&gt; is the cyclomatic complexity of method m, and &lt;b&gt;cov(m)&lt;/b&gt; is the test code
coverage provided by automated tests.&lt;/span&gt;&lt;span lang="EN-US"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span lang="EN-US"&gt;By mixing it up, it is hard
to understand the meaning of an estimation of the fabricated complexity value
and worst, it makes harder to predict which refactoring will affect
positively the value of the metric. This is the reason why so far in NDepend,
we favored a more linearly independent/vectorial way to assess the exact causes of
problems. To detect complex methods poorly covered with NDepend one just needs the following CQL rule and adjust thresholds at whim:&lt;/span&gt;








 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
  
  MicrosoftInternetExplorer4
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 




&lt;span style="font-size:12pt;font-family:&amp;#39;Times New Roman&amp;#39;,&amp;#39;serif&amp;#39;;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span style="font-size:12pt;font-family:&amp;#39;Times New Roman&amp;#39;,&amp;#39;serif&amp;#39;;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;WARN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;IF&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;Count&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;0&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;IN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;SELECT&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;METHODS&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;WHERE&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;CyclomaticComplexity&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;10&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;AND&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;PercentageCoverage&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&amp;lt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;90&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;ORDER&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;BY&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;NbLinesOfCode&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;DESC&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;The CQL result panel provides all needed information to work with the &lt;i&gt;crap methods &lt;/i&gt;detected and lets jump directly into VisualStudio to view and edit culprit methods:&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/CRAPCode.jpg"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/CRAPCode.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;Alternatively,
the CQL flexibility lets correlate these metrics with other kind of
relevant information, like for example detecting crap methods added recently to
the code base (or also crap methods where ChangesObjectState, ChangesTypeState, CodeWasChanged, is declared inside or outside certains assemblies/nalmespaces/classes, not IsGeneratedByCompiler, is used by a certain code element, is using certain code elements, BecameObsolete...etc):&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;








 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
  
  MicrosoftInternetExplorer4
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 





&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;WARN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;IF&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;Count&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;0&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;IN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;SELECT&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;METHODS&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;b&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;WHERE&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;WasAdded&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;AND&lt;/span&gt;&lt;/b&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;CyclomaticComplexity&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;10&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;AND&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;PercentageCoverage&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&amp;lt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt;90&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;ORDER&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;BY&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;" lang="EN-US"&gt;NbLinesOfCode&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;" lang="EN-US"&gt;DESC&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Also one can do experiments and compose metrics at whim:&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="color:red;" lang="EN-US"&gt;&lt;a href="http://codebetter.com/controlpanel/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/CQLComposition.jpg"&gt;&lt;span style="color:red;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/CQLComposition.jpg"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FabricatedComplexity/CQLComposition.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;






 
  
  
 




 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 





&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;b&gt;&lt;span style="font-size:16pt;color:red;" lang="EN-US"&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size:16pt;color:red;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;This situation might
evolve in the future. But so far I would prefer&lt;/span&gt;&lt;span lang="EN-US"&gt; to avoid giving the
NDepend user the feeling that fighting fabricated complexity is only a matter of
reducing the value of a &lt;i&gt;naked &lt;/i&gt;index (i.e an index without any dimension). Things must remain close to the code itself: if
I have a method with 15 parameters it seems much more meaningful than having
a class with a maintainability index equals to 18 or a CRAP value of 33.21. &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Are you using these
formulas? What&amp;rsquo;s your opinion on this?&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249379" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/kHoLTk9Dj64" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/06/28/fighting-fabricated-complexity.aspx</feedburner:origLink></item><item><title>MSpec... for Boo!</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/nvBzjdDgdi4/mspec-for-boo.aspx</link><pubDate>Sun, 28 Jun 2009 05:41:50 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249362</guid><dc:creator>aaronjensen</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Jeffery Olson (a fellow Eleutian guy) has put in some good effort to create a very readable, very clean MSpec DSL for Boo. You should &lt;a href="http://github.com/olsonjeffery/machine.specifications.boo/tree/master"&gt;give it a gander&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249362" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/nvBzjdDgdi4" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/mspec/default.aspx">mspec</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/boo/default.aspx">boo</category><feedburner:origLink>http://codebetter.com/blogs/aaron.jensen/archive/2009/06/27/mspec-for-boo.aspx</feedburner:origLink></item><item><title>Talking conventions with the Hanselman</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/rOZfPCn0LpQ/talking-conventions-with-the-hansleman.aspx</link><pubDate>Sat, 27 Jun 2009 15:22:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249336</guid><dc:creator>Jeremy D. Miller</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Scott Hanselman &lt;a href="http://hanselminutes.com/default.aspx?showID=185"&gt;posted a podcast he &amp;amp; I recorded at NDC&lt;/a&gt; last week.&amp;nbsp; We nominally talked about applying conventions and opinionated software to .Net, then we went off on every possible tangent that presented itself.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249336" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/rOZfPCn0LpQ" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/jeremy.miller/archive/2009/06/27/talking-conventions-with-the-hansleman.aspx</feedburner:origLink></item><item><title>The “Anti-For” Campaign</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/qLmYHrZri9A/the-anti-for-campaign.aspx</link><pubDate>Fri, 26 Jun 2009 04:35:26 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249307</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;Recently, there has been an effort launched called the &lt;a href="http://www.antiifcampaign.com/"&gt;“Anti-If Campaign”&lt;/a&gt; in which they deride the use of if statements and instead, focus on Object Oriented Principles in order to create more flexible designs.&amp;#160; Now certainly, I have a sympathetic ear to this cause as I’ve seen code that literally walks off the side of the screen due to nesting of if statements.&amp;#160; Pattern matching to me, especially at the top level of the function is actually quite beautiful in a way, such as the implementations in Haskell:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c847654b-3dff-4fd4-928b-52fdb03c6f0a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#008000;"&gt;--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Haskell&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;lucas :: Int &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Integer
lucas &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;
lucas &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;
lucas n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And in Erlang, this also holds true:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:309094f4-0c8d-4e93-83af-86ac469bbdba" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;%&lt;/span&gt;&lt;span style="color:#000000;"&gt; Erlang
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-module&lt;/span&gt;&lt;span style="color:#000000;"&gt;(lucascalc).
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-export&lt;/span&gt;&lt;span style="color:#000000;"&gt;([lucas&lt;/span&gt;&lt;span style="color:#000000;"&gt;/&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;]).

lucas(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
lucas(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
lucas(N) &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (N &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (N &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;).&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Simple, easy to understand and best of all, no if statements.&amp;#160; But, instead of focus on this debate, I’d like to propose another which strikes closer to this functional programmers heart, the “Anti-For Campaign”.&amp;#160; This is simply to say that we should create and use composable functions instead of explicit for loops.&amp;#160; This is actually an old post I had written months ago and until now had been unfinished, but now with some inspiration, it’ll finally be done.&lt;/p&gt;

&lt;h2&gt;What and Why?&lt;/h2&gt;

&lt;p&gt;Before you throw all sorts of questions asking what and why, let me instead ask a question.&amp;#160; When you’re writing a loop, ask yourself the question, “What am I accomplishing in this loop?”&amp;#160; Chances are, it might be one of the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Query (Map, Filter, etc)&lt;/li&gt;

  &lt;li&gt;Aggregation (Sum, Count, etc)&lt;/li&gt;

  &lt;li&gt;Perform some side effect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re doing more than one of those in a single loop, then well, you’re probably doing too much.&amp;#160; In fact, Martin Fowler’s Refactoring site has a refactoring called &lt;a href="http://www.refactoring.com/catalog/splitLoop.html"&gt;“Split Loop”&lt;/a&gt; which would solve that issue.&amp;#160; It is better for future refactorings and readability if we keep those loops pointed to do one thing, and one thing only.&amp;#160; Better yet, we could rid ourselves of that loop altogether, and that’s what we’ll focus on here.&lt;/p&gt;

&lt;p&gt;Looking at first two bullet points, you’ll notice most of LINQ is indeed built around those two to be able to query data as well as aggregate.&amp;#160; The final bullet point, we perform some sort of side effect, perhaps writing to a file, printing to the console, or even perhaps sending messages.&amp;#160; &lt;/p&gt;

&lt;p&gt;So, what’s my problem with them?&amp;#160; My problem is that it focuses more on the How instead of the What.&amp;#160; Let’s look at a quick example down below of what I mean.&amp;#160; First, we’ll attempt to find all prime numbers under 100 using C# as an example language.&amp;#160; First in the How:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e7517c49-06cc-4423-a641-fec8ee8a3894" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; IsPrime(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; i) {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; lim &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Math.Sqrt(i);
    
    Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; check &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
    check &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; j &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        j &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; lim &lt;/span&gt;&lt;span style="color:#000000;"&gt;||&lt;/span&gt;&lt;span style="color:#000000;"&gt; (i &lt;/span&gt;&lt;span style="color:#000000;"&gt;%&lt;/span&gt;&lt;span style="color:#000000;"&gt; j &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color:#000000;"&gt; check(j &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
        
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; check(&lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
}

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;[] args) {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; numbers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;100&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; output &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; List&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; number &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; numbers)
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;(IsPrime(number)) output.Add(number);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem is that if we then want to compose another operation, well, it’s really hard to do inside of these for loops.&amp;#160; We’re too focused on the how at this point.&amp;#160; Instead, getting to know generics and lazy evaluation, in .NET 2.0 and beyond, we were able to write generic functions to take advantage of some functional constructs.&amp;#160; This will give us a more declarative style that we can now focus on the what.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:59330329-7867-403f-a083-540beaf692a4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Filter&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; items,
    Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; predicate) {
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; item &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; items)
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;(predicate(item))
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;yield&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; item;
}

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;[] args) {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; numbers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;100&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; primes &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; items.Filter(x &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; IsPrime(x));
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Of course we realize that LINQ already has such constructs built in, so we could rewrite the entire code above in just one statement.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:63f23cd3-baf9-46ca-8d50-517f5ebabdfb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; primes &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;100&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    .Where(x &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; IsPrime(x));&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And what’s better is that it is composable that we could do other operations such as aggregations (sum, count, etc) without much additional code:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3953a208-84d3-401f-9f1c-c451beab8611" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; primesCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;100&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    .Where(x &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; IsPrime(x))
    .Count();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And there you have it.&amp;#160; We not only have a query, but also an aggregation.&amp;#160; Try doing that with those non-composable loops!&amp;#160; &lt;a href="http://www.codethinked.com/post/2009/06/15/Life-After-Loops.aspx"&gt;Justin Etheredge has a nice writeup&lt;/a&gt; as well recently on the subject.&lt;/p&gt;

&lt;h2&gt;Coping Strategies&lt;/h2&gt;

&lt;p&gt;Functional languages tend to deemphasize the use of such constructs.&amp;#160; In fact, Haskell has neither a for loop nor a while loop, and languages such as F# and OCaml have limited support for such constructs in terms of no break and continue.&amp;#160; We tend to look at those two in particular with suspicion due to the fact that it cannot return a value and instead it mutates state in some fashion.&amp;#160; With that in mind, how do we cope with the fact that those aren’t available to us?&amp;#160; Above I showed a basic concept of a filter instead of an explicit loop, but what about some other considerations?&lt;/p&gt;

&lt;p&gt;Some things we might want to consider with some links to some of my previous posts on the subject:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/08/13/recursing-on-recursion-continuation-passing.aspx"&gt;Explicit Recursion&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;Transforming each item (Map/Select &amp;amp; SelectMany in LINQ)&lt;/li&gt;

  &lt;li&gt;Selecting items (Filter/Where in LINQ)&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/02/14/fun-with-folds.aspx"&gt;Aggregating Data&lt;/a&gt; (Folds/Aggregate in LINQ)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In fact, many times that people could consider using explicit recursion could instead use a fold to aggregate the data which then cuts out the issue of tail call optimization.&amp;#160; By truly understanding the goals of LINQ as well as the concepts of functional programming, we can realize that most of the looping that we do can indeed be replaced by the above, outside of side effects of course.&lt;/p&gt;

&lt;h2&gt;Now What About Those Side Effects?&lt;/h2&gt;

&lt;p&gt;Is there a place where we draw the line and say that explicit loops are ok?&amp;#160; Eric Lippert was recently asked about the reasoning of the &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx"&gt;lack of the ForEach extension method on IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;.&amp;#160; His response was that he was philosophically opposed to such an endeavor as the whole approach is to cause a side effect.&amp;#160; As IEnumerable&amp;lt;T&amp;gt; collections are immutable, he doesn’t believe it makes as much sense because you wouldn’t be side effecting the collection itself.&amp;#160; Not only that, but introducing closures can complicate object lifetimes and all sorts of potential reference issues.&lt;/p&gt;

&lt;p&gt;What about me?&amp;#160; I understand his concerns, and in C#, I can certainly see where he is coming from.&amp;#160; However, in F# we have such constructs readily available to us in the iter and iteri functions as shown below in F# Interactive:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:62c5fb8e-73f1-4057-b68c-f2ee5de6ee16" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; flip f y x &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; f x y
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; [&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;..&lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt;]
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; List.map((&lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; List.filter(flip (&lt;/span&gt;&lt;span style="color:#000000;"&gt;%&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; List.iter(printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%d&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;6&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;12&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;18&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; [&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;..&lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt;]
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; List.map((&lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; List.filter(flip (&lt;/span&gt;&lt;span style="color:#000000;"&gt;%&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; List.iteri(printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%d\t%d&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;       &lt;/span&gt;&lt;span style="color:#800080;"&gt;6&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;       &lt;/span&gt;&lt;span style="color:#800080;"&gt;12&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;       &lt;/span&gt;&lt;span style="color:#800080;"&gt;18&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Outside of logging, writing to the console and such are rare in functional programming, so once again, I can certainly understand the concern.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I hope by going through some basic scenarios that we will indeed question our code the next time we see that we are writing that explicit loop with a for and a while.&amp;#160; With a tool chest filled with such functions as transforming every item, to filtering content, to aggregating data and so on, we can realize that we can create composable solutions instead of creating mutable collections or mutable variables and aggregating to them which are not as much.&amp;#160; So, come and join me in the “Anti-For Campaign”.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249307" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/qLmYHrZri9A" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Erlang/default.aspx">Erlang</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/26/the-anti-for-campaign.aspx</feedburner:origLink></item><item><title>Becoming a better developer, or “How to sidestep a question”</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/36Iy76jLNdk/becoming-a-better-developer-or-how-to-sidestep-a-question.aspx</link><pubDate>Thu, 25 Jun 2009 22:01:48 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249287</guid><dc:creator>Kyle Baley</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;This post is over a week in the making. I attended &lt;a href="http://ukdotnet.ning.com/xn/detail/2391280:Event:5286"&gt;AltNetBeers #9&lt;/a&gt; last week in London which is making a quick rise in my list of top developer-related events, big or small. And that’s not just because I got one of the best compliments ever from someone who claims his non-IT friend reads a single IT-related blog (take that, Hanselman!). Anyway, it’s nice to know I’m reaching my target audience.&lt;/p&gt;  &lt;p&gt;The event was not altogether removed from what &lt;a href="http://serialseb.blogspot.com/"&gt;Sebastien&lt;/a&gt; outlined at the one I attended &lt;a href="http://codebetter.com/blogs/kyle.baley/archive/2008/06/19/london-alt-net-beers-recap.aspx"&gt;last year&lt;/a&gt; which shows a degree of vision to which I only aspire. We spent a couple of hours talking on a topic of choice (as voted by attendees), then socialized until the place closed down at the unreasonable hour of 11pm. I’m told much merriment ensued afterward but, after &lt;a href="http://img34.yfrog.com/i/55x.jpg/"&gt;posing for a picture&lt;/a&gt;, I made my way back to the hotel.&lt;/p&gt;  &lt;p&gt;The topic at hand, and I’m paraphrasing: How do we become better developers today, and how do we create them tomorrow? Nice and subjectively vague, just how I like my open spaces. The runner up topic, brownfield applications, seemed suspiciously planted so I threw my weight behind the one we eventually chose.&lt;/p&gt;  &lt;div style="border-bottom:black 1px solid;border-left:black 1px solid;padding-bottom:0.25em;background-color:#dddddd;margin:0.25em;padding-left:0.25em;width:245px;padding-right:0.25em;float:right;font-size:8pt;border-top:black 1px solid;border-right:black 1px solid;padding-top:0.25em;"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="image" border="0" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/kyle.baley/image_5F00_149FEF64.png" width="244" height="164" alt="" /&gt; Kinda concerned at how often this image is relevant to my posts&lt;/div&gt;  &lt;p&gt;Conversation ran the usual gamut from apprenticeship programs (and thanks to &lt;a href="http://neildoesdotnet.blogspot.com/"&gt;Neil&lt;/a&gt; for the extensive historical thesis into the concept) to showing passion to defining professions to just going out there and doing it. My position through most of it was to focus more on the non-technical skillz, to the point where I think I pooh-poohed actual book learning more than I should have. If you decided, based on my advice, to drop out of university, I’d advise you to politely grovel to your dean to get back in. It’s still important. But take a psyche course or two when you go back.&lt;/p&gt;  &lt;p&gt;Alas, we didn’t offer as much practical advice to the first part of the question as I would have liked so I’m expanding on the closing comment I made. It was essentially: Follow your instincts.&lt;/p&gt;  &lt;p&gt;Now, there’s a danger with throwing out a broad statement like this because I made it in a very specific context. Namely, in a crowd of people who had taken time out of their lives to come to an IT-related event (albiet, one with more social activities than your average code camp; it *is* held in a pub after all). These are people who, at some level, have made a conscious decision to improve themselves on their own time. They could have had much better things to do but their instincts told them that this was an event that was more important than attending the new West End show, Deliverance: The Musical. (Side note: I know I just made that up now but MAN, the Duellin’ Banjoes scene would rock live; I suspect they’d lose a lot of people at intermission though.)&lt;/p&gt;  &lt;p&gt;A lot of people have questions like, well, how do I make myself a better developer. The fact that they are asking those questions is the first step. The next step is to trust that they can muddle their way through their own answer. Because as we proved last Wednesday night, the same answer may not work for everybody. Me? I’m not much of a book learner. Nor, ironically, do I get much out of scanning blog posts (other than creating my own internal Google index so I know where to look later when the topic becomes relevant to me).&lt;/p&gt;  &lt;p&gt;It’s dead simple to make yourself a better developer nowadays. Resources are plentiful and mostly free. And the ones you pay for, like conferences and training courses, are easy enough to rank with a little research (e.g. JP’s Nothin’ But .NET, Oredev, NDC). All it takes is a focused effort to make yourself better and to put some thought into how you go about it. You alone know which bloggers out there speak the same language you do. And you alone know which technologies will help you in your daily life and which ones interest you. Yes, SharePoint developers are in high demand but is it a product you want to learn and spend eight hours a day working with? (That’s not meant to be facetious actually, stop laughing.)&lt;/p&gt;  &lt;p&gt;There will be much second-guessing and wondering which way you should go. And that’s fine. I won’t pretend I’ve made the right decisions all the time but like the software we’re supposed to be writing, most decisions are reversible. Humming or hawing about a contract in Dubai? Go for it. If you don’t like it, go home when it’s done. Worried about being away from your family that long? Well, you have some soul-searching to do but once you’ve made the decision, move on to the next one.&lt;/p&gt;  &lt;p&gt;Was it the right decision? In my experience, unless you have direct and obvious evidence to the contrary, the answer is always yes.&lt;/p&gt;  &lt;p&gt;Like I said, the topic is generic enough that pretty much everyone’s opinion will be valid, likely because it has worked for them personally. The underlying message I want to get across isn’t so much how to improve yourself (because you already know how to do it), but rather, if you’re in the London area, AltNetBeers is not to be missed. Many thanks to Neil, Toby, Scott, Andrew, Chris (both of them), Paul, Christian, and Lorenzo for the conversations and, of course, to the incredibly humble and reticent Sebastien Lambla for organizing and chairing the event, and also to Neil Robbins for the geek lunch two days later.&lt;/p&gt;  &lt;p&gt;Kyle the Well-Shod&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249287" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/36Iy76jLNdk" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/kyle.baley/archive/tags/Career/default.aspx">Career</category><feedburner:origLink>http://codebetter.com/blogs/kyle.baley/archive/2009/06/25/becoming-a-better-developer-or-how-to-sidestep-a-question.aspx</feedburner:origLink></item><item><title>Ubiquitous Language in Product Development</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/BkPtbFlfYJk/ubiquitous-language-in-product-development.aspx</link><pubDate>Wed, 24 Jun 2009 21:15:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249270</guid><dc:creator>Dave Laribee</dc:creator><slash:comments>4</slash:comments><description>&lt;p&gt;Hi there. &lt;/p&gt;
&lt;p&gt;I got this question from a fellow named Martin today: &lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;Real simple ... most of the discussion on DDD surrounds the design in terms of the ubiquitous language.&lt;br /&gt;&lt;br /&gt;
What if you are developing a product that is designed to be a tool for
a certain industry, say marketing, but the processes and terminology
all differ according to the marketing company that is in discussion.&lt;br /&gt;&lt;br /&gt;
To be specific, one company may use the term Client and the other Customer?&lt;br /&gt;&lt;br /&gt;
How do you accommodate the difference in terms? &amp;nbsp;Do you code to a common denominator or do you just pick one and go?&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Great question; in product development we&amp;#39;re often trying to make a general solution that solves the common problems of customers that have different languages. There&amp;#39;s no way around that.&lt;/p&gt;
&lt;p&gt;First, find those elements that are universal. Question if there&amp;#39;s really a need for all of this customization or if your clients are OK with calling a client a customer. Having a ubiquitous language that can be used end-to-end, from developer to end user, is extremely valuable and kind of the original point. &lt;/p&gt;
&lt;p&gt;For variable terms you should, in my estimation and experience, form an &lt;i&gt;internal ubiquitous language&lt;/i&gt; that&amp;#39;s adhered to across product management and software development. In cases like this, you&amp;#39;ll can potentially also apply DDD guidance to a generic subdomain that handles terminology. This domain usually goes by the name &amp;quot;localization,&amp;quot; only slightly extended to allow clients to change terminology. This is exactly how we handle the differences in terminology (silly) between agile methods like Scrum and XP in our product at VersionOne. We simply have a set of components that handle the change between backlog item and story, iteration and sprint, etc.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249270" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/BkPtbFlfYJk" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/DDD/default.aspx">DDD</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/product+development/default.aspx">product development</category><feedburner:origLink>http://codebetter.com/blogs/david_laribee/archive/2009/06/24/ubiquitous-language-in-product-development.aspx</feedburner:origLink></item><item><title>Providing Safe Alternatives</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/3_pLRGs8MCc/providing-safe-alternatives.aspx</link><pubDate>Wed, 24 Jun 2009 13:08:19 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249263</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;When I was reading through &lt;a href="http://realworldhaskell.org"&gt;Real World Haskell&lt;/a&gt;, I was struck several times by the mention of providing safe function alternatives.&amp;#160; The idea is to provide a function that in all cases returns a value as well as the one which is meant to accept valid input and throw exceptions should that contract be violated.&amp;#160; There is a real performance consideration to be taken into account as a function which repeatedly throws exceptions as logic will tend to overwhelm a system and slow it down significantly.&amp;#160; Imagine if you will an application which reads a large directory to check each file for an X509 certificate, whether it has one or not, and it throws an exception if one is not present.&amp;#160; The problem of course is there is no way to determine whether a file was signed at the time using the .NET class without resorting to P/Invoke (my favorite).&lt;/p&gt;  &lt;p&gt;Recently on Twitter, there was talk finally of the &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94072"&gt;inclusion of Guid.TryParse in .NET 4.0&lt;/a&gt;, yet somehow, &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282488"&gt;Enum.TryParse has not been addressed yet&lt;/a&gt; (gee, we’re consistent here).&amp;#160; The idea is to provide a way of determining whether the string is a Guid format without throwing an exception, and instead returning a flag indicating success and an out parameter with the value which is set to the value if there is success, else the default value.&amp;#160; Before we dig any further, let’s look at some terminology.&lt;/p&gt;  &lt;h2&gt;Partial Versus Total Functions&lt;/h2&gt;  &lt;p&gt;To get some terminology straight, let’s talk about partial functions versus total functions.&amp;#160; Partial functions are those functions which only return values for a defined subset of valid inputs, as throwing an exception is not considered a return value.&amp;#160; On the other hand, functions that return valid results over the entire input are considered to be total functions.&amp;#160; Let’s look at a quick example of what that means in F# interactive, the first being a partial function and the latter being a total function.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:45719e3b-2c94-4c73-8f83-8e6666282714" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Linq;;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ([] : int list).First();;
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.First[TSource](IEnumerable`&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; source)
   at &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;StartupCode$FSI_0003&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;.$FSI_0003.main@()
stopped due to error
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ([] : int list).FirstOrDefault();;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : int &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We see by the above that the First() function is a partial function because it only has defined behavior for non-empty collections, whereas FirstOrDefault() has defined behavior for empty lists and returns the default value for the given generic type.&amp;#160; When we’re writing our code, we better know which one we’re dealing with as default values can cause unintended consequences.&amp;#160; Without proper tests around these calls, this can be hard to manage.&amp;#160; &lt;/p&gt;

&lt;p&gt;Mitigating these partial functions with calls to such things as the Count() extension method is not necessarily the answer either.&amp;#160; When dealing with non-lazy lists such as an array or a List&amp;lt;T&amp;gt;, then such things as the number of items in our collection is well known and part of the state.&amp;#160; In the case of the lazy sequence, this is not the case, so any call to Count() would cause an evaluation of all items in the list, which could lead to unintended consequences.&amp;#160; So, if we’re chaining together 4 partial functions together, we could be doing something wrong.&amp;#160; All in all, total functions should be preferred usage, but of course within reason.&lt;/p&gt;

&lt;h2&gt;Can We Do Better Than Try?&lt;/h2&gt;

&lt;p&gt;Going back to one of the original points, can we do better than the standard Try (Parse/GetValue/etc) pattern that is pervasive throughout .NET code?&amp;#160; After all, it tries to do two things at once, and the success flag can be missed if one so chose.&amp;#160;&amp;#160; F# decided to take a slightly different angle to this problem by returning the success flag and the value as a pair tuple.&amp;#160; Such an example would be like this memoize function below:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:61a0baf6-a98f-4403-8385-a861b4cc38ef" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Collections.Generic

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoize f &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; t &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dictionary&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_,_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; t.TryGetValue(n) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt; , value) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; value
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;, _    ) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; res &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; f n
        t.Add(n, res)
        res&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing above is calling the TryGetValue method which takes a key and returns a success flag as well as the out parameter for the value (set if there is success).&amp;#160; F# automatically creates a pair tuple for us which we can then pattern match against to take appropriate action.&amp;#160; Unfortunately, for this go around at least, C# does not treat tuples as first class data types, so a solution like this exclusive to F# at this time.&amp;#160; Overall, a much cleaner design, but maybe there’s another option?&lt;/p&gt;

&lt;h2&gt;Maybe there’s an Option…&lt;/h2&gt;

&lt;p&gt;Instead of dealing with the clumsiness of the above code, I would much rather solve this in a clean way using the F# Option type.&amp;#160; This allows us to definitively specify whether we have a value or not.&amp;#160; Oh great you say, another version of null.&amp;#160; Not quite, as sometimes null can be a value…&amp;#160; More to the point, we can have a universal solution to having a value without having to resort to the Nullable&amp;lt;T&amp;gt; or reference type null-ness nonsense.&amp;#160; Simply put, the Option type is no more than the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ade5c4ca-230d-4103-b6ca-615137e7c0d5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Option&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt; =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Some &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;of&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; None&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice two constructors for the Option type, the Some which takes a value, and the None which takes no parameters.&amp;#160; Now, using this to extend the Dictionary class much like the F# Map class, we can add a function which returns an Option type whether we have a value or not.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:007b6878-3890-454a-acdd-a52b159ca95f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Collections.Generic.Dictionary&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;k,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;v&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Lookup(k:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;k) =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.TryGetValue(k) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt; , value) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Some value
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;, _    ) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; None&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we can rewrite our memoize function to utilize this in a cleaner fashion using the Option type for pattern matching purposes to show our true intentions of whether we have a value or not.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bef88ed3-0ea4-4b0d-9b18-cc6301f08bf0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoize f &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; t &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dictionary&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_,_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; t.Lookup(n) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Some v &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; v
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; None   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; res &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; f n
        t.Add(n, res)
        res&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Much cleaner and shows better intent on our part.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;How far do we take this?&amp;#160; After all, making full functions everywhere sometimes isn’t feasible as we want to constrain our system.&amp;#160; Adding both partial and full implementations might just clutter up our entire API.&amp;#160; With this comes balance, but there are certainly places where this comes into play such as parsing values, querying values from collections and so on.&amp;#160; Could Code Contracts in .NET 4.0 help us here in terms of statically verifying that we’re not failing on a chained partial function?&amp;#160; We’re not quite finished here with this discussion as I’ve yet to cover the many values of null.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249263" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/3_pLRGs8MCc" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Haskell/default.aspx">Haskell</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/24/providing-safe-alternatives.aspx</feedburner:origLink></item><item><title>NDC 2009 Wrapup</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/Tt0pZrvjnGE/ndc-2009-wrapup.aspx</link><pubDate>Wed, 24 Jun 2009 12:58:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249262</guid><dc:creator>Jeremy D. Miller</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;I&amp;rsquo;m on my way back from the &lt;a href="http://www.ndc2009.no/en/"&gt;Norwegian 
Developer&amp;rsquo;s Conference&lt;/a&gt; in Oslo this past week.&amp;nbsp; I had a fantastic time 
meeting some new people, hanging out with friends, interacting with a couple 
heroes, and seeing a little bit of Norway.&amp;nbsp; Special thanks to &lt;a href="http://andersnoras.com/"&gt;Anders Nor&amp;aring;s&lt;/a&gt; for inviting me to NDC.&amp;nbsp; I had a 
blast with the Bergen gang:&amp;nbsp; &lt;a href="http://blog.fohjin.com/"&gt;Mark Nijhof&lt;/a&gt;, 
&lt;a href="http://twitter.com/stclairjohn"&gt;John St Clair&lt;/a&gt;, and &lt;a href="http://blog.torresdal.net/"&gt;Jon Arild T&amp;oslash;rresdal&lt;/a&gt;.&amp;nbsp; I got to put a face 
to &lt;a href="http://andreasohlund.blogspot.com/"&gt;Andreas &amp;Ouml;hlund&lt;/a&gt; who is 
developing the StructureMap integration with WCF and nServiceBus.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I think all the talks were recorded and are going to be posted to the NDC 
website for download.&amp;nbsp; I&amp;rsquo;ll blog the links as soon as they go up.&amp;nbsp; I gave 8 
talks in 4 days across 2 cities.&amp;nbsp; I&amp;rsquo;m not sure I want to be one of those 
professional speakers that do that a couple dozen times a year.&amp;nbsp; I had an 
awesome crowd for my &amp;ldquo;Design and Testability&amp;rdquo; talk that I appreciated.&amp;nbsp; I had a 
comparatively small crowd for my &amp;ldquo;Conventions over Configuration&amp;rdquo; talk, but I 
had a lot of good questions afterwards that probably made that my favorite 
session of the week.&amp;nbsp; Most importantly, I presented some preliminary design 
patterns work on the &amp;ldquo;Screen Activation Lifecycle&amp;rdquo; from my book and I thought it 
went over pretty well (you can see a little preview of it on &lt;a href="http://neverindoubtnet.blogspot.com/2009/06/screen-factory.html"&gt;Ward 
Bell&amp;rsquo;s blog here&lt;/a&gt;).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I got even more book ideas from &lt;a href="http://jonas.follesoe.no/"&gt;Jonas 
Folles&amp;oslash;&amp;rsquo;s&lt;/a&gt; talk.&amp;nbsp; &amp;lt;bias&amp;gt;Apparently there are some crazy people out 
there who think it&amp;rsquo;s not entirely insane to do &amp;ldquo;View First&amp;rdquo; navigation.&amp;nbsp; I need 
to cover that pattern in the book and pretend it&amp;rsquo;s not a nutso thing to 
do.&amp;lt;/bias&amp;gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Lastly, I gave my &amp;ldquo;Lessons Learned from StructureMap&amp;rdquo; talk one more time.&amp;nbsp; I 
think I&amp;rsquo;m going to retire this one permanently, so I might try to blog it out 
over the next 2 months.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Upon reflection, I think my favorite thing about NDC was that there was a lot 
of more or less ALT.NET-ish content (and the older speakers from the bigger 
software world that we admire), but we weren&amp;rsquo;t in special camps or the &amp;ldquo;Agile 
gutter&amp;rdquo; track like in the DevTeach&amp;rsquo;s I went to a couple years ago.&amp;nbsp; I love that 
the tee shirts for the conference said &amp;ldquo;Legalize Unit Testing.&amp;rdquo;&amp;nbsp; I don&amp;rsquo;t ever 
see ALT.NET becoming completely the mainstream, but it&amp;rsquo;d be nice to stop being 
this strangely alternative stuff on the fringe.&lt;/p&gt;
&lt;p&gt;I got ambushed by Hanselman and &lt;a href="http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-Storyteller-with-Jeremy-Miller/"&gt;recorded 
for Channel 9&lt;/a&gt; spouting off on 3 hours sleep about &amp;ldquo;Executable Requirements&amp;rdquo;, 
StoryTeller, apologizing for using angle brackets, and explaining why I&amp;rsquo;m not 
using Oslo/MGrammar for creating external DSL&amp;rsquo;s for testing (hint:&amp;nbsp; I&amp;rsquo;m very 
unenthusiastic about the phrase &amp;ldquo;you just have to parse the AST&amp;rdquo;).&amp;nbsp; There&amp;rsquo;s 
several other impromptu recordings from the &lt;a href="http://channel9.msdn.com/tags/HanselminutesOn9/"&gt;speaker&amp;rsquo;s area 
here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;All in all, I had a great time and I&amp;rsquo;d be happy to recommend NDC next year 
for those of you in that part of the world.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249262" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/Tt0pZrvjnGE" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/jeremy.miller/archive/2009/06/24/ndc-2009-wrapup.aspx</feedburner:origLink></item><item><title>A small web design rant: beware of the RADical designer</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/KuQZ8q8QcEw/a-small-web-design-rant-beware-of-the-radical-designer.aspx</link><pubDate>Mon, 22 Jun 2009 10:09:39 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249211</guid><dc:creator>pvanooijen</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;A web designer is responsible for the look of a web site. She designs how the information is displayed on the pages, what illustrations are used and how the parts look together. Working with Visual Studio the collaboration between developer and designer can be streamlined. What usually works quite well is the designer creating an html page and style sheet with the look of the site. The places with dynamic content are marked as plain div’s, in the first draft usually filled with some dummy content to get a first impression. As a developer I turn this page into the master page substituting the content div’s with content placeholders. The classes in the style sheet are used in all the views and pages and I am freed from worries about the look. A clear separation of concerns. &lt;/p&gt;  &lt;p&gt;Over time some adaptations might be necessary, due to content not fitting or specifications changing. This is usually no problem, I get a new css or updated html. Many a good designer does understand asp.net markup, making round tripping even easier. In the process both visual designers are used (by the designer) and mark up is edited (by me). This is no problem as good tools used are two way designers: when you change the look visually the markup changes and when you change the markup the visual appearance changes.&lt;/p&gt;  &lt;p&gt;All of this is of course pretty obvious. But recently I had an experience where it did not work. Describing the scenario our designer assured us it was no problem. She (actually he) would make a good design and his tool would provide us with plain html and a decent css stylesheet. The visual appearance, in the browser, was nice and the design was approved. But alas the markup behind was not that nice. The html was over complicated with things like div’s whose only purpose was to wrap up yet another div. The css did contain a lot of classes. Most of them were quite verbose and defined many many style attributes. A later analysis of their usage showed they were not &lt;em&gt;cascading&lt;/em&gt; style sheets, many an attribute was repeated, usually with exactly the same value. Trying to change the appearance of the site was hard. We had to wrestle our way through the trees of markup. And trying to change the font of a particular part was a quest through styles. Changing which one(s) would do the trick ?&lt;/p&gt;  &lt;p&gt;Going back to the designer did not solve that much, his knowledge of the mark up side of web design was not sufficient for an effective communication. The lessons learned were several. First of all make sure that your designer does not only have good esthetical judgment but can communicate his ideas in a shared language, that of html and css. And when she has a tool which generates that make sure it is a two way tool, so proposals for change can be dealt with.&lt;/p&gt;  &lt;p&gt;All reminded me very much of the RAD discussions. With again the same conclusion. RAD on itself smells but a good 2-way visual designer can still be nice. Coming from Delphi I used to believe that kind of tool existed. Having seen the haystacks of code Visual Studio produces led to serious doubt. And having seen what a visual design tool can do has shattered my last hope.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249211" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/KuQZ8q8QcEw" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/peter.van.ooijen/archive/2009/06/22/a-small-web-design-rant-beware-of-the-radical-designer.aspx</feedburner:origLink></item><item><title>Agile Behavior: Nurture Knowledge Database</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/nxNLam-XKKA/agile-behavior-nurture-knowledge-database.aspx</link><pubDate>Sun, 21 Jun 2009 09:26:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:249174</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;






 
  
  
 




 
  Normal
  0
  
  
  
  
  false
  false
  false
  
  EN-US
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 





&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&lt;a target="_blank" href="http://www.ndepend.com/Features.aspx#CQL"&gt;Code Query Language (CQL)&lt;/a&gt;
can be used to write all sorts of code convention. I&amp;rsquo;ve described some&lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/05/11/write-active-conventions-on-your-code-base.aspx"&gt; CQL range
of usage&lt;/a&gt;,
and as an illustration I also exposed some CQL rules dedicated to &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/06/19/rules-on-net-framework-usage.aspx"&gt;the .NET Framework usage&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;We discovered a weird issue in our
last &lt;a target="_blank" href="http://www.NDepend.com"&gt;NDepend&lt;/a&gt; version: to select programmatically a particular row in a Windows
Form DataGridView you need to call the setter &lt;/span&gt;&lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;DataGridView.&lt;/span&gt;&lt;span style="font-size:11pt;font-family:Consolas;"&gt;FirstDisplayedCell&lt;/span&gt;&lt;span&gt;. However if the DataGridView height is too small and
doesn&amp;rsquo;t let show any row, then an exception will be raised: &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0in 0in 0.0001pt;"&gt;&lt;i&gt;&lt;span&gt;Exception.Type {System.InvalidOperationException}&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p style="margin:0in 0in 0.0001pt;"&gt;&lt;i&gt;&lt;span&gt;Exception.Message {No room is available to display rows.}&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p style="margin:0in 0in 0.0001pt;"&gt;&lt;i&gt;&lt;span&gt;Exception.StackTrace {&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p style="margin:0in 0in 0.0001pt;"&gt;&lt;i&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;at
System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32
value)&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p style="margin:0in 0in 0.0001pt;"&gt;&lt;i&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;at System.Windows.Forms.DataGridView.set_FirstDisplayedCell(DataGridViewCell
value)&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&lt;i&gt;&amp;hellip;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;IMHO this behavior is so
counter-intuitive and prone to error that I would qualify it as a .NET
Framework bug. But &lt;i&gt;Microsoft&lt;/i&gt; is not willing to fix such behavior in the name of
ascendant compatibility. The solution is then to capitalize on
your own knowledge database. Typically we included the following CQL rule in
our set of rules. It warns as soon as there is a method that doesn&amp;rsquo;t check the rows
available height before setting &lt;/span&gt;&lt;span style="font-size:11pt;font-family:Consolas;"&gt;FirstDisplayedCell&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;// &amp;lt;Name&amp;gt;&lt;/span&gt;&lt;span style="background:lime none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;Check height space before setting FirstDisplayedCell
of &lt;span&gt;&amp;nbsp;&lt;/span&gt;a DataGridView&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;&amp;lt;/Name&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;SELECT&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;METHODS&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;WHERE&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;"&gt;IsDirectlyUsing&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;&amp;quot;System.Windows.Forms.DataGridView.set_FirstDisplayedCell(DataGridViewCell)&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;AND&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;!(&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;"&gt;IsDirectlyUsing&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;&amp;quot;System.Windows.Forms.Control.get_Height()&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;AND&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#000064;"&gt;IsDirectlyUsing&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;&amp;quot;System.Windows.Forms.DataGridView.get_ColumnHeadersHeight()&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;/* The pattern implementation is:&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
((rowToSelect.State &amp;amp; DataGridViewElementStates.Displayed) == 0) {&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
(this.Height - this.ColumnHeadersHeight &amp;gt; 0) {&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;this.FirstDisplayedCell = rowToSelect.Cells[0];&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:green;"&gt;*/&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0in;"&gt;&lt;span&gt;We immediately spotted 3 others
risky areas in our code.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=249174" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/nxNLam-XKKA" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/06/21/agile-behavior-nurture-knowledge-database.aspx</feedburner:origLink></item><item><title>F# – Async Running with Continuation Scissors</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/9aDodK2lSHg/f-async-running-with-continuation-scissors.aspx</link><pubDate>Sat, 20 Jun 2009 06:04:04 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248939</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;As you may have noticed, I’ve been covering a bit about concurrency on this blog lately, and for good reason.&amp;#160; Between Axum, Erlang, Scala and F#, there is a lot to explore with actor model concurrency, task based concurrency, data parallel applications and so on.&amp;#160; In the next couple of months, I have some talks coming up on concurrency and in particular with F#.&amp;#160; I’ve been covering a bit of that with F# and mailbox processing, but I wanted to step back a bit into the asynchronous workflows and hitting against a well known API.&amp;#160; &lt;/p&gt;  &lt;p&gt;In this case, let’s walk through a simple example of using the &lt;a href="http://apiwiki.twitter.com/Twitter-API-Documentation"&gt;Twitter HTTP API&lt;/a&gt; to get a user timeline using F# asynchronous workflows.&amp;#160; Best of all, I’ve done all of the above code without ever once opening up Visual Studio and instead using the power of a simple text editor and F# interactive, I’m able to be quite productive.&amp;#160; It certainly makes me rethink my utter dependence on certain tools…&lt;/p&gt;  &lt;h2&gt;Give me some tweets… eventually, when you get around to it&lt;/h2&gt;  &lt;p&gt;In order to get started, we need some helpers when dealing with LINQ to XML.&amp;#160; &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/11/f-duck-typing-and-structural-typing.aspx"&gt;In a previous post&lt;/a&gt;, I talked about the need for such a thing due to the lack of implicit operator support in F#.&amp;#160; We can use the same operator that we had before such as the following:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:199c0565-d5a0-406e-a951-99c08a147a45" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a) arg)

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt;) : string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Alternatively, we could look at utilizing the XNamespace as well due to it also having an implicit operator to convert from a string.&amp;#160; This is helpful as it has a defined the op_Addition which allows us to add an XNamespace to a string to create an XName.&amp;#160; To take advantage we could add an additional operator to take care of this.&amp;#160; Below is a simple session of F# interactive to show how it might work:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4de5fc91-a1c5-4ccf-8767-beb3c23053ff" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;!?&lt;/span&gt;&lt;span style="color:#000000;"&gt;) : string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XNamespace &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;!?&lt;/span&gt;&lt;span style="color:#000000;"&gt; ) : (string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XNamespace)

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;!?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : (string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt;:it@&lt;/span&gt;&lt;span style="color:#800080;"&gt;205&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; foo {LocalName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
                      Namespace &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;
                      NamespaceName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you’ll notice above, I curried the XNamespace implicit operator function and sure enough it recognizes it needs a string in order to produce an XName.&amp;#160; Now that we’ve defined our helpers, let’s start looking at how to get a user’s timeline in Twitter.&amp;#160; We’ll need to define a holder for our status data as well as the URL we will be using to post data.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3d95bf17-90e2-4de7-9105-5a4eb994d342" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; UserStatus &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; { UserName : string; ProfileImage : string; Status : string; StatusDate : DateTime }

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; timelineUrl &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;http://twitter.com/statuses/friends_timeline.xml&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’re interested at this point just of the user name, their image file location, their status and the date of the tweet.&amp;#160; Now let’s move onto retrieving and parsing the data.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:52dc38aa-f5cb-472d-89b5-17e7d0377517" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; parseDocument xml &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; document &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; XDocument.Parse(xml)
  document.Root.Descendants()
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Seq.choose(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; node &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
         &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; node.Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;user&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt;
           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; status &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; HttpUtility.HtmlDecode(node.Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;text&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Value)
           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; image &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; node.Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;user&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;profile_image_url&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Value
           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; userName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; node.Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;user&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;screen_name&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Value
           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; statusDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.ParseExact(node.Element(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;created_at&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Value, 
                                                &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;ddd MMM dd HH:mm:ss +0000 yyyy&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, 
                                                CultureInfo.CurrentCulture)
           Some { UserName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; userName; ProfileImage &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; image; Status &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; status; StatusDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; statusDate}
         &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt; None)  

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; getUserTimeline userName password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; request &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebRequest.Create timelineUrl :&lt;/span&gt;&lt;span style="color:#000000;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; HttpWebRequest
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;do&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.Credentials &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NetworkCredential(userName, password)
          
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; response &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.AsyncGetResponse()
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; StreamReader(response.GetResponseStream())
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; xml &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader.AsyncReadToEnd()
          
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; parseDocument xml
        }&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above code, we create a WebRequest, setting the credentials through Basic Authentication.&amp;#160; We’ll cover this approach for now with some attention to OAuth at some point in the relative near future.&amp;#160; After setting the identity, we get the response stream and read it to the end.&amp;#160; This gives us XML to parse and by using LINQ to XML, we’re able to iterate through each node, and if the user node is not null then we parse out each element for which we’re concerned.&lt;/p&gt;

&lt;p&gt;Now that we have the ability to do some asynchronous behavior, what do we do about it?&amp;#160; In previous posts, I’ve taken the road of running these operations in parallel, which then runs all instances and returns an answer.&amp;#160; But, if we’re&amp;#160; only executing one instance, how do we enlist in asynchronous behavior overall?&amp;#160; To illustrate, I’ll create a simple Windows Forms application to display the tweets in a textbox and have a refresh button to call the above function to repopulate our textbox.&amp;#160; First, let’s create the basic skeleton:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:40f65dc8-8a32-40b7-9322-de876ba1e274" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; form &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Form(Visible&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;, TopMost &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Width&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800080;"&gt;500&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Height&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800080;"&gt;300&lt;/span&gt;&lt;span style="color:#000000;"&gt;)

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; status &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; TextBox(Multiline&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;, ScrollBars&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;ScrollBars.Vertical)
form.Controls.Add status
status.Width &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;400&lt;/span&gt;&lt;span style="color:#000000;"&gt;
status.Height &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;300&lt;/span&gt;&lt;span style="color:#000000;"&gt;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; refresh &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Button(Text&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Refresh&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
form.Controls.Add refresh
refresh.Left &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;410&lt;/span&gt;&lt;span style="color:#000000;"&gt;
refresh.Top &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we have a basic skeleton, let’s add the behavior to the button to refresh our textbox.&amp;#160; To do this, we simply add a handler to the button click event.&amp;#160; In there, we will call Async.RunWithContinuations which gives us the ability to handle not only the successful case, but also should an exception be thrown or some form of cancelation occurs.&amp;#160; Before we look at the implementing code, let’s look at the signature of RunWithContinuations:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:171314ff-f655-4ee9-b2fe-aa1f634cc891" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.RunWithContinuations;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it :
  (&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a -&amp;gt; unit) * (exn -&amp;gt; unit) * (OperationCanceledException -&amp;gt; unit) *&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Async&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt; -&amp;gt; unit = &amp;lt;fun:clo@0-42&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What this tells us is that it takes a success continuation as the first argument, the exception continuation as the second argument, the cancelation continuation as the third, and then finally the asynchronous operation.&amp;#160; This allows us to uniquely handle each type of event, should they occur.&amp;#160; In this case, our success continuation should populate the textbox, whereas both the cancelation and the exceptional case should show a message box containing the exception.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f42edafa-9e26-4ecd-a7c1-0e1c225a8884" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;refresh.Click.Add
   (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      status.Text &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      Async.RunWithContinuations(
        (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; res &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
           res &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Seq.iter(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; item &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; status.Text &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; sprintf &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;User: %s - %s\r\n\r\n%s&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; item.UserName item.Status status.Text )),
        (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; exn &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; MessageBox.Show(sprintf &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;An error occurred: %A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; exn) &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ignore),
        (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; cxn &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; MessageBox.Show(sprintf &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;A cancellation error ocurred: %A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; cxn) &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ignore),
        (getUserTimeline &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;username&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;password&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)))&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt; As you can see, the behavior is rather straight forward to define handlers for our three cases.&amp;#160; Lastly, we provide our credentials to the getUserTimeline function and then when the button is clicked, we’ll get results much like the following screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_07355EFB.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_4626DF96.png" width="461" height="259" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt; Then we can keep pressing the refresh button and then asynchronously, it will handle the behavior appropriately, either in success or in failure.&amp;#160;&amp;#160; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This is a pretty simply and naive application, but it can show how you can take advantage of asynchronous behavior using the Async Workflows in F#.&amp;#160; By being able to have the F# libraries handle the exceptions and cancelation, a major source of errors is reduced by quite a bit.&amp;#160; Giving us the power then to handle each one of these scenarios, either in success, failure or cancelation is a great concept.&amp;#160; There is still more yet to be covered including Futures and the inclusion of the Task Parallel Library as well as other adventures into Twitter and Bing APIs.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248939" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/9aDodK2lSHg" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Concurrency/default.aspx">Concurrency</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/20/f-async-running-with-continuation-scissors.aspx</feedburner:origLink></item><item><title>Revisiting Memoization</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/ibUAbs4FldY/revisiting-memoization.aspx</link><pubDate>Thu, 18 Jun 2009 15:04:14 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248691</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;After revisiting the &lt;a href="http://www.haskell.org/haskellwiki/Memoization"&gt;Haskell Wiki&lt;/a&gt; recently, I wanted to look at &lt;a href="http://en.wikipedia.org/wiki/Memoization"&gt;memoization&lt;/a&gt; again for a brief second after talking about it &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/08/01/recursing-into-recursion-memoization.aspx"&gt;a while ago&lt;/a&gt;.&amp;#160; In particular, there were two competing ideas, one around using a generic dictionary/map for storing the memoized values, the other using a lazy list approach.&amp;#160; I wanted to briefly look into those as possible solutions.&lt;/p&gt;  &lt;h2&gt;The Baseline Approach&lt;/h2&gt;  &lt;p&gt;Let’s take a baseline approach, and instead of using the trite &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number"&gt;Fibonacci sequence&lt;/a&gt;, let’s use the &lt;a href="http://en.wikipedia.org/wiki/Lucas_number"&gt;Lucas Sequence&lt;/a&gt; instead.&amp;#160; It’s a slight variation on the theme, and enough to avoid that dreaded function.&amp;#160; The baseline should produce the following sequence: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123&lt;/p&gt;  &lt;p&gt;It might be implemented something like this:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5c52cb4d-2de7-4704-892b-b5832ae39ab3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;function&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 0I &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 2I
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I) &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; 2I)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that F# cannot handle BigInt values as literals like you can for integers and other primitives, but can be easily worked around by using a when clause in the pattern.&amp;#160; When we run the aforementioned function in F# interactive, we see that we get the expected results.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2b0d1c6a-8f8f-47e1-993d-878a4b91ebc9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas 2I;;
Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.003&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.000&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : bigint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 3I&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem with the above function is that when dealing with larger numbers, and calling this function repeatedly, you have to take a rather large performance hit each time.&amp;#160; And worse yet, when dealing with those larger numbers say greater than 30, the function just may hang.&amp;#160; We can solve this through a process called memoization, which I’ve talked about before.&amp;#160; The main idea is to cache all values that we’ve seen before for quicker retrieval instead of calculating the value each and every time.&lt;/p&gt;

&lt;h2&gt;The LazyList Approach&lt;/h2&gt;

&lt;p&gt;There are two ways I’ll cover memoization here, one using a lazy list and the other using the dictionary approach.&amp;#160; First, let’s look at how we might be able to use a list to store those values we’ve already calculated.&amp;#160; Let’s first look at the Haskell implementation which is similar to the Haskell Wiki example.&amp;#160; &lt;/p&gt;

&lt;p&gt;
  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:226ac5df-c376-4fcf-a11f-f04d3cb1d87f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;memoized_lucas :: Int &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Integer
memoized_lucas &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  (map lucas [&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; ..] &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        lucas &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        lucas n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas (n&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas (n&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;The approach here is to perform a map of the lucas from 0 to infinity and taking the nth value.&amp;#160; Since this is partially applied, we need to wait on the last value to say which value we need.&amp;#160; When we run this through GHCi, we can calculate the 1000th Lucas number rather easily with me omitting the result and only checking the time:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c6616b04-6f60-49fb-8acd-c4b809e09541" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt;Main&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas &lt;/span&gt;&lt;span style="color:#800080;"&gt;1000&lt;/span&gt;&lt;span style="color:#000000;"&gt;
(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0.03&lt;/span&gt;&lt;span style="color:#000000;"&gt; secs, &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; bytes)
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Not only that, but the performance is rather good.&amp;#160; Any subsequent calls to this function should now be memoized such as the following call:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:837ca8cf-0adf-4195-b737-174eec729c98" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt;Main&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas &lt;/span&gt;&lt;span style="color:#800080;"&gt;1000&lt;/span&gt;&lt;span style="color:#000000;"&gt;
(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0.00&lt;/span&gt;&lt;span style="color:#000000;"&gt; secs, &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; bytes)
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, how would we apply this to F#?&amp;#160; Can we get the same results as a lazy language?&amp;#160; If we switch over from using a list to a LazyList, the answer is yes, and the performance will be close.&amp;#160; First, we need to define the nth function which returns the value at the index specified.&amp;#160; Using pattern matching, we can then return the appropriate value, or recurse again.&amp;#160; We could use Seq.nth, but this may be more efficient.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b922427e-f3a1-4656-a042-a809c31a96dd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;module&lt;/span&gt;&lt;span style="color:#000000;"&gt; LazyList &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; Microsoft.FSharp.Collections.LazyList

  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; nth list index &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; list &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Cons(h, t) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; index &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; index &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt; h &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt; nth t (index &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; invalidArg &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;index&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;the index was outside the range of elements in the list&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we’ve defined our nth function, it’s time to define our memoized Lucas function.&amp;#160; The logic is the same as above, and I’ve introduced a new operator to get the nth value which could be used as an infix operator.&amp;#160; The problem is that F# currently does not allow for the infix operator to be in the middle during partially applied functions, so instead I have to define it in the beginning.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f63fef6e-d979-4348-831e-21f82e950848" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; Microsoft.FSharp.Math
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;!&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) s (n:bigint) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; LazyList.nth s (int n)

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas : bigint &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; bigint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;function&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 0I &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 2I
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I) &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; 2I)
  (&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;!&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) (LazyList.map lucas (seq {0I..&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; BigInt(Int32.MaxValue)} &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; LazyList.of_seq))&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a rather clumsy approach in some ways as we’re downcasting our bigint value to an integer, so in reality, we’re only able to get values up to Int32.MaxValue, which in most cases, should be ok. Let’s look at the performance of this in the same way we did above:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3bb1ac63-1723-4d5e-b437-0c9385d9cfde" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas 1000I;;
Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.092&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.093&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;15&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that we are taking a slight performance penalty in terms of our Generation 0 memory in the GC.&amp;#160; Any subsequent calls are memoized given the following results:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f0889c20-02f6-4e76-987e-6a3a8dc77c15" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas 1000I;;
Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.001&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.015&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let’s kick it up a notch to see what kind of penalty we may pay should we calculate the 10,000th Lucas sequence number:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8bc71079-acfa-483d-9f1f-b6566e40ca9c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas 10000I;;
Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;07.922&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;07.815&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;1511&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;356&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ouch!&amp;#160; But, there is another approach that we can use and that I’ve shown before using the generic dictionary to store our values.&lt;/p&gt;

&lt;h2&gt;The Dictionary Approach&lt;/h2&gt;

&lt;p&gt;The generic dictionary approach is another approach rather than using the lazy list approach.&amp;#160; We simply create a dictionary outside of our inner function which stores these values from our inner Lucas sequence function.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8b5c10bc-f751-4f3b-ab43-1ea9194164fb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Collections.Generic
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt; : bigint -&amp;gt; bigint =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; t &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dictionary&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_,_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; t.TryGetValue(n) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt; , res) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; res
    &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;, _  ) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
       &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 0I &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 2I
       &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I
       &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; res &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; 1I) &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas (n &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; 2I)
           t.Add(n, res)
           res
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; n &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; lucas n&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code itself is rather self explanatory.&amp;#160; We have the outer dictionary to store our values, then we have the inner function to calculate the value at the nth index.&amp;#160; We then determine whether it is in the dictionary and, if so return it, else calculate, add to the list and then return.&amp;#160; You’ll notice that we return not the literal value of the calculation, but instead a function which allows us to specify the nth at a later time.&amp;#160; This is important!&lt;/p&gt;

&lt;p&gt;Looking at the performance, we’ll notice the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:396327ea-26e5-4e4c-9789-ed0f90081c93" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt; 1000I;;&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.006&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.000&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt; 1000I;;&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.001&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.000&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that the first calls is less expensive than the lazy list approach and has less impact on Generation 0 of the GC.&amp;#160; Any subsequent calls, as above will be nil.&amp;#160; But once again, let’s ratchet up the number to 10,000 much as we did above:&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cf658ecc-b646-47ea-b94d-ee2c3734a34d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt; 10000I;;&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;Real: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.047&lt;/span&gt;&lt;span style="color:#000000;"&gt;, CPU: &lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00.062&lt;/span&gt;&lt;span style="color:#000000;"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen1: &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, gen2: &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice a drastically less performance hit than the above solution.&amp;#160; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;If nothing else, it was an interesting exercise in seeing different ways of memoization in action and what kind of performance penalties you may pay along the way.&amp;#160; Now back to your regular programming…&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248691" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/ibUAbs4FldY" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Haskell/default.aspx">Haskell</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/18/revisiting-memoization.aspx</feedburner:origLink></item><item><title>NHibernate : Handling the Special Cases</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/gd7G3fROYvo/nhibnerate-handling-the-special-cases.aspx</link><pubDate>Thu, 18 Jun 2009 14:03:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248687</guid><dc:creator>karl</dc:creator><slash:comments>8</slash:comments><description>&lt;p&gt;In my last &lt;a href="http://codebetter.com/blogs/karlseguin/archive/2009/06/10/nhibernate-some-naked-thoughts.aspx"&gt;NHibernate post&lt;/a&gt; I mentioned that NHibernate is great because it does the heavy lifting for the common tasks that make up 95% of my data access layer, while letting me tweak the remaining 5% as needed.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s look at some of the ways you can tweak, or even circumvent NHibernate for those queries that require some extra attention. Do note that there are a lot of different solutions &amp;ndash; we&amp;#39;re only looking at a few. &lt;/p&gt;
&lt;p&gt;The most obvious method is to use named queries and custom SQL statements in your mapping. This provides you a lot of flexibility without diverging from NHibernate&amp;#39;s realm. The &lt;a href="http://www.nhforge.org/doc/nh/en/index.html"&gt;NHibernate reference&lt;/a&gt; has good documentation on this approach, so we won&amp;#39;t cover it (check out section &lt;a href="http://www.nhforge.org/doc/nh/en/index.html#querysql-namedqueries"&gt;14.2 &amp;quot;Named Queries&amp;quot;&lt;/a&gt;, &lt;a href="http://www.nhforge.org/doc/nh/en/index.html#querysql-cud"&gt;14.3 &amp;quot;Custom SQL for create, update and delete&amp;quot;&lt;/a&gt; and &lt;a href="http://www.nhforge.org/doc/nh/en/index.html#querysql-creating"&gt;14.4 &amp;quot;Custom SQL for loading&amp;quot;&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The next option is to use an ISqlQuery via the ISession.CreateSqlQuery. Unlike an IQuery which works against your domain using HQL, an ISqlQuery is closely tied to your database and SQL. However, you still get access to NHibernate parameter substitutions, paging capabilities and O/R mapping (if you want). Again, the NHibernate reference has a very detailed section, check out &lt;a href="http://www.nhforge.org/doc/nh/en/index.html#querysql-creating"&gt;14.1 &amp;quot;Using an ISQLQuery&amp;quot;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can optimize your Deletes by supplying the ISession.Delete command some HQL rather than the instance you want to delete. For example, if you want to delete all comments belonging to a blog post, you can do:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;session.Delete(&amp;quot;from Comment where PostId = :id&amp;quot;, postId, NHibernateUtil.Int32);&lt;/pre&gt;
&lt;p&gt;More recently the NHibernate team made it possible to execute any statement (insert/delete/update) from an IQuery. You can find out more from &lt;a href="http://ayende.com/Blog/archive/2009/05/28/nhibernate-ndash-executable-dml.aspx"&gt;this post&lt;/a&gt; by Ayende.&lt;/p&gt;
&lt;p&gt;When necessary (typically bulk imports or other bulk operations), you can circumvent NHibernate&amp;#39;s first-level cache by using an IStatelessSession rather than an ISession. Rather than calling OpenSession on your ISessionFactory, simply call OpenStatlessSession. Keep in mind that the IStatelessSession doesn&amp;#39;t have the exact same API as an ISession.&lt;/p&gt;
&lt;p&gt;When things get really crazy, you can always get an ADO.NET connection from the ISession&amp;#39;s Connection property. With a raw database connection, the world is your oyster. You can always write your code in such a way that you have direct access to our connection string to create your own connection, thus completely and fully circumventing NHibernate.&lt;/p&gt;
&lt;p&gt;Finally, any of these methods can be combined with some simple designs in order to ensure that everything is neat and tidy:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;public abstract class Repository : IRepository
{
    public void Delete&amp;lt;T&amp;gt;(T entity)
    {
        WithinTransaction(s =&amp;gt; s.Delete(target));
    }

    protected void WithinTransaction(Action action)
    {
        WithinTransaction(Session, action);
    }        
    protected static void WithinTransaction(ISession session, Action action)
    {
        var transaction = session.BeginTransaction();
        try
        {
            action();
            transaction.Commit();
        }
        catch (Exception)
        {
            transaction.Rollback();
            throw;
        }
        finally
        {
            transaction.Dispose();
        }
    }
}


public class PostRepository : Repositor, IPostRepository
{
    public void Delete(int postId)
    {
        WithinTransaction(s =&amp;gt; 
            {
                session.Delete(&amp;quot;from Comment where PostId = :id&amp;quot;, postId, NHibernateUtil.Int32);
                session.Delete(&amp;quot;from Post where Id = :id&amp;quot;, postId, NHibernateUtil.Int32);
            });
    }
}&lt;/pre&gt;
&lt;p&gt;Once you understand NHibernate&amp;#39;s flexibility and understand your code&amp;#39;s hot spots, it generally takes a pretty trivial amount of tweaking to speed things up. While the changes you make, in the name of performance, to your models (both domain and data) may be disgusting rest assured that HHibernate will not stand in your way. Be warned though that many of the above solutions may have side effects with respect to NHibernate&amp;#39;s 1st and 2nd level caches. However, if you were to custom write everything, including caching, you&amp;#39;d likely have similar synch issues.
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248687" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/gd7G3fROYvo" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/karlseguin/archive/2009/06/18/nhibnerate-handling-the-special-cases.aspx</feedburner:origLink></item><item><title>CppDepend Beta now available </title><link>http://feedproxy.google.com/~r/CodeBetter/~3/unlONu7YnxE/cppdepend-beta-now-available.aspx</link><pubDate>Sun, 14 Jun 2009 15:43:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248583</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;






 
  
  
 




 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 





&lt;/p&gt;
&lt;p&gt;&lt;span lang="EN-US"&gt;A few months ago I was happy
to announce the Java version of NDepend: &lt;a target="_blank" href="http://www.XDepend.com"&gt;XDepend&lt;/a&gt;&lt;/span&gt;&lt;a target="_blank" href="http://www.XDepend.com"&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/a&gt;&lt;span lang="EN-US"&gt;. Actually a third brother was under
development and today, I am glad to announce &lt;a target="_blank" href="http://www.CppDepend.com"&gt;CppDepend&lt;/a&gt;&lt;/span&gt;&lt;a target="_blank" href="http://www.CppDepend.com"&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/a&gt;&lt;span lang="EN-US"&gt;, aka NDepend for C++. &lt;a target="_blank" href="http://www.cppdepend.com/CppDependDownload.aspx"&gt;A beta
version is freely downloadable&lt;/a&gt;.
You can use it right now to demystify that huge legacy C++ applications you are working on. Most of NDepend features are available in this beta. We are now
entering a debugging phase to reach a RTM product. Feel free to report bugs and
feedbacks on this &lt;a target="_blank" href="http://code.google.com/p/cppdepend/issues/list"&gt;google group&lt;/a&gt;&lt;/span&gt;&lt;a href="http://code.google.com/p/cppdepend/issues/list"&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/a&gt;&lt;span lang="EN-US"&gt;. Alternatively you can contact
directly Issam Lahlali (&lt;span class="go"&gt;&lt;i&gt;cppdepend
at google mail&lt;/i&gt;)&lt;/span&gt;, the product manager of the project.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span lang="EN-US"&gt;To make things more
practical, Issam just wrote an &lt;a target="_blank" href="http://cppdepend.blogspot.com/"&gt;article&lt;/a&gt;&lt;/span&gt;&lt;a href="http://cppdepend.blogspot.com/" target="_blank"&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/a&gt;&lt;span&gt; &lt;span lang="EN-US"&gt;showing what CppDepend has to
say about the MFC code base.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248583" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/unlONu7YnxE" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/06/14/cppdepend-beta-now-available.aspx</feedburner:origLink></item><item><title>I love 100% coverage!</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/Ja4EfxCBhB0/i-love-100-coverage.aspx</link><pubDate>Sat, 13 Jun 2009 09:02:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248531</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>9</slash:comments><description>&lt;p&gt;Last week I wrote a &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2009/06/07/high-test-coverage-ratio-is-a-good-thing-anyway.aspx"&gt;post&lt;/a&gt; about non-trivial reasons why 100% coverage is useful anyway. &lt;/p&gt;
&lt;p&gt;Yesterday, my quest for 100% coverage saved me one more time from some code written too quickly.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Code written too quickly, not 100% covered:&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="padding-left:30px;"&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FullCoverage/FullCoverageKO.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FullCoverage/FullCoverageKO.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Code fixed 100% covered:&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="padding-left:30px;"&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FullCoverage/FullCoverageOK.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FullCoverage/FullCoverageOK.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; I am not sure that a proper code review would have detect this &lt;i&gt;one character&lt;/i&gt; bug, human eyes are not trained for that.&lt;/p&gt;
&lt;p&gt;Moreover, this code is in a &lt;i&gt;IComparer&amp;lt;T&amp;gt;.Compare(...)&lt;/i&gt; method used for sorting. In this condition the bug side-effect could be hardly detected by unit tests and contracts.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.FullCoverage/FullCoverageOK.png"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248531" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/Ja4EfxCBhB0" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/06/13/i-love-100-coverage.aspx</feedburner:origLink></item><item><title>You’re Not Your Data Access</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/_Z1ROj8QGWA/you-re-not-your-data-access.aspx</link><pubDate>Fri, 12 Jun 2009 03:50:33 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248446</guid><dc:creator>robconery</dc:creator><slash:comments>34</slash:comments><description>&lt;p&gt;Seems I touched off a bit of a “swirl” with a comment I made on my last blog post:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;I think, in general, the .NET crowd overthinks and over-engineers just about everything&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I said as much in my MIX presentation, where I basically challenged ASP.NET developers to &lt;a href="http://videos.visitmix.com/MIX09/T62F"&gt;“embrace their inner scripter” and stop building rockets&lt;/a&gt;. I have a fairly strong opinion on this – and I’ll save that for another time – but I think it’s high time to remind folks that there’s a lot more to an application then how you get your app gets it’s freak on with your database.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Good Morning!      &lt;br /&gt;&lt;/strong&gt;I remember watching Fight Club for the very first time back in the thick of the very first DotCom bubble. I was feeling pretty ill that day so I stayed home and watched the DVD, and was pulled in pretty dramatically. One line still resonates with me:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;You&amp;#39;re not your job. You&amp;#39;re not how much money you have in the bank. You&amp;#39;re not the car you drive. You&amp;#39;re not the contents of your wallet. You&amp;#39;re not your f******* khakis.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I feel the same way about building software: &lt;strong&gt;You’re Not Your Data Access.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When I watched that movie I had just bought a nice pair of $180 Kenneth Cole shoes to wear to a client’s VC meeting, to go with my groovy business-casual Cornflower Blue button down shirt. I clearly remember looking over at my closet – the feeling of fashion sobriety coming over me – wondering what I had turned into (if you knew me then you’d know I don’t like wearing that kind of stuff. I’m happy in a white t-shirt and shorts).&lt;/p&gt;  &lt;p&gt;I had this same feeling when I popped my head out of the .NET community just 4 years ago (before I worked at Microsoft) to see what this “Rails thing” was all about. I remember the feeling well – and it was very, very sobering and inspiring at the same time. I remember creating an application in fairly short order with all the requisite pieces put where they needed to be put, with every tutorial urging me to “stop thinking – just build”. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Camaro Programming      &lt;br /&gt;&lt;/strong&gt;The .NET platform is pretty dang powerful. It lets you build your own personal Tower of Babel if you so choose, and it can abstract away the very fabric of space-time if you let it. In short it’s a killer set of tools that any mechanically-inclined &lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob.conery/towerofbable_5F00_458B62B9.jpg"&gt;&lt;img title="tower-of-bable" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;margin-left:0px;margin-right:0px;border-right-width:0px;" height="239" alt="tower-of-bable" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob.conery/towerofbable_5F00_thumb_5F00_081AFE32.jpg" width="299" align="right" border="0" /&gt;&lt;/a&gt;person would love to use. So naturally it can be very easily abused!&lt;/p&gt;  &lt;p&gt;Consider my good friend Eric. &lt;a href="http://blog.wekeroad.com/subsonic/in-which-we-discuss-digital-gearheads-and-geek-mastery/"&gt;I’ve written about Eric a few times before&lt;/a&gt; – but in summary he’s my token wrench-head friend that loves his engines and American cars. I lived with this guy for years and he rapidly&amp;#160; filled up our garage with parts and *crap* from every car he’d ever owned. And he could dutifully tell you which part went to which car and what that part did – no matter if it was useless.&lt;/p&gt;  &lt;p&gt;Eric built and maintained a hunk of junk 1968 Capri which we used to call “The Crappy”. This thing could drive – &lt;em&gt;and I mean really drive&lt;/em&gt; – I think Eric pushed it to 180 mph once on a closed race course. It had &lt;em&gt;that sound&lt;/em&gt; that all muscle cars make – the sound that makes your ribs shake and your inner 16 yr old say “coooooolll!”.&lt;/p&gt;  &lt;p&gt;The problem was that no one ever wanted to drive in it. It smelled, it was ugly, and it absolutely sucked the gas down. Eric would steadfastly defend his Crappy because “it was the easiest car in the world to work on” and “could blow doors on any production car from any country”.&lt;/p&gt;  &lt;p&gt;“Yah but when do you really need to go 180 miles an hour man?”&lt;/p&gt;  &lt;p&gt;“When I race dammit”&lt;/p&gt;  &lt;p&gt;I don’t need to draw the parallels here. I think you get it.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Let it Go Already      &lt;br /&gt;&lt;/strong&gt;I want to be perfectly clear about what it is I’m saying here, and that is that it’s very, very easy for the geek inside us to whisper sweetly in our ears, saying “this Super Massive Paradigm Shifter will be the biggest application EVAR… don’t mess it up!”. I felt this very sensation when starting … well every app that I ever made.&lt;/p&gt;  &lt;p&gt;I don’t like being wrong – no one does. So defensively I’ll be sure to include patterning where I can, making sure to separate out my projects whenever it … seems like I’ll crap for not separating out what I’m doing into another project. &lt;/p&gt;  &lt;p&gt;I’ll use &lt;a href="http://twitter.com/kevindente/statuses/2121170805"&gt;enums instead of boolean parameters&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Oh wait, &lt;a href="http://devpinoy.org/blogs/cruizer/archive/2007/09/12/enums-are-evil.aspx"&gt;no I won’t&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I’ll be sure to &lt;a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx"&gt;avoid Singletons&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I’ll absolutely &lt;a href="http://weblogs.asp.net/fbouma/archive/2003/11/18/38178.aspx"&gt;never use Stored Procedures&lt;/a&gt;, I mean seriously &lt;a href="http://weblogs.asp.net/bsimser/archive/2004/09/05/225808.aspx"&gt;never&lt;/a&gt;, &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2005/07/05/130093.aspx"&gt;ever&lt;/a&gt;, &lt;a href="http://c2.com/cgi/wiki?StoredProceduresAreEvil"&gt;ever&lt;/a&gt; (unless &lt;a href="http://weblogs.asp.net/rhoward/archive/2003/11/17/38095.aspx"&gt;Rob Howard tells me to&lt;/a&gt;). Or maybe unless &lt;a href="http://msdn.microsoft.com/en-us/library/ms978510.aspx"&gt;my boss tells me to&lt;/a&gt;. I’ll &lt;a href="http://www.codinghorror.com/blog/archives/000117.html"&gt;let Jeff decide&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;I’ll &lt;a href="http://sqlblog.com/blogs/paul_nielsen/archive/2009/05/09/why-use-stored-procedures.aspx"&gt;always use Stored Procedures&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I &lt;a href="http://aspadvice.com/blogs/azamsharp/archive/2007/10/04/My-Thoughts-on-NHibernate.aspx"&gt;won’t use NHibernate&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I won’t use &lt;a href="http://www.tonymarston.net/php-mysql/object-relational-mappers-are-evil.html"&gt;any ORM for that matter&lt;/a&gt;. Seriously - &lt;a href="http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx"&gt;Never&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Well &lt;a href="http://blairbryant.wordpress.com/2007/04/11/orm-isnt-an-excuse-for-not-using-stored-procedures/"&gt;I might use both&lt;/a&gt;…&lt;/p&gt;  &lt;p&gt;I &lt;a href="http://www.ejlife.net/blogs/john/2006/08/16/1155746828758.html"&gt;won’t use Code-coverage&lt;/a&gt; to tell me anything. &lt;/p&gt;  &lt;p&gt;I’ll be sure to &lt;a href="http://codebetter.com/blogs/patricksmacchia/archive/2008/04/09/make-the-most-of-your-test-coverage-data.aspx"&gt;use Code-coverage&lt;/a&gt; data to tell me everything.&lt;/p&gt;  &lt;p&gt;I’ll make sure my &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/09/21/wrestling-with-pure-evil.aspx"&gt;method and variables are named properly on Fridays&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Reflection? &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/07/29/is-it-evil-to-use-reflection-when-testing.aspx"&gt;Not a chance&lt;/a&gt;. &lt;a href="http://www.west-wind.com/weblog/posts/351.aspx"&gt;Unless Rick says it’s OK&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I &lt;a href="http://www.theserverside.com/news/thread.tss?thread_id=17635"&gt;won’t use Extreme Programming&lt;/a&gt; (I’ll be sure to use Lean… whatever that is)&lt;/p&gt;  &lt;p&gt;I’ll be sure &lt;a href="http://ericlefevre.net/wordpress/2009/06/02/is-scrum-evil-beyond-our-session-at-xp-day-paris/"&gt;not to have scrums&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I’ll also be sure &lt;a href="http://www.stickyminds.com/sitewide.asp?Function=edetail&amp;amp;ObjectType=ART&amp;amp;ObjectId=14287"&gt;not to multi-task&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;I &lt;a href="http://ayende.com/Blog/archive/2008/08/22/JFHCI-The-evil-that-is-configuration.aspx"&gt;won’t use configuration either&lt;/a&gt; – hard-coding FTW!&lt;/p&gt;  &lt;p&gt;I won’t use &lt;a href="http://digg.com/programming/C_extension_methods_are_evil"&gt;static methods&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I won’t use &lt;a href="http://www.aaron-powell.com/blog/january-2009/are-extensions-really-evil.aspx"&gt;extension methods&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I’ll &lt;a href="http://www.codinghorror.com/blog/archives/000486.html"&gt;keep Native American stereotypes out of my code&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I’ll make sure that all my methods are public and &lt;a href="http://ayende.com/Blog/archive/2008/04/03/Internals-are-Evil.aspx"&gt;never sealed or internal&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I’ll check twice before I use any of &lt;a href="http://www.codethinked.com/post/2009/04/02/Ten-C-Keywords-That-You-Shouldne28099t-Be-Using.aspx"&gt;these evil keywords&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;I’ll also make double damn sure &lt;a href="http://weblogs.asp.net/bsimser/archive/2007/10/30/regions-evil.aspx"&gt;not to use regions in my code&lt;/a&gt; – ever.&lt;/p&gt;  &lt;p&gt;I &lt;a href="http://pbdj.sys-con.com/node/42545"&gt;won’t use VB&lt;/a&gt; (who would?) and I’ll be sure to &lt;a href="http://paskov.info/tags/c/"&gt;use C# with great c&lt;/a&gt;are.&lt;/p&gt;  &lt;p&gt;I &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/02/01/Composite-keys-are-evil.aspx"&gt;won’t use Composite Keys&lt;/a&gt; in my database &lt;/p&gt;  &lt;p&gt;And I’ll be very, very sure to &lt;a href="http://friendfeed.com/bellware/846011d6/forces-of-good-and-evil-will-do-battle-in-norway"&gt;keep away from Norway&lt;/a&gt;.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;You wake up at Seatac, SFO, LAX. You wake up at O&amp;#39;Hare, Dallas-Fort Worth, BWI. Pacific, mountain, central. Lose an hour, gain an hour. This is your life, and it&amp;#39;s ending one minute at a time. You wake up at Air Harbor International. If you wake up at a different time, in a different place, could you wake up as a different person&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Let.&lt;/p&gt;  &lt;p&gt;It.&lt;/p&gt;  &lt;p&gt;Go.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;     &lt;br /&gt;Simplicity is beautiful. Simple doesn’t mean hard-coding, it doesn’t mean cutting corners or being sloppy – it means building what you’ve been asked to build, not a rocket to Saturn and most of the time it’s a skateboard to the corner store. &lt;/p&gt;  &lt;p&gt;Not every application needs to be stitched together from Codebetter posts and Twitter rants. Focus on what’s important – the experience, not what’s under the hood. You’ll change that quite a few times no matter what you think :).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248446" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/_Z1ROj8QGWA" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/rob.conery/archive/2009/06/11/you-re-not-your-data-access.aspx</feedburner:origLink></item><item><title>F# – Duck Typing and Structural Typing</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/rbVyJs1ulRI/f-duck-typing-and-structural-typing.aspx</link><pubDate>Thu, 11 Jun 2009 16:53:18 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:248422</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;As you may have noticed on this blog lately that I’ve been focusing on Asynchronous Workflows.&amp;#160; In those adventures, I’ve been taking well known APIs such as Twitter, Bing, among others and seeing what I can do with them.&amp;#160; In this instance, when using LINQ to XML, I’ve run into a slight syntax problem.&lt;/p&gt;  &lt;h2&gt;The Problem with LINQ to XML&lt;/h2&gt;  &lt;p&gt; The syntax problem I had revolves around the retrieval of an XElement from the given XContainer.&amp;#160; The signature of this is as follows:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d6326816-0118-4d04-8971-1317a8369346" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; XContainer : XNode {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; XElement Element(XName name);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But, what you may not realize is that there is an implicit operator which allows us to implicitly convert a string to an XName with little effort such as this:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:afacadad-f244-4daf-921d-a72f60ee5496" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;sealed&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName : IEquatable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;XName&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, ISerializable {
...
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;implicit&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;operator&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName(
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; expandedName);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows us to use a string instead of an XName when specifying which element name we want, including namespace if desired, rather easily.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bc427b99-9d4c-459c-84f6-ac39684523c1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; document &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; XDocument.Parse(xml);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; statuses &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; document.Root.Descendants()
    .Select(node &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; node.Element(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;text&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Value);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately for us, this behavior does not work in the current F# release.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1893402f-ad79-422a-8a81-e3b37bfa87cc" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; document.Root.Descendants() 
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Seq.map(fun node &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; node.Element(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;node&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).Value);;

stdin(&lt;/span&gt;&lt;span style="color:#800080;"&gt;5&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800080;"&gt;65&lt;/span&gt;&lt;span style="color:#000000;"&gt;): error FS0001: This expression has &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        string
but is here used with &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        XName&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It does not recognize this implicit operator.&amp;#160; What options do we have around this?&amp;#160; One simple answer might be just a simple function that does the conversion for us, such as this:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:43cc0d63-2361-4778-8187-a6d0345af5f7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; ) : string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName.op_Implicit;;
val ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; ) : string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; xFoo &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
val xFoo : XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; foo&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we’re now able to do is convert at will using the !! operator, or what I call the “Convert Dammit” operator.&amp;#160; But, there is another solution worth exploring that is a bit more generic through the use of duck typing.&lt;/p&gt;

&lt;h2&gt;Duck Typing to the Rescue?!?&lt;/h2&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;For those unfamiliar with the term &lt;a href="http://en.wikipedia.org/wiki/Duck_typing"&gt;duck typing&lt;/a&gt;, it is a style of dynamic typing in which the object’s current set of methods, properties, etc, determines its validity instead of its inheritance chain.&amp;#160; In F#, we have the ability to use this mechanism to use this to our advantage to create generic functions.&amp;#160; For example, let’s take the implicit operator example from above and use duck typing instead of the direct call to the XName class.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:80df072c-c3a4-4561-a74a-21ffa6b18225" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Longhand&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a,&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a)&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a) arg)
  
&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Shorthand - let the compiler do the work&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a) arg)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The two above statements are syntactically the same, although in the latter version, I don’t have to clutter up my declarations as much and instead let the compiler type inference magic do its work.&amp;#160; From here on out, I’ll use the shorthand unless the longhand doesn’t quite work out.&amp;#160; What you’ll notice is that I have a requirement for my ^a generic to have a restriction that it must have the implicit operator declared.&amp;#160; Then we can execute said operation by passing in our argument.&amp;#160; I can rewrite the convert dammit operator then to look something like this:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb0c7fb0-6b43-412c-8e71-f798978e9d92" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Xml.Linq
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt;) : string &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; xFoo &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Our new XName&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt; Now you can see here that I didn’t explicitly have to call anything on XName as I let the implicit operator do the work for me.&amp;#160; Best of all here is that it’s now statically typed to that any call to implicit must have that operator defined on it, else it will not compile.&lt;/p&gt;

&lt;p&gt;But, let’s not stop there, you can continue on all sorts of tangents.&amp;#160; What about functions which have more than one argument?&amp;#160; Let’s take the addition operator.&amp;#160; Could we use duck typing in our favor for this?&amp;#160; Although it’s not recommended, the answer is yes.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8324a32f-ebc3-45e7-8cc8-be3f4676c01b" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; add arg1 arg2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Addition : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a) (arg1, arg2))
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; add :
   &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;when&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; ) :  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a)

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System;;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; addedTime &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; add DateTime.Now (TimeSpan.FromSeconds &lt;/span&gt;&lt;span style="color:#800080;"&gt;5000&lt;/span&gt;&lt;span style="color:#000000;"&gt;.);;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; addedTime : DateTime &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;6&lt;/span&gt;&lt;span style="color:#000000;"&gt;/&lt;/span&gt;&lt;span style="color:#800080;"&gt;11&lt;/span&gt;&lt;span style="color:#000000;"&gt;/&lt;/span&gt;&lt;span style="color:#800080;"&gt;2009&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;00&lt;/span&gt;&lt;span style="color:#000000;"&gt;:&lt;/span&gt;&lt;span style="color:#800080;"&gt;32&lt;/span&gt;&lt;span style="color:#000000;"&gt; PM&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The reason why it’s not recommended is that a lot of primitive types are implicitly augmented with this operator such as int, float, etc, so this would not work for those plus not work on string as well since dynamic invocation is not allowed on this particular instance.&amp;#160; &lt;/p&gt;

&lt;p&gt;This is useful though in other scenarios.&amp;#160; Take for example extracting a Name property from a given class.&amp;#160; Once again, using the methodologies from above, it’s rather simple:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:431e4364-c355-4318-bd77-656269681ac6" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; getName arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; Name : string) arg)
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.IO
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; fName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; getName (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; FileInfo(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;fsi.exe&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;))
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; dName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; getName (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; DirectoryInfo(Environment.CurrentDirectory))&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What about multiple restrictions?&amp;#160; That’s not too difficult either.&amp;#160; Let’s do the canonical Duck example with a little twist.&amp;#160; First, we will define the overall function which will extract the Flying and Walking state of a given class.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb4b59c1-e236-4e37-acd4-c4c633f8b628" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; flyAndWalk arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; flying &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; Fly : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; string) arg)
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; walking &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; Walk : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; string) arg)
  (flying, walking)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And now we can define our classes which have the methods which have our interest such as the Fly and Walk methods.&amp;#160; We execute each of these, obtain the result and finally return the results of each as a tuple.&amp;#160; Now, let’s define two classes below of a Duck and an Eagle.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2992a6ed-d2ed-458a-a2c5-3887628b1356" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Duck() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Swim() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;paddling&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Fly() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;flapping&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Walk() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;waddling&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Eagle() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Fly() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;soaring&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Walk() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;creeping&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (eFly, eWalk) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; flyAndWalk (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Eagle())
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (dFly, dWalk) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; flyAndWalk (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Duck())&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our final result will be rather predictable as you might imagine.&amp;#160; Just as well if we put a Penguin class which contains no fly, well, then this class won’t compile as shown below in F# interactive:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fae1e10d-8b89-42db-ba44-fd1f5d31b329" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Penguin() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Walk() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;waddle&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Swim() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;swimming&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Penguin &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Penguin
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; Swim : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; string
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; Walk : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; string
  end

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (pFly, pWalk) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; flyAndWalk(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Penguin());;

  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (pFly, pWalk) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; flyAndWalk(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Penguin());;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;-------------------------------^^^^^^^^^^^^^&lt;/span&gt;&lt;span style="color:#000000;"&gt;

stdin(&lt;/span&gt;&lt;span style="color:#800080;"&gt;84&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800080;"&gt;32&lt;/span&gt;&lt;span style="color:#000000;"&gt;): error FS0001: The &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Penguin&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt; does not support any operators na
med &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Fly&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, it’s a rather powerful concept, when used in moderation, can help you solve some interesting problems such as implicit operators as well as other scenarios.&lt;/p&gt;

&lt;h2&gt;Structural Typing&lt;/h2&gt;

&lt;p&gt;There is a concept that is prevalent in languages such as OCaml, ML, and Scala for &lt;a href="http://en.wikipedia.org/wiki/Structural_typing"&gt;Structural Typing&lt;/a&gt; as opposed to Duck Typing.&amp;#160; This is a concept in which compatibility and equivalence is based upon the type’s structure and not the explicit declarations as I’ve had above.&amp;#160; Equivalence is then measured by the structural sameness between the two even though they may be nothing more than anonymous objects.&lt;/p&gt;

&lt;p&gt;In F#, we can accomplish this in a sense through the use of the record type quite easily.&amp;#160; For example, we can define a class that has two properties and then check for sameness between them.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b8680d2-d9f3-4466-a962-2d9515556c22" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Foo &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; { X : int; Y : int};;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Foo &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  {X: int;
   Y: int;}

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; {X&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800080;"&gt;32&lt;/span&gt;&lt;span style="color:#000000;"&gt;;Y&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800080;"&gt;45&lt;/span&gt;&lt;span style="color:#000000;"&gt;} &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; {X&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800080;"&gt;32&lt;/span&gt;&lt;span style="color:#000000;"&gt;;Y&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800080;"&gt;45&lt;/span&gt;&lt;span style="color:#000000;"&gt;};;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : bool &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Normally, given that these are reference types and not standard structures, they shouldn’t ordinarily equate one to another automatically, but F# through the use of structural typing, does allow for this.&amp;#160; The same also applies for Discriminated Unions as well such as the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:31c6ebb8-6cd2-4d33-b245-bdda90186ef2" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; DU &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; A &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;of&lt;/span&gt;&lt;span style="color:#000000;"&gt; string &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; B &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;of&lt;/span&gt;&lt;span style="color:#000000;"&gt; int;;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; DU &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; A &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;of&lt;/span&gt;&lt;span style="color:#000000;"&gt; string
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; B &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;of&lt;/span&gt;&lt;span style="color:#000000;"&gt; int

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; B &lt;/span&gt;&lt;span style="color:#800080;"&gt;42&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; B &lt;/span&gt;&lt;span style="color:#800080;"&gt;42&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : bool &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once again, since they are reference types, they shouldn’t equal each other, but with F#, they do, through the use of structural typing.&amp;#160; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Through the use of structural typing and duck typing, we realize how flexible the F# language really is.&amp;#160; This gives us the ability to be creative in how we solve our problems generically in specialized cases such as our LINQ to XML example from above.&amp;#160; Now that F# is a full citizen in the .NET space with Visual Studio 2010, it’s even more exciting about what we can do with the language.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=248422" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/rbVyJs1ulRI" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/OOP/default.aspx">OOP</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/11/f-duck-typing-and-structural-typing.aspx</feedburner:origLink></item></channel></rss>
