<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>dev{shaped} &#187; MAXoutput</title>
	<atom:link href="http://devshaped.com/category/maxoutput/feed/" rel="self" type="application/rss+xml" />
	<link>http://devshaped.com</link>
	<description></description>
	<lastBuildDate>Mon, 13 Jul 2009 14:47:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- podcast_generator="podPress/8.8" -->
		<copyright>&#xA9; </copyright>
		<managingEditor>derek@webradius.com ()</managingEditor>
		<webMaster>derek@webradius.com()</webMaster>
		<category></category>
		<itunes:keywords></itunes:keywords>
		<itunes:subtitle></itunes:subtitle>
		<itunes:summary></itunes:summary>
		<itunes:author></itunes:author>
		<itunes:category text="Society &amp; Culture"/>
		<itunes:owner>
			<itunes:name></itunes:name>
			<itunes:email>derek@webradius.com</itunes:email>
		</itunes:owner>
		<itunes:block>No</itunes:block>
		<itunes:explicit>no</itunes:explicit>
		<itunes:image href="http://devshaped.com/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<image>
			<url>http://devshaped.com/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
			<title>dev{shaped}</title>
			<link>http://devshaped.com</link>
			<width>144</width>
			<height>144</height>
		</image>
		<item>
		<title>Efficiency Upgrade</title>
		<link>http://devshaped.com/2009/05/efficiency-upgrade/</link>
		<comments>http://devshaped.com/2009/05/efficiency-upgrade/#comments</comments>
		<pubDate>Wed, 13 May 2009 00:59:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[MAXoutput]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://devshaped.com/?p=179</guid>
		<description><![CDATA[by Derek Hatchard
 Developers solve problems. Good developers solve problems efficiently. The reward for that efficiency is [insert your preferred reward here]. Let&#8217;s assume you&#8217;re simply motivated by the satisfaction of a job well done (or maybe a promotion, a raise, longer lunch breaks, or just some extra time with your family). Whatever the motivation, [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://derekh.com/" target="_blank">Derek Hatchard</a></p>
<p><img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 22px 22px; display: inline; border-top: 0px; border-right: 0px" border="0" align="right" src="http://devshaped.com/wp-content/uploads/2009/01/derek-hatchard.jpg" /> Developers solve problems. Good developers solve problems efficiently. The reward for that efficiency is [insert your preferred reward here]. Let&#8217;s assume you&#8217;re simply motivated by the satisfaction of a job well done (or maybe a promotion, a raise, longer lunch breaks, or just some extra time with your family). Whatever the motivation, the secret to solving problems efficiently is not inventing everything from scratch. Certainly the secret is not spinning your mental wheels hoping for inspiration. And most definitely the secret is not using Intellisense to arbitrarily try classes, methods, and properties until something works.</p>
<p>The secret to solving development problems efficiently is having the right information and the means to find the information you don&#8217;t have.</p>
<p><span id="more-179"></span></p>
<p>If you have been developing software for more than a week, you know that a working knowledge of language syntax and program construction (that is, &quot;programming&quot;) is only a small part of the necessary skill set for solving problems with software. As a developer, you need to understand the technologies, processes, and methods available to you. You also need to have a set of reliable resources you can turn to when you encounter problems.</p>
<p>Below you will find some resource recommendations to help you find the information you need to help solve development problems more efficiently so you can get on with living the rest of your life.</p>
<h1>Finding Answers to Specific Problems</h1>
<h2>Search Engines</h2>
<p>I have heard colleagues muse about the changing role of &quot;software developer,&quot; postulating that development has become a job mostly concerned with research and discovery. While that is no doubt an overstatement, the ability to find information quickly is an important skill. The place many of us turn to first is our search engine of choice whether it&#8217;s Live Search, Google, or a meta search site like Dogpile.</p>
<p>The appeal of most search engines is the beautiful simplicity of a single search box, but the needs of a developer are not simple when you factor in multiple versions of frameworks, programming languages with similar constructs, and behavior differences between competing products (I&#8217;m looking at you, Structured Query Language and Cascading Style Sheets). With just a short time investment, you can learn some advanced search operators and tricks to help narrow search results to exactly the information you need. For example, search for &quot;live advanced search keywords&quot; or &quot;google advanced operators&quot; and you&#8217;ll find a bunch of operators to help you become a search ninja.</p>
<p>In addition to your search engine of choice, Wikipedia can be an invaluable problem solving tool. I have found Wikipedia to be especially useful when searching for general topics such as common computer science algorithms and concepts. Although I often end up at Wikipedia through search engine results, Wikipedia has its own search box right on the home page (<a title="http://wikipedia.org/" href="http://wikipedia.org/">http://wikipedia.org/</a>).</p>
<p>Of course not all problems will have solutions waiting to be discovered via a search engine. Some problems have already been solved in another part of your organization and you simply have no idea about the pre-existing solution. There are many tools designed to help companies deal with code asset management and search behind the firewall. One interesting product is Krugle, a specialty search engine built for indexing and searching code.</p>
<h2>Stack Overflow</h2>
<p>Searching is great when you know exactly what to search for and somebody else has previously solved a similar problem. When you are dealing with a more difficult issue, you may want to pose your specific problem to other developers for advice. There are plenty of discussion boards and forums for developers on the Internet to ask and answer questions.</p>
<p>One intriguing and useful site is Stack Overflow, a &quot;collaboratively edited question and answer site for programmers.&quot; This innovative developer Q&amp;A site allows users to post responses to questions and vote answers up or down based on usefulness, accuracy, etc. In addition to posting questions, you can search Stack Overflow or browse posted questions by tag. When you are posting a question on Stack Overflow, the application automatically searches existing questions and shows you matches that might already contain the answer you seek.</p>
<p>For obscure topics and scenarios such as bizarre error codes, Stack Overflow can be a job saver.</p>
<h2>Twitter</h2>
<p>Microblogging seems to be all the rage these days and some developers find services like Twitter to be an extremely effective way to share tips and send out requests for help. Twitter and similar services can be a two-edged sword, though, as they can be as much a distraction as a tool for information exchange.</p>
<p>If you are not familiar with Twitter, it lets you follow posts or &quot;tweets&quot; from other Twitter users. Tweets can be no more than 140 characters. You choose who you want to follow.</p>
<p>There are plenty of ways to use Twitter that will surely reduce your efficiency, but with a bit of intentionality in your approach, you can build a reasonable size network of trusted colleagues with whom you can swap questions, answers, tips, and tricks. (When your friends hear you are &quot;on Twitter,&quot; they may demand to know your Twitter username. You might need to maintain separate Twitter accounts for work and personal use.)</p>
<h2>Officemates, Coworkers, and IM Contacts</h2>
<p>If you are anything like me, you find it tempting to just ask an officemate or IM contact for an answer. Of course this means solving my problem at the expense of my colleague who is distracted from doing his/her own work. With your recently acquired ninja search engine skills, take a look at some of the research on the impact of interruptions on productivity and ponder how a work culture that accepts interruptions as common practice could ultimately make you a less effective developer. Just something to think about before you start picking the brains of your cubicle mates.</p>
<h2>Other Options</h2>
<p>What sources do you rely on for finding answers to specific problems? Leave a comment below.</p>
<h1>Keeping Up with New Technologies</h1>
<p>With the rapid and continuous rate of change in the software development space, keeping up with the latest and greatest technologies could be a full-time job – in fact it is in many organizations! It really has become impossible to keep up with everything, but you should not let the fear of information overload prevent you from staying informed about significant changes directly related to your area of expertise. Being informed about new technologies helps you make informed choices about the tools, platforms, and libraries you will use to get from problem to solution.</p>
<h2>Blogs</h2>
<p>One easy way to stay informed about topics that might affect you is to subscribe to at least a few blogs in your area. Scott Guthrie&#8217;s blog is a good choice if you only want to get the big headline news like a new release of the .NET Framework (<a title="http://weblogs.asp.net/scottgu/" href="http://weblogs.asp.net/scottgu/">http://weblogs.asp.net/scottgu/</a>). If you&#8217;re a Silverlight developer, Tim Heuer&#8217;s blog would be a good choice (<a title="http://timheuer.com/blog/" href="http://timheuer.com/blog/">http://timheuer.com/blog/</a>). If you&#8217;re an ASP.NET developer, perhaps try the Visual Web Developer Team Blog (<a title="http://blogs.msdn.com/webdevtools/" href="http://blogs.msdn.com/webdevtools/">http://blogs.msdn.com/webdevtools/</a>). </p>
<p>If checking blogs via a blog reader is not your thing, you might want to use a service that will deliver new blog posts to you via email.&#160; FeedBlitz (<a title="http://www.feedblitz.com/" href="http://www.feedblitz.com/">http://www.feedblitz.com/</a>) is a service I use for several blogs that I want to track even when I&#8217;m too busy to spend time reading my larger blog subscription list. Or you might want to add a few feeds to a customized browser home page through a service like Pageflakes or Netvibes.</p>
<h2>Podcasts</h2>
<p>Another way to stay current on technology is by listening to audio podcasts like .NET Rocks (<a title="http://dotnetrocks.com/" href="http://dotnetrocks.com/">http://dotnetrocks.com/</a>) and Hanselminutes (<a title="http://hanselminutes.com/" href="http://hanselminutes.com/">http://hanselminutes.com/</a>). Internet audio shows like these are generally packed with great technical content that helps keep you up to date. Podcasts can be an easy way to transform a tedious commute into a serious professional development opportunity. Personally I use the Zune desktop software (<a title="http://zune.net/" href="http://zune.net/">http://zune.net/</a>) to automatically download new podcast episodes and sync them to my Zune device. If you don&#8217;t have a Zune, you can still grab the MP3 files from the Zune download folder and copy them to any portable media player.</p>
<h2>User Groups</h2>
<p>User groups are another great way to stay current on new technologies. User groups are community-run technical groups that hold events (both in-person and virtual) for members to learn about technologies related to the focus area(s) of the groups. User group events often happen outside of normal work hours, which can admittedly make it difficult for some people to attend. In addition to learning opportunities, user group events can be a great place to discuss technical issues you are tackling and increase the size of your network of colleagues that you can turn to for help.</p>
<p>Go to <a title="http://www.ineta.org/" href="http://www.ineta.org/">http://www.ineta.org/</a> to find details (including meeting times and places) for .NET-focused user groups in your neck of the woods.</p>
<h1>Challenging Your Thinking</h1>
<p>When building software, your existing methods for thinking about problems are used to design a solution based on the tools and information you have available. Too many times I have seen developers stuck in a mental rut and developing solutions inefficiently because of outdated or misguided thinking about the problem at hand. Case in point: I used to spend far too much time building and deploying simple tools and utilities as compiled executables until I learned to embrace scripting as a fully legitimate form of programming (after having turned my back on my past as a UNIX scripting hack). In part my thinking was influenced by the experiences of other developers who were sharing their experiences online.</p>
<p>If you do not have people challenging your think, I believe you will stagnate as a developer. That means talking to or reading the musings of other developers. Ideally you will find yourself disagreeing with or questioning their thoughts on a regular basis. You aren&#8217;t being challenged if you only read what likeminded people write.</p>
<p>Below are some sources that I find useful in challenging my thinking and assumptions. They might not work for you, which is fine, but I encourage you to find your own trusted sources that will regularly challenge your thinking.</p>
<p><strong>CodingHorror.com</strong> is written by the brilliant and prolific Jeff Atwood. It has been a constant trove of insight and reasoned perspective, ranging from discussions of technical details to choosing a programming chair. (Jeff is one of the founders of the abovementioned Stack Overflow.)</p>
<p><strong>JoelOnSoftware.com</strong> is written by the equally brilliant Joel Spolsky. He is the source of such gems as &quot;The Joel Test: 12 Steps to Better Code&quot; and &quot;Things You Should Never Do.&quot; In addition to development topics, Joel writes about management and business issues. (Joel is the other founder of Stack Overflow.&#160; Hmmm&#8230; perhaps I have a bias.)</p>
<p><strong>Ted Neward</strong> writes a technical blog (<a title="http://blogs.tedneward.com/" href="http://blogs.tedneward.com/">http://blogs.tedneward.com/</a>) that I regularly enjoy. Although I don&#8217;t read every post, I always find his pontifications worth digesting.</p>
<p><strong>Daniel Crenna </strong>(<a href="http://dimebrain.com/blog">http://dimebrain.com/blog</a>) is another technical blogger whose musings I enjoy reading. Daniel is a great community champion and the founder of several open source projects. His blogging pace has slowed down in 2009 as he&#8217;s been working on some useful projects, but I keep hoping he&#8217;ll starting writing again soon. He&#8217;s a web / RIA guy, an area I&#8217;m interested in, so he stays in the ol&#8217; blog list.</p>
<h1>Join the Conversation</h1>
<p>What do you do to be more efficient as a developer? What are your favorite resources for challenging your thinking? How do you keep up on technology changes in your area of expertise? How do you find solutions to specific problems? Leave a comment to share <strong>YOUR</strong> tips with us.</p>
</p>
<p>&#8212;&#8211;</p>
<p><em>Derek Hatchard is the content editor for </em><a href="http://microsoft.com/youshapeit/msdn"><em>http://microsoft.com/youshapeit/msdn</em></a><em> and </em><a href="http://devshaped.com"><em>http://devshaped.com</em></a><em>. He is also the founder of </em><a href="http://crowdspace.net/"><em>Crowd Space</em></a><em>. You can find him online at </em><a href="http://derekh.com"><em>http://derekh.com</em></a><em>, </em><a href="http://ardentdev.com"><em>http://ardentdev.com</em></a><em>, and </em><a href="http://twitter.com/derekhat"><em>http://twitter.com/derekhat</em></a><em>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/05/efficiency-upgrade/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting Started with Continuous Integration</title>
		<link>http://devshaped.com/2009/04/getting-started-with-continuous-integration/</link>
		<comments>http://devshaped.com/2009/04/getting-started-with-continuous-integration/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 09:00:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[MAXoutput]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[continuous integration]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/04/getting-started-with-continuous-integration/</guid>
		<description><![CDATA[by Sondre Bjellås
 Continuous Integration is a development practice that can help improve your company&#8217;s ability to deliver quality software. It can reduce the time to market and the deployment cycle from functional complete products to having the solutions deployed on desktop or servers.
Continuous Integration, or CI, can start with a lone developer desktop and [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://sondreb.com/blog/" target="_blank">Sondre Bjellås</a></p>
<p><img style="border-right-width: 0px; margin: 0px 0px 22px 22px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sondre-bjellas" border="0" alt="sondre-bjellas" align="right" src="http://devshaped.com/wp-content/uploads/2009/03/sondrebjellas.jpg" width="85" height="57" /> Continuous Integration is a development practice that can help improve your company&#8217;s ability to deliver quality software. It can reduce the time to market and the deployment cycle from functional complete products to having the solutions deployed on desktop or servers.</p>
<p>Continuous Integration, or CI, can start with a lone developer desktop and mature closer towards production. The ultimate goal is to have a CI environment that can take you from checked-in code to production ready installation packages.</p>
<p><strong>The instructions in this article will help you get started with CI even if you&#8217;re a single developer or a small team.</strong></p>
<p>This is a process that takes time, but don&#8217;t be afraid of the road ahead, I will walk you through the first baby-steps towards a well-tuned machine. CI-practice is one of many important elements for the software development industry to mature to levels of other types of manufacturing.</p>
<p><span id="more-159"></span></p>
<h2><b>Introduction to Continuous Integration</b></h2>
<p>Martin Fowler has written a very good introduction to CI which you can read this on his website: <a href="http://martinfowler.com/articles/continuousIntegration.html">http://martinfowler.com/articles/continuousIntegration.html</a> [1]</p>
<p><i>Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily &#8211; leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly. This article is a quick overview of Continuous Integration summarizing the technique and its current usage.</i></p>
<p>The clean and simple essence of CI is to avoid the situation <i>“It works on my machine”</i>. This has caused a lot of pain between developer and users, but also between developers. Most of us have experienced that our projects stops compiling properly after a <i>Get Latest</i> operation on the source control system. Another important aspect of CI is to have a cycles of automatic compilation and testing of your projects. We will get deeper into automatic testing later in this article.</p>
<h2><b>Source Control Systems</b></h2>
<p>If you are not familiar with source control systems (SCM), I suggest reading a good introduction by Eric Sink. While the first CI example I will give in this article doesn’t use SCM, it is an essential piece of the puzzle if you&#8217;re doing computer software development. Even as a single developer, it&#8217;s advisable to use a SCM. SCM will give you better control and management of your projects, with history and the ability to restore deleted code.</p>
<p>Introduction to SCM: <a href="http://www.ericsink.com/scm/scm_intro.html">http://www.ericsink.com/scm/scm_intro.html</a> [2]</p>
<p>There are many providers of SCM software; for the purpose of this article we will use Microsoft Visual SourceSafe. When your team grows and you have the finances to purchase licenses, I suggest upgrading to Microsoft Visual Studio Team System, which is a complete Application Life-Cycle Management (ALM) suite.</p>
<h2><b>Setting up your environment</b></h2>
<p>There are various CI solutions available for .NET developers, one of the most popular is Cruise Control .NET (<a href="http://ccnet.thoughtworks.com/">http://ccnet.thoughtworks.com/</a> [3]).</p>
<p>[Please note that all examples are from a 64-bit system where the Program Files paths includes <i>(x86)</i> for 32-bit applications. If you have a 32-bit environment, please make sure you remove any reference to <i>(x86)</i>.]</p>
<p>Cruice Control .NET (CC.NET) comes in various forms, for beginners the setup alternative is the simplest to get you started (CruiseControl.NET-1.4.2-Setup.exe).</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0021.jpg" width="300" height="232" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0041.jpg" width="300" height="232" /></p>
<p>After the installation has completed, a new folder will appear in the Windows start menu. From here we can access the main configuration file and launch the console application.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0061.jpg" width="270" height="119" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0081.jpg" width="500" height="206" /></p>
<p>Before we continue with updates to our configuration file, we need to learn a little bit about build tools…</p>
<h2><b>Build Tools</b></h2>
<p>To compile the source code, you will need a tool that does the build for you. One alternative is NAnt, which is a free tool (<a href="http://nant.sourceforge.net/">http://nant.sourceforge.net/</a> [4]).</p>
<p>With Visual Studio 2005 Microsoft introduced MSBuild, which is a standalone build engine for .NET. This has made it more accessible for third parties and made it simpler to automate and script.</p>
<p>The simplest way to get started with a build process is to utilize MSBuild, which is already installed if you have .NET Framework 2.0. We will configure CC.NET to start the MSBuild process which takes care of the compilation of our Visual Studio Solution.</p>
<h2><b>New Project or Existing Solutions</b></h2>
<p>It is much less effort to include guidance, frameworks, and tools in the beginning of a new project. This applies to CI as well. The further you get in the development process, the harder it gets to make everything work properly in a CI-environment.</p>
<h2><b>Sample Projects</b></h2>
<p>For the purpose of this article, we’ll create a new Visual Studio solution called <i>IntegrationSample</i>. Within this solution we will create 3 projects: <i>IntegrationSample</i> (Windows Forms), <i>Integration.Common</i> (Class Library) and <i>Integration.Test</i> (Class library).</p>
<p>Add a <i>Person.cs</i> class to the Common project with the following code:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> Integration.Common     <br />{     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">class</span> Person     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> Firstname { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> Lastname { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span> DateTime Birthdate { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     </p>
<p>&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">bool</span> IsOlderThan60()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> (Birthdate &lt; DateTime.Now.AddYears(-<span style="color: maroon">60</span>));     <br />&#160;&#160;&#160; }     </p>
<p>&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">override</span>&#160;<span style="color: blue">string</span> ToString()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>&#160;<span style="color: blue">string</span>.Format(<span style="color: maroon">&quot;{0} {1}&quot;</span>, Firstname, Lastname);     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </div>
<p>This class represents part of our business domain model and has certain business logic applied to it that is important to our business, namely the method that tells us if a person is more than 60 years old. The next step is to apply a test for our domain object.</p>
<h2><b>Automated Testing with Unit Tests</b></h2>
<p>Being able to automate the testing process of your projects is an important feature of a CI-environment. Having source code that works as tests for your application logic will help you when you’re later going back to modify, extend and fix existing code. With good tests you can ensure that your code changes won&#8217;t break the whole application. There are different frameworks available for unit testing, the most widely used with .NET is NUnit (<a href="http://www.nunit.org">www.nunit.org</a> [5]). To learn more about test-driven development, visit Wikipedia: <a href="http://en.wikipedia.org/wiki/Test-driven_development">http://en.wikipedia.org/wiki/Test-driven_development</a> [6]</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0101.jpg" width="300" height="233" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image012" border="0" alt="clip_image012" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0121.jpg" width="300" height="234" /></p>
<p>When the installation of NUnit is complete, you can explore the included samples to learn more about writing unit tests. You can use the NUnit GUI to run your unit tests outside of the CI-environment.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image014" border="0" alt="clip_image014" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0141.jpg" width="244" height="176" /></p>
<p>To start writing unit tests, you need to add a reference to the following assembly in your <i>Integration.Test</i> project: &quot;C:\Program Files (x86)\NUnit 2.4.8\bin\nunit.framework.dll&quot;. Additionally you should create a reference between the Test and the Common project, so we can access our business domain model in the tests project.</p>
<p>When you’re done adding the reference, let’s create a class named <i>PersonTest.cs</i> which will contain all the unit tests for our specific domain object.</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">using</span> System;     <br /><span style="color: blue">using</span> NUnit.Framework;     <br /><span style="color: blue">using</span> Integration.Common;     </p>
<p><span style="color: blue">namespace</span> Integration.Test     <br />{     <br />&#160; [TestFixture]     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">class</span> PersonTest     <br />&#160; {     <br />&#160;&#160;&#160; [Test]     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">void</span> IsOlderThan60Test()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; Person person = <span style="color: blue">new</span> Person();     <br />&#160;&#160;&#160;&#160;&#160; person.Firstname = <span style="color: maroon">&quot;Sondre&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; person.Lastname = <span style="color: maroon">&quot;Bjellås&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; person.Birthdate = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1905</span>, <span style="color: maroon">4</span>, <span style="color: maroon">4</span>);     <br />&#160;&#160;&#160;&#160;&#160; Assert.IsTrue(person.IsOlderThan60());     </p>
<p>&#160;&#160;&#160;&#160;&#160; person.Birthdate = DateTime.Now;     <br />&#160;&#160;&#160;&#160;&#160; Assert.IsFalse(person.IsOlderThan60());     </p>
<p>&#160;&#160;&#160;&#160;&#160; person.Birthdate = DateTime.MaxValue;     <br />&#160;&#160;&#160;&#160;&#160; Assert.IsFalse(person.IsOlderThan60());     <br />&#160;&#160;&#160; }     </p>
<p>&#160;&#160;&#160; [Test]     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">void</span> ToStringTest()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; Person person = <span style="color: blue">new</span> Person();     <br />&#160;&#160;&#160;&#160;&#160; person.Firstname = <span style="color: maroon">&quot;Sondre&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; person.Lastname = <span style="color: maroon">&quot;Bjellås&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; Assert.AreEqual(person.ToString(), <span style="color: maroon">&quot;Sondre Bjellås&quot;</span>);     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </div>
<p>Tests are done through assertions. There are many types of assert methods included in the NUnit framework and you should use them to validate your business logic. Writing good unit tests is hard and it&#8217;s a practice in which you will continually improve as you get more experienced with it.</p>
<p>You can now begin to run and validate the unit tests on your local machine, open the NUnit GUI application and open the Integration.Test.dll file.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image0161.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image016" border="0" alt="clip_image016" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image016-thumb1.jpg" width="500" height="290" /></a></p>
<p>As you can see in my screenshot, both of the tests for the Person object validates. When we later go back to change the business logic, we can use the unit tests as a means to validate that our changes doesn’t break anything.</p>
<h2><b>Bringing it all together</b></h2>
<p>Let’s go back to the CC.NET configuration file and start modifying it so we get our CI-environment up and running. It&#8217;s advisable to have a dedicate computer that act as a build-computer, but it&#8217;s not a requirement.</p>
<p>The first thing we’re going to do is modify the default configuration file to run NUnit and our tests, here is the script so far:</p>
<pre style="font-size: 9pt"><span style="color: blue">&lt;</span><span style="color: maroon">cruisecontrol</span> <span style="color: red">xmlns:cb</span>=&quot;<span style="color: blue">urn:ccnet.config.builder</span>&quot;<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">project</span> <span style="color: red">name</span>=&quot;<span style="color: blue">IntegrationSample</span>&quot;<span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span><span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">path</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\NUnit 2.4.8\bin\nunit-console.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">path</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
     <span style="color: blue">&lt;</span><span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Integration.Test\bin\Debug\Integration.Test.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span>/<span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span>/<span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span>/<span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">cruisecontrol</span><span style="color: blue">&gt;</span></pre>
<p>Save the configuration and start the CruiceControl.NET application in the start menu. This will open a console application that will run your configuration. This is useful for beginners and when you’re setting up a new environment. When the configuration is complete, you should use the Windows Service for a more stable runtime.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image0181.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image018" border="0" alt="clip_image018" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image018-thumb1.jpg" width="500" height="252" /></a></p>
<p>When you ran the CC.NET installation, it should have created a virtual directory for you on the local Internet Information Services. If you happen to have any problems with this, make sure you create a virtual directory called webdashboard and map it to <i>C:\Program Files (x86)\CruiseControl.NET\webdashboard</i>.</p>
<p>Opening up the web dashboard should show us our IntegrationSample project and hopefully a green status, indicating that our unit tests has completed successfully. With the current configuration, we need to click the <i>Force</i> button to run tests.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image020.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image020" border="0" alt="clip_image020" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image020-thumb.jpg" width="500" height="246" /></a></p>
<p>This dashboard will be your viewport into the status and history of integration builds. You can drill down into the details of a build and you can have multiple CC.NET projects specified in the configuration file.</p>
<p>Our next step is to make CC.NET compile our source code using MSBuild.exe. </p>
<p>Within the &lt;tasks&gt; element in the configuration, add the following script above the NAnt task, which will tell CC.NET to execute MSBuild.exe with our Visual Studio solution file.</p>
<pre><span style="color: blue">&lt;</span><span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>IntegrationSample.sln<span style="color: blue">&lt;</span>/<span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>/noconsolelogger /p:Configuration=Debug /v:m<span style="color: blue">&lt;</span>/<span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">targets</span><span style="color: blue">&gt;</span>Build<span style="color: blue">&lt;</span>/<span style="color: maroon">targets</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>720<span style="color: blue">&lt;</span>/<span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">logger</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">logger</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span></pre>
<p>To validate that CC.NET is working correctly, we can go ahead and make a quick modification to the source code. If you reverse the operator for the age method, we should see the IntegrationSample fail on the dashboard. Drilling down into the details, we should see that our unit test fails.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image022.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image022" border="0" alt="clip_image022" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image022-thumb.jpg" width="504" height="244" /></a></p>
<p>The last and final step is to add support for a source control system. As you have now seen, you can setup an CI-environment without source control, but it makes sense to use source control. A source control system adds history and backup support and the ability to make heavy and local changes and not disrupt the active codebase which is inside the source control database.</p>
<h2><b>Visual SourceSafe 2005</b></h2>
<p>If you don&#8217;t have a source control system yet, an easy way to get started is with Microsoft Visual SourceSafe 2005. With an MSDN Subscription, you can download it from the downloads page.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image024" border="0" alt="clip_image024" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image024.jpg" width="300" height="230" /></p>
<p>When you go beyond a small team of programmers I suggest upgrading the source control with a more modern provider, like Visual Studio Team System or SourceGear [7].</p>
<p>Going back to Visual Studio and our <i>IntegrationSample</i> project, we will go ahead and add it to our source control provider of choice.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image026" border="0" alt="clip_image026" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image026.jpg" width="304" height="98" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image028" border="0" alt="clip_image028" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image028.jpg" width="300" height="238" /></p>
<p>When the solution and all the projects is added to the source control, we can open the Visual SourceSafe Explorer and see how our newly added project is added and we can begin to check-out and check-in code changes.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image030" border="0" alt="clip_image030" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image030.jpg" width="500" height="248" /></p>
<p>We’re now ready for the final changes to our ccnet.config file. Open up the configuration file from the start menu and add the following new elements to the top beneath the &lt;project&gt; element.</p>
<pre><span style="color: blue">&lt;</span><span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">intervalTrigger</span> <span style="color: red">seconds</span>=&quot;<span style="color: blue">60</span>&quot; /<span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>

<span style="color: blue"> &lt;</span><span style="color: maroon">sourcecontrol</span> <span style="color: red">type</span>=&quot;<span style="color: blue">vss</span>&quot; <span style="color: red">autoGetSource</span>=&quot;<span style="color: blue">true</span>&quot; <span style="color: red">applyLabel</span>=&quot;<span style="color: blue">true</span>&quot;<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\Microsoft Visual SourceSafe\ss.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">project</span><span style="color: blue">&gt;</span>$/IntegrationSample<span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">username</span><span style="color: blue">&gt;</span>build<span style="color: blue">&lt;</span>/<span style="color: maroon">username</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">password</span><span style="color: blue">&gt;</span>Password123<span style="color: blue">&lt;</span>/<span style="color: maroon">password</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span>C:\Source\VSS<span style="color: blue">&lt;</span>/<span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span> <span style="color: blue">&lt;</span>!-- Path to your SourceSafe database --<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>false<span style="color: blue">&lt;</span>/<span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">sourcecontrol</span><span style="color: blue">&gt;</span></pre>
<p>Make sure you use a different working directory than your normal project folder, if you are running CC.NET on the same machine that you&#8217;re developing on. You also need to modify the &lt;workingDirectory&gt; and &lt;assembly&gt; elements we created earlier. See the bottom of the article for a full version of our build script.</p>
<p>When you start CruiceControl.NET again, you should see something similar to this output. Please beware that it can take a short while for the process to start.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image032.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image032" border="0" alt="clip_image032" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image032-thumb.jpg" width="500" height="238" /></a></p>
<p>As you can see, the script finds that there has been modification in the project inside our source control, which triggers a get process of the source code and later compilation and test-run.</p>
<p>You can specify your own trigger interval in the configuration file, depending on the size of your development team and your software, you can tune it to make sure you build often, but not so much that your build server starts having trouble.</p>
<p>You should create a special user account that your build process can run as and access the source control, as you can see in the example above I created a user called <i>build</i>.</p>
<p>Now it&#8217;s time to test the whole cycle of editing source code, check-in, watch the build server figure out there has been a modification and trigger a compilation and test-run. Let&#8217;s check out the Person.cs file and modify the age check to make our unit tests fail.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image034.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image034" border="0" alt="clip_image034" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image034-thumb.jpg" width="244" height="176" /></a></p>
<div style="font-family: courier new"><span style="color: blue">public</span>&#160;<span style="color: blue">bool</span> IsOlderThan60() </p>
<p>{ </p>
<p>&#160; <span style="color: blue">return</span> (Birthdate &gt; DateTime.Now.AddYears(-<span style="color: maroon">60</span>)); </p>
<p>} </p>
<p></div>
<p>When you’re checking code back into the source control system, it’s good practice to add an informative comment. This is useful for historical purposes and for other team members to get an overview of changes in the history view.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image036" border="0" alt="clip_image036" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image036.jpg" width="500" height="269" /></p>
<p>After checking in our changes, we only need to wait a few seconds to see that CC.NET discovers our change and runs our script.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image038.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image038" border="0" alt="clip_image038" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image038-thumb.jpg" width="500" height="123" /></a></p>
<p>Congratulations, you now have a CI-environment up and running!</p>
<h2><b>Going Forward</b></h2>
<p>We have looked at the base tools in a CI-environment, going forward I suggest taking a look at frameworks and tools that works good together with CC.NET. FxCop [8] is a tool from Microsoft that analyzes your assemblies and reports anything that is not according to the industry standards for naming conventions, etc. NCover [9] is a tool which will generate code coverage reports (that means it will highlight which application logic in your application is being covered by unit tests). If you’re a component shop, API documentation is important. Using tools like SandCastle [10] you can automatically generate help files from your XML comments.</p>
<p>There is a whole world to explore and the extension ability to your CI-environment is endless and everything is there to help you improve the process of building good software with high quality.</p>
<p>You should now understand how to setup your own CI-environment using free tools. Best of luck in setting up your own environment! </p>
<h2><b>References:</b></h2>
<p>[1] <a href="http://martinfowler.com/articles/continuousIntegration.html">http://martinfowler.com/articles/continuousIntegration.html</a></p>
<p>[2]<a href="http://www.ericsink.com/scm/scm_intro.html">http://www.ericsink.com/scm/scm_intro.html</a></p>
<p>[3] <a href="http://ccnet.thoughtworks.com/">http://ccnet.thoughtworks.com/</a></p>
<p>[4] <a href="http://nant.sourceforge.net/">http://nant.sourceforge.net/</a></p>
<p>[5] <a href="http://www.nunit.org/">http://www.nunit.org/</a></p>
<p>[6] <a href="http://en.wikipedia.org/wiki/Test-driven_development">http://en.wikipedia.org/wiki/Test-driven_development</a></p>
<p>[7] <a href="http://www.sourcegear.com/">http://www.sourcegear.com/</a></p>
<p>[8] <a href="http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx">http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx</a></p>
<p>[9] <a href="http://www.ncover.com/">http://www.ncover.com/</a></p>
<p>[10] <a href="http://www.codeplex.com/Sandcastle">http://www.codeplex.com/Sandcastle</a></p>
<h2><b>Sample Soure Code</b> (includes the ccnet.config build script):</h2>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/IntegrationSample.zip" target="_blank">IntegrationSample.zip</a></p>
<h2><b>Build Script</b></h2>
<pre><span style="color: blue">&lt;</span><span style="color: maroon">cruisecontrol</span> <span style="color: red">xmlns:cb</span>=&quot;<span style="color: blue">urn:ccnet.config.builder</span>&quot;<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">project</span> <span style="color: red">name</span>=&quot;<span style="color: blue">IntegrationSample</span>&quot;<span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span><span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">intervalTrigger</span> <span style="color: red">seconds</span>=&quot;<span style="color: blue">60</span>&quot; /<span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span>/<span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span><span style="color: maroon">sourcecontrol</span> <span style="color: red">type</span>=&quot;<span style="color: blue">vss</span>&quot; <span style="color: red">autoGetSource</span>=&quot;<span style="color: blue">true</span>&quot; <span style="color: red">applyLabel</span>=&quot;<span style="color: blue">true</span>&quot;<span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\Microsoft Visual SourceSafe\ss.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">project</span><span style="color: blue">&gt;</span>$/IntegrationSample<span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">username</span><span style="color: blue">&gt;</span>build<span style="color: blue">&lt;</span>/<span style="color: maroon">username</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">password</span><span style="color: blue">&gt;</span>Password123<span style="color: blue">&lt;</span>/<span style="color: maroon">password</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span>C:\Source\VSS<span style="color: blue">&lt;</span>/<span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span> <span style="color: blue">&lt;</span>!-- Path to your SourceSafe database --<span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>false<span style="color: blue">&lt;</span>/<span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span>/<span style="color: maroon">sourcecontrol</span><span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span><span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>

   <span style="color: blue">&lt;</span><span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\IntegrationSample\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>IntegrationSample.sln<span style="color: blue">&lt;</span>/<span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>/noconsolelogger /p:Configuration=Debug /v:m<span style="color: blue">&lt;</span>/<span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">targets</span><span style="color: blue">&gt;</span>Build<span style="color: blue">&lt;</span>/<span style="color: maroon">targets</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>720<span style="color: blue">&lt;</span>/<span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">logger</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">logger</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span>/<span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span>

   <span style="color: blue">&lt;</span><span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">path</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\NUnit 2.4.8\bin\nunit-console.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">path</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
     <span style="color: blue">&lt;</span><span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\IntegrationSample\Integration.Test\bin\Debug\Integration.Test.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span>/<span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span>/<span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span>/<span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">cruisecontrol</span><span style="color: blue">&gt;</span></pre>
<p>&#8212;&#8211;</p>
<p>Sondre is a <a href="http://www.microsoftregionaldirectors.com/">Microsoft Regional Director</a> with 11 years of experience in software and web development. He currently works as Technology Leader for the Microsoft department at <a href="http://www.capgemini.no/">Capgemini</a> in Oslo, Norway. Sondre is additionally the leader of the Norwegian .NET User Group in Oslo and a speaker at various technology and software development events. Active blogger, writer and technology enthusiast with a special passion for robotics, intelligent systems and open source.</p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/04/getting-started-with-continuous-integration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# Features You Should Be Using</title>
		<link>http://devshaped.com/2009/03/c-features-you-should-be-using/</link>
		<comments>http://devshaped.com/2009/03/c-features-you-should-be-using/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 10:00:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[MAXoutput]]></category>

		<guid isPermaLink="false">http://devshaped.com/?p=38</guid>
		<description><![CDATA[by Ted Neward
 When Microsoft first introduced C# to the masses in 2000, it was, in many respects, a language that anyone who&#8217;d ever worked with Java or C++ could pick up, learn, and be a productive developer after only a few hours of study. Granted, there were a few subtleties that took a bit [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://blogs.tedneward.com/" target="_blank">Ted Neward</a></p>
<p><img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 22px 22px; display: inline; border-top: 0px; border-right: 0px" title="ted_neward" border="0" alt="ted_neward" align="right" src="http://devshaped.com/wp-content/uploads/2009/02/ted-neward1.jpg" width="85" height="57" /> When Microsoft first introduced C# to the masses in 2000, it was, in many respects, a language that anyone who&#8217;d ever worked with Java or C++ could pick up, learn, and be a productive developer after only a few hours of study. Granted, there were a few subtleties that took a bit of getting used to, like events, but on the whole, the C# language emerged as a pretty close descendant of its predecessors.</p>
<p>But as C# developed, first in the 2.0 release and then in the 3.0 release, the language took a sharp turn from its ancestral legacy into some strange territory by comparison &#8212; first with enumerators in 2.0, then lambda expressions in 3.0 &#8212; and a number of .NET developers found themselves longing for &quot;the good old days&quot; of C#, worrying that the language was too complicated and painful to use now. Granted, the LINQ features of C# were widely and warmly received, but some of the underlying features that made LINQ possible were barely understood, and to this day, rarely used outside of LINQ.</p>
<p>It&#8217;s time to put that trend where it belongs &#8212; out to pasture. Let&#8217;s take a look at the top 6 features of C# that you should be using if you&#8217;re not already. And while we&#8217;re at it, let&#8217;s see if we can spare your fingers some trauma, by reducing the number of keystrokes required to get something done.</p>
<p><span id="more-38"></span></p>
<h2>Auto-Generated Properties (3.0)</h2>
<p>This is an easy one, a natural time-saver. How many times haven&#8217;t you written this kind of code?</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">public</span>&#160;<span style="color: blue">class</span> Person     <br />{     <br />&#160; <span style="color: blue">private</span>&#160;<span style="color: blue">string</span> m_FirstName;     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> FirstName     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">get</span> { <span style="color: blue">return</span> m_FirstName; }     <br />&#160;&#160;&#160; <span style="color: blue">set</span> { m_FirstName = value; }     <br />&#160; }     </p>
<p><span style="color: green">// &#8230;</span>     <br />}</div>
<p>Oh, sure, I know, several plugins for Visual Studio will generate those property definitions for you, but let&#8217;s be honest &#8212; any time an IDE feature has to write code for you, that&#8217;s a bad sign. How much nicer is it to just let the language do that for you (since that&#8217;s what languages do best):</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">public</span>&#160;<span style="color: blue">class</span> Person     <br />{     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> FirstName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     <br />}</div>
<p>Fingers feeling less traumatized already? The best part is, there&#8217;s no difference: The compiler will &quot;cook up&quot; the private field and property &quot;get&quot; and &quot;set&quot; blocks for you, doing exactly what you would have. Incidentally, this also solves the &quot;Should I use the field or the property when I&#8217;m inside the class&quot; dilemma, since now the only access is through the property, making the code easier to maintain when you do, finally, have to make the properties more than just get/set.</p>
</p>
<h2>Object Initializers (3.0)</h2>
<p>Along the same lines as auto-generated properties, the object initializer is another way the language saves you some finger trauma. Thanks to the prevalence of properties and dearth of intelligent constructors, a lot of .NET code looks a lot like this:</p>
<div style="font-family: courier new; font-size: 9pt">Person p = <span style="color: blue">new</span> Person();     <br />p.FirstName = <span style="color: maroon">&quot;Katie&quot;</span>;     <br />p.LastName = <span style="color: maroon">&quot;Ellison&quot;</span>;     <br />p.Gender = Gender.FEMALE;     <br />p.Age = <span style="color: maroon">25</span>;</div>
<p>In other words, the &quot;new-then-prepare-before-use&quot; idiom. (Don&#8217;t feel bad if you&#8217;ve been doing this without even realizing it &#8212; lots of C++ and Java code look exactly the same). Fortunately, the object initializer can write all the same code in a slightly terser manner:</p>
<div style="font-family: courier new; font-size: 9pt">Person p = <span style="color: blue">new</span> Person { FirstName = <span style="color: maroon">&quot;Katie&quot;</span>, LastName = <span style="color: maroon">&quot;Ellison&quot;</span>,     <br />Gender = Gender.FEMALE, Age = <span style="color: maroon">25</span>};</div>
<p>Now, granted, it&#8217;s not a huge savings in terms of keystrokes, but it does help stress the &quot;prepare-before-use&quot; nature that constructors are supposed to infer. It also makes it easier to write data-only objects, such as data transfer objects (DTOs), since now you have one less constructor that needs to be written, at a minimum. In fact, if all the constructor is doing is passing values to those same properties, leave it off altogether. This also helps avoid writing four or five (or more) different constructors to cover all the possible intialization permutations, along the lines of constructors that take &quot;FirstName and LastName&quot;, &quot;FirstName, LastName and Age&quot;, &quot;FirstName, LastName and Gender&quot;, and so on.</p>
<h2>Implicitly Typed Local Variables (3.0)</h2>
<p>This one is simple: instead of writing:</p>
<div style="font-family: courier new; font-size: 9pt">Dictionary&lt;Person, List&lt;Person&gt;&gt; departmentList = </div>
<div style="font-family: courier new; font-size: 9pt">&#160; GetDepartmentList(<span style="color: maroon">&quot;Accounting&quot;</span>);</div>
<p>&#8230; you can let the compiler figure out what the type of departmentLists is by examination, and not have to worry about getting the declaration exactly right:</p>
<div style="font-family: courier new; font-size: 9pt">var departmentList = GetDepartmentList(<span style="color: maroon">&quot;Accounting&quot;</span>);</div>
<p>Note that despite the obvious syntactic similarity to JavaScript &quot;var&quot; declarations, this isn&#8217;t a dynamic object &#8212; the language still knows it is a Dictionary generic parameterized on a Person and a List of Persons, and Intellisense will demonstrate that to anybody interested in looking. It&#8217;s simply a shorthand way to avoid some additional finger trauma.</p>
<h2>Static Classes (2.0)</h2>
<p>Static classes won&#8217;t spare your fingers, but they can spare your brain, because a static class is deliberately written such that the consumers of a static class can&#8217;t create an instance &#8212; in other words, a static class is exactly what its name implies, a class filled with nothing but static methods and properties. In other words, this is a perfect place to put those &quot;utility&quot; methods that don&#8217;t really belong on an object instance:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: green">// This isn&#8217;t really an operation belonging to Persons,</span>     <br /><span style="color: green">// but it doesn&#8217;t exactly fit well anywhere else in the</span>     <br /><span style="color: green">// hierarchy. So we put it here.</span>     <br /><span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">class</span> Legal     <br />{     <br />&#160; <span style="color: blue">public static</span>&#160;<span style="color: blue">void</span> Marry(Person spouse1, Person spouse2)     <br />&#160; {     <br />&#160; <span style="color: green">// &#8230;</span>     <br />&#160; }     <br />}</div>
<p>Static classes make sure that developers realize that these are methods that aren&#8217;t intended to be object instances &#8212; in a way, it&#8217;s almost a step backwards to the earlier days of procedural programming, but let&#8217;s face it, sometimes what you really want is just a global method. The various &quot;math&quot; operations come to mind, for example &#8212; it&#8217;s not really an operation of &quot;Int32&quot; to calculate the sine of itself (but extension methods, next, give you that if you want it).</p>
<h2>Extension Methods (3.0)</h2>
<p>Extension methods are simultaneously one of the most powerful and most dangerous features of C# 3.0 in that they present the appearance of sliding new methods in on an existing and defined type, even if that type is sealed. (In other words, no inheritance is necessary).</p>
<p>Consider the System.Int32 type. As a mathematical type, it doesn&#8217;t know how to do much besides the four cardinal operations: add, subtract, multiply, divide, and all that. That&#8217;s great for simple math, but if you&#8217;re trying to build something bigger and stronger than that, you don&#8217;t get a lot of help from it. Granted, the Framework has a number of useful operations on the Math class to do some of that, but particularly for some of the unary operations (like taking the absolute of a given number, Math.Abs()), it feels awkward &#8212; it would be nice if that was an operation on System.Int32 natively.</p>
<p>Extension methods allow you to do exactly that:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> MathExtensions     <br />{     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">class</span> Int32Operations     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">int</span> Abs(<span style="color: blue">this</span>&#160;<span style="color: blue">int</span> i)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> Math.Abs(i);     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </p>
<p><span style="color: blue">namespace</span> ConsoleApplication1     <br />{     <br />&#160; <span style="color: blue">using</span> MathExtensions;     </p>
<p>&#160; <span style="color: blue">class</span> Program     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">static</span>&#160;<span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">int</span> i = -<span style="color: maroon">12</span>;     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">int</span> abs_i = i.Abs();     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </p>
</div>
<p>The syntax is a little indirect, but there&#8217;s two main parts. The definition of the extension method itself is fairly straightforward&#8211;it must be a static method on a static class, whose first parameter is adorned with the extra modifier &quot;this&quot;, indicating the instance (the &quot;this&quot; object) on which the method is operating. Next, at the point of usage, the &quot;using&quot; statement brings the extension methods in that namespace (&quot;MathExtensions&quot;) into the compiler&#8217;s field of vision, so that from that point on, those operations are available on the types specified in the &quot;this&quot; parameter. So, in the case above, Abs() appears on &quot;int&quot;s, but not on longs, decimals, floats or doubles (unless those extension methods were also put into MathExtensions, which wouldn&#8217;t be a bad idea).</p>
<p>The power of extension methods is pretty obvious, particularly when you consider that you could write an extension method taking a &quot;this&quot; parameter of Object, thus adding a method to the root of the entire hierarchy. Do this with care.</p>
<h2>Anonymous Methods (2.0) and Lambda Expressions (3.0)</h2>
<p>These two features are really the same thing, an easier way to pass a method (instance or static) as a parameter to a function. In of themselves, it may seem like they&#8217;re just an easier way to write one-off event handlers, but when combined with enumerators (below), it introduces a new style of programming to C#, that of functional programming, in the same vein as languages like Haskell or ML or their more recent brethren, F# and Scala.</p>
<p>Here&#8217;s a concrete example: we have a collection of Persons in a List that need to see if their birthday is today. We could, of course, rip through the list using the traditional foreach loop, find the Persons we want to find, then apply the logic (add 1 to their age) to each one, but if this list grows to be thousands of objects long, that could take a while.</p>
<p>Instead, let&#8217;s apply some anonymous methods and enumerators to do some of this in parallel. (Note that all of this is without having to do anything with F#, the Concurrency Control Runtime, or the Parallel LINQ extensions). We&#8217;ll have to do a little groundwork first, but in a second it&#8217;ll be clear why this is a powerful extension to the language.</p>
<p>First, let&#8217;s create an extension method that will add itself to a List&lt;T&gt; and provide a new Filter function that will take a method (in this case, we&#8217;ll use the Predicate&lt;T&gt; type already defined in 3.0) that will be used to decide which elements should be included in the results. Second, let&#8217;s create a second extension method on List&lt;T&gt;s that will take an Action&lt;T&gt; type and apply it to each of the elements in that list:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> ListExtensions     <br />{     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">class</span> ListOperations     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span> List&lt;T&gt; Filter&lt;T&gt;(<span style="color: blue">this</span> List&lt;T&gt; list, Predicate&lt;T&gt; filterFunc)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; List&lt;T&gt; results = <span style="color: blue">new</span> List&lt;T&gt;();     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (T it <span style="color: blue">in</span> list)     <br />&#160;&#160;&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (filterFunc(it))     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; results.Add(it);     <br />&#160;&#160;&#160;&#160;&#160; }     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> results;     <br />&#160;&#160;&#160; }     </p>
<p>&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">void</span> Process&lt;T&gt;(<span style="color: blue">this</span> List&lt;T&gt; list, Action&lt;T&gt; actionFunc)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (T it <span style="color: blue">in</span> list)     <br />&#160;&#160;&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; actionFunc(it);     <br />&#160;&#160;&#160;&#160;&#160; }     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}</div>
<p>Using the new methods is pretty easy, as they just &quot;appear&quot; on the List&lt;T&gt; type once the ListExtensions namespace has been &quot;using&quot;ed:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> ConsoleApplication1     <br />{     <br />&#160; <span style="color: blue">using</span> ListExtensions;     </p>
<p>&#160; <span style="color: blue">class</span> Person     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> Name     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span>; <span style="color: blue">set</span>;     <br />&#160;&#160;&#160; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span> DateTime Birthday     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span>; <span style="color: blue">set</span>;     <br />&#160;&#160;&#160; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">int</span> Age     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span>; <span style="color: blue">set</span>;     <br />&#160;&#160;&#160; }     <br />&#160; }     </p>
<p>&#160; <span style="color: blue">class</span> Program     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">static</span>&#160;<span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; List&lt;Person&gt; database = <span style="color: blue">new</span> List&lt;Person&gt;(     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person[]     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Ted Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1971</span>, <span style="color: maroon">2</span>, <span style="color: maroon">7</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">38</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Charlotte Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1971</span>, <span style="color: maroon">11</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">38</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Michael Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1993</span>, <span style="color: maroon">7</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">15</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Matthew Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1999</span>, <span style="color: maroon">9</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">8</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Cathi Gero&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1971</span>, <span style="color: maroon">4</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">38</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Newborn Baby&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = DateTime.Now, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">0</span>}     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; });     <br />&#160;&#160;&#160;&#160;&#160; database.Process((p) =&gt; Console.WriteLine(<span style="color: maroon">&quot;{0} is {1}&quot;</span>, p.Name, p.Age));     <br />&#160;&#160;&#160;&#160;&#160; Console.WriteLine(<span style="color: maroon">&quot;============ Checking for birthdays ==============&quot;</span>);     <br />&#160;&#160;&#160;&#160;&#160; database.Filter((p) =&gt; p.Birthday.Date == DateTime.Today.Date).Process((it) =&gt; it.Age = it.Age + <span style="color: maroon">1</span>);     <br />&#160;&#160;&#160;&#160;&#160; database.Process((p) =&gt; Console.WriteLine(<span style="color: maroon">&quot;{0} is now {1}&quot;</span>, p.Name, p.Age));     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </p>
</div>
<p>So far, so good. But remember, we started this discussion talking about doing operations in parallel, and these are all happening in sequence. To demonstrate doing them in parallel without changing the basic programming model, let&#8217;s create a &quot;Parallel Process&quot; function, ParProcess, as an async-delegate-invoking version of the Process function above:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> ListExtensions     <br />{     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">class</span> ListOperations     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: green">// &#8230;</span>     </p>
<p>&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">void</span> Process&lt;T&gt;(<span style="color: blue">this</span> List&lt;T&gt; list, Action&lt;T&gt; actionFunc)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (T it <span style="color: blue">in</span> list)     <br />&#160;&#160;&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; actionFunc(it);     <br />&#160;&#160;&#160;&#160;&#160; }     <br />&#160;&#160;&#160; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">static</span>&#160;<span style="color: blue">void</span> ParProcess&lt;T&gt;(<span style="color: blue">this</span> List&lt;T&gt; list, Action&lt;T&gt; actionFunc)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; List&lt;IAsyncResult&gt; iars = <span style="color: blue">new</span> List&lt;IAsyncResult&gt;();     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (T it <span style="color: blue">in</span> list)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; iars.Add(actionFunc.BeginInvoke(it, <span style="color: blue">null</span>, actionFunc));     </p>
<p>&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (var iar <span style="color: blue">in</span> iars)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; ((Action&lt;T&gt;)(iar.AsyncState)).EndInvoke(iar);     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </p>
</div>
<p>This would be a bit easier to write if we didn&#8217;t have to call EndInvoke on each async delegate invocation, but CLR lore holds that failing to do so leaks resources (and presumably, after enough of them, will crash the process). Writing an equivalent &quot;Parallel Filter&quot; function is trickier, since each asynchronous invocation will need to add the compared item into a collection to be returned to the user, but it&#8217;s not impossible &#8212; I leave that as an exercise to the reader. (Which is another way of saying, &quot;I could have written this, but I was feeling either lazy or under terrible pressure to turn in my overdue article&quot;.)</p>
<p>Using the ParProcess version is no different than the Process version:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> ConsoleApplication1     <br />{     <br />&#160; <span style="color: blue">using</span> ListExtensions;     </p>
<p>&#160; <span style="color: green">// &#8230;</span>     </p>
<p>&#160; <span style="color: blue">class</span> Program     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">static</span>&#160;<span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; List&lt;Person&gt; database = <span style="color: blue">new</span> List&lt;Person&gt;(     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person[]     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Ted Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1971</span>, <span style="color: maroon">2</span>, <span style="color: maroon">7</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">38</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Charlotte Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1971</span>, <span style="color: maroon">11</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">38</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Michael Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1993</span>, <span style="color: maroon">7</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">15</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Matthew Neward&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1999</span>, <span style="color: maroon">9</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">8</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Cathi Gero&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1971</span>, <span style="color: maroon">4</span>, <span style="color: maroon">3</span>), </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">38</span>},     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Person {Name = <span style="color: maroon">&quot;Newborn Baby&quot;</span>, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Birthday = DateTime.Now, </div>
<div style="font-family: courier new; font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age = <span style="color: maroon">0</span>}     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; });     <br />&#160;&#160;&#160;&#160;&#160; database.Process((p) =&gt; Console.WriteLine(<span style="color: maroon">&quot;{0} is {1}&quot;</span>, p.Name, p.Age));     <br />&#160;&#160;&#160;&#160;&#160; Console.WriteLine(<span style="color: maroon">&quot;============ Checking for birthdays ==============&quot;</span>);     <br />&#160;&#160;&#160;&#160;&#160; database.Filter((p) =&gt; p.Birthday.Date == DateTime.Today.Date).Process((it) =&gt; it.Age = it.Age + <span style="color: maroon">1</span>);     <br />&#160;&#160;&#160;&#160;&#160; database.Process((p) =&gt; Console.WriteLine(<span style="color: maroon">&quot;{0} is now {1}&quot;</span>, p.Name, p.Age));     </p>
<p>&#160;&#160;&#160;&#160;&#160; Console.WriteLine(<span style="color: maroon">&quot;========== Processing Asynchronously =============&quot;</span>);     <br />&#160;&#160;&#160;&#160;&#160; database.ParProcess(<span style="color: blue">delegate</span> (Person p) {&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(<span style="color: maroon">&quot;Printing {0} on {1}&quot;</span>, p.Name,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Thread.CurrentThread.ManagedThreadId);     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Thread.Sleep(<span style="color: maroon">5</span>*<span style="color: maroon">1000</span>); });     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />} </div>
<p>When run, the results that appear for the managed thread id&#8217;s may differ, but on my system, they ranged from 3 to 7.</p>
<h2>Summary</h2>
<p>In some ways, this may seem like a perverse way to take features of a programming language and do crazy things with them &#8212; after all, all of the code I&#8217;ve written above could have been done using &quot;traditional&quot; C# / object-oriented approaches, using traditional looping approaches and so on. I don&#8217;t think any of the designers of C# would hesitate for one moment to agree with that statement.</p>
<p>But the more we as C# developers can look at the new features of the language, including the new &quot;dynamic&quot; features that are coming in C# 4.0, and use them to better and more succinctly express the intent of the code or design, the more we can reduce the amount of code we have to write, ship, and maintain. And in the end, let&#8217;s be honest &#8212; the best line of code we&#8217;ve ever written is the one that we didn&#8217;t have to write in the first place.</p>
</p>
<p>&#8212;</p>
<p><em>Ted Neward is a Principal Consultant with ThoughtWorks, an international consultancy developing agile solutions. He is fluent in Java, C#, Scala, F#, C++, and a few other languages, and spends a lot of time studying programming languages, virtual execution engines, and scalable enterprise systems. He currently resides in the Pacific Northwest.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/03/c-features-you-should-be-using/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Accelerate Your Coding with Code Snippets</title>
		<link>http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/</link>
		<comments>http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 03:44:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[MAXoutput]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[noyes]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/</guid>
		<description><![CDATA[by Brian Noyes
 One of the most underutilized productivity features in Visual Studio 2005 and Visual Studio 2008 is Code Snippets. Even though most developers have heard about them, I find when working with consulting customers that there are few developers who use them on a regular basis. Even if they do use them, they [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://briannoyes.net" target="_blank">Brian Noyes</a></p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/brian-noyes.jpg"><img title="Brian_Noyes" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 22px 22px; border-right-width: 0px" height="57" alt="Brian_Noyes" src="http://devshaped.com/wp-content/uploads/2009/01/brian-noyes-thumb.jpg" width="85" align="right" border="0" /></a> One of the most underutilized productivity features in Visual Studio 2005 and Visual Studio 2008 is Code Snippets. Even though most developers have heard about them, I find when working with consulting customers that there are few developers who use them on a regular basis. Even if they do use them, they vastly underutilize them by only using a couple of them and never create their own. In this article, I give you a quick intro into what code snippets are, how they work, what is available out of the box, and how to go beyond that by quickly creating your own code snippets.</p>
<h2>Code Snippets Overview</h2>
<p>Code snippets were introduced in Visual Studio 2005 and allow you to emit a chunk of code into your editor by typing a few keystrokes (a shortcut mnemonic). I&#8217;m going to use C# for the examples in this article, but just realize that code snippets work in Visual Basic and other .NET languages as well. That chunk of code can just be a static chunk of code, but what really makes code snippets powerful is that they can have placeholders that you can quickly overtype when you invoke the code snippet.</p>
<p> <span id="more-21"></span>
<p>To try them out, the best one to start with is one you can probably use the most often: the <b><i>prop</i></b> snippet. Code snippets have both a title and a shortcut, although these will often both just be set to the shortcut value. The <b><i>prop</i></b> snippet emits a property with a backing member variable in Visual Studio 2005, or an auto-implemented property in Visual Studio 2008. Code snippets show up in the IntelliSense list as well, so if a snippet has a longer shortcut than a few letters, often you can just invoke it by typing a few of the letters and select it out of IntelliSense.</p>
<p>For example, if I go inside a class declaration and type <b><i>prop</i></b> and hit <b><i>Tab</i></b> twice (once to dismiss the IntelliSense list, the second time to invoke the code snippet), I get the following in the editor:</p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="background: lime; color: blue">int</span> <span style="background: lime">MyProperty</span> { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p> 
<p>The type and property name will be highlighted in green, indicating that they are the placeholders defined in the snippet. For this snippet, the type is the first placeholder and will be selected so that if you start typing, you will overtype what is there. So for example, say I want a string property. I type <b><i>string</i></b> and hit <b><i>Tab</i></b>. Tab moves between the placeholders, and when the focus is placed on the next placeholder, it selects it as well so you can overtype with the value you want. So if I type <b><i>Name</i></b> and hit <b><i>Enter</i></b>, I leave the placeholder mode and my changes are accepted. If I keep hitting Tab, it revisits the placeholders in order in case you accidently tabbed past one or changed your mind as you were filling things in.</p>
<p>This one may not be stunning in its time savings, but when you add up the keystrokes (4 + 1 + 6 + 1 + 4 + 1) you have 17 versus the 30 for typing out the whole property yourself. Wham! You just became almost twice as fast at writing code! OK, don&#8217;t tell your manager just yet, you need to actually get used to doing this all the time first… </p>
<p>Now consider setting a switch case statement for an enumeration with a number of values. For example, say you needed to set up a switch/case on the states for System.Windows.Input.MouseAction. If you had a local variable of that type with something like the following method:</p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="color: blue">void</span> HandleMouseInput(<span style="color: #2b91af">MouseAction</span> action)     <br />{     </p>
<p>} </p>
<p> 
<p>If I go inside that method and type <b><i>switch Tab Tab</i></b>, I get the following snippet with a placeholder on the variable <b><i>switch_on</i></b>. </p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="color: blue">void</span> HandleMouseInput(<span style="color: #2b91af">MouseAction</span> action)     <br />{     <br />&#160; switch (<span style="background: lime">switch_on</span>)     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">default</span>:     <br />&#160; }     <br />}     </p>
<p> 
<p>If I type in <b><i>action</i></b> for the placeholder (the local variable of enum type MouseAction) and hit <b><i>Enter</i></b>, the snippet fills in all the enum values as case statements:</p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="color: blue">void</span> HandleMouseInput(<span style="color: #2b91af">MouseAction</span> action)     <br />{     <br />&#160;&#160;&#160; <span style="color: blue">switch</span> (action)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.LeftClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.LeftDoubleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.MiddleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.MiddleDoubleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.None:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.RightClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.RightDoubleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.WheelClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">default</span>:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160; }     <br />}     </p>
<p> 
<p>That is a huge keystroke savings filling all the tedious structure code! You just need to fill in what to do for each case.</p>
<h2>Built-in Code Snippets</h2>
<p>The list of built-in code snippets for C# is actually fairly short. There is really only a small number included out of the box. You can find the full list here with a short description of each for the core C# language snippets: (<a href="http://msdn.microsoft.com/en-us/library/z41h7fat(VS.80).aspx)">http://msdn.microsoft.com/en-us/library/z41h7fat(VS.80).aspx)</a>. There are some additional ones in other categories that ship with Visual Studio 2008, but the list is still fairly short for C#.&#160; Visual Basic actually ships with hundreds. But this shouldn&#8217;t make you feel shorted as a C# developer. You can actually find a greatly expanded library of C# code snippets that shipped after VS 2005 here: <a href="http://msdn.microsoft.com/en-us/vs2005/aa718338.aspx">http://msdn.microsoft.com/en-us/vs2005/aa718338.aspx</a>. You can also find libraries of other code snippets out on CodePlex and other places on the net.&#160; <b><i>do</i></b>, <b><i>while</i></b>, <b><i>for</i></b>, <b><i>foreach</i></b>, and a number of other common control structures are all in the basic set, along with everything from data access patterns to file IO patterns in the expanded sets. <b><i>ctor</i></b> is another one that is handy for declaring a default constructor for a class.</p>
<p>There is a Code Snippet Manager in the Tools menu of Visual Studio that will let you browse all the built in snippets, import new ones, and search online. However, if you understand what is going on under the covers, it is actually easier to just manipulate them as files.</p>
<h2>Inside Code Snippets</h2>
<p>Code snippets themselves are nothing more than XML files with a particular schema that reside in some directories that Visual Studio is aware of. Specifically, snippet files end with a (not surprising) extension of .snippet. The built-in code snippets that ship with Visual Studio reside under the folder C:\Program Files\Microsoft Visual Studio 9.0 under VC# or VB respectively. Visual Studio is the default editor for that file type, so you can just browse to those folders and double click on the .snippet files and edit the XML in Visual Studio. If you want to create your own snippets, put them in your Documents folder under Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets (or similar path for VB or 2005). Visual Studio watches this file all the time, so changes or additions to snippets take effect immediately without needing to restart Visual Studio.</p>
<p>There are a lot of elements supported in the schema but the key ones to focus on are Shortcut in the Header element, and the Declarations and Code elements under the Snippet element. Say I wanted to emit a code snippet like the following with a placeholder over the <b><i>Boo!</i></b> string so I could overwrite it:</p>
<p style="font-family: courier new, courier">System.<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Boo!&quot;</span>); </p>
<p>To do so, I would have a code snippet that looks like this inside: </p>
<p style="font-family: courier new, courier"><span style="color: blue">&lt;?</span><span style="color: #a31515">xml</span><span style="color: blue"> </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot;<span style="color: blue"> </span><span style="color: red">encoding</span><span style="color: blue">=</span>&quot;<span style="color: blue">utf-8</span>&quot;<span style="color: blue"> ?&gt;</span>     <br /><span style="color: blue">&lt;</span><span style="color: #a31515">CodeSnippets</span><span style="color: blue">&#160; </span><span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</span>&quot;<span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">CodeSnippet</span><span style="color: blue"> </span><span style="color: red">Format</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0.0</span>&quot;<span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;</span>cwboo<span style="color: blue">&lt;/</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Declarations</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>output<span style="color: blue">&lt;/</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>Boo!<span style="color: blue">&lt;/</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Declarations</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Code</span><span style="color: blue"> </span><span style="color: red">Language</span><span style="color: blue">=</span>&quot;<span style="color: blue">csharp</span>&quot;<span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;![CDATA[</span><span style="color: gray">System.Console.WriteLine(&quot;$output$&quot;$end$);</span><span style="color: blue">]]&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Code</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160; &lt;/</span><span style="color: #a31515">CodeSnippet</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&lt;/</span><span style="color: #a31515">CodeSnippets</span><span style="color: blue">&gt;</span>     </p>
<p>In this case, the IntelliSense shortcut to invoke it is <b><i>cwboo</i></b>. The code snippet declares one literal variable named <b><i>output</i></b> with a default value of <b><i>Boo!</i></b>. The actual code of the snippet has to be declared inside an XML CDATA section because some legal characters in C #and other languages could cause XML parsing errors. The <b><i>output</i></b> literal is a named placeholder in the emitted code which will be replaced with the default value and highlighted for overtyping as discussed earlier. You can see that when you use a literal in the snippet, you enclose it within leading and trailing <b><i>$</i></b> delimiters. Everything else in the snippet (except other variables) is emitted as plain text in the editor when the snippet is invoked. You can see I am also using a built-in literal <b><i>$end$</i></b>, which specifies where the input cursor is placed when you hit <b><i>Enter</i></b> to leave the placeholder mode of snippets.</p>
<h2>Creating Your Own Snippets</h2>
<p>By now you are probably emitting some form of &quot;Eeewwww!!!&quot; sound at the thought of editing the XML whenever you want to create your own snippets. The good news is that you don&#8217;t have to. There are a couple of free tools out there to help you create and edit snippets without ever needing to touch the XML directly. One I have been using for years as a C# guy is Snippy (<a href="http://www.codeplex.com/snippy">http://www.codeplex.com/snippy</a>). It puts a very simple dialog interface on top of the XML schema for you to create your snippets (see Figure 1).</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/snippy.png"><img title="snippy" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="480" alt="snippy" src="http://devshaped.com/wp-content/uploads/2009/01/snippy-thumb.png" width="482" border="0" /></a> </p>
<p><strong><em>Figure 1: Snippy</em></strong></p>
<p>A great way to start many custom snippets is to start with an existing one and just edit it. Just remember to do a &quot;Save As…&quot; and save it to your Documents code snippets folder mentioned earlier so you don&#8217;t overwrite the built-in ones.</p>
<p>Another choice is the Code Snippet Editor for Visual Basic 2008 (</span><a href="http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx">http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx</a>). For those language bigots, you will have to get over the title: it actually supports multiple languages (see Figure 2).</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/csevb.png"><img title="CSEVB" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="400" alt="CSEVB" src="http://devshaped.com/wp-content/uploads/2009/01/csevb-thumb.png" width="482" border="0" /></a> </p>
<p><strong><em>Figure 2: Code Snippet Editor for Visual Basic 2008</em></strong></p>
<p>Either one of these will allow you to quickly create your own snippets and stop typing the same code over and over again.</p>
<h2>Code Snippet Functions</span></h2>
<p>I showed you the switch snippet earlier in the article, and that may lead you down the path of thinking you can pull off anything with a snippet. Unfortunately that is one of the most powerful ones you will find. There is a whopping three functions in the C# library for expanded capabilities in your snippets (</span><a href="http://msdn.microsoft.com/en-us/library/ms242312.aspx"><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">http://msdn.microsoft.com/en-us/library/ms242312.aspx</span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">). The <b><i>GenerateSwitchCases</i></b> function is the one that did the magic for the <b><i>switch</i></b> code snippet. The <b><i>ClassName</i></b> function will emit the class name of the class in which the snippet is invoked. This is used by the <b><i>ctor</i></b> snippet I mentioned earlier to declare a default constructor. And lastly there is the <b><i>SimpleTypeName</i></b> function, which will take a fully qualified type name, and will emit both a <b><i>using</i></b> statement in C# (<b><i>Imports</i></b> in VB) and the root class name so that you don&#8217;t have fully qualified types in the emitted code. This is used by the built-in snippet <b><i>cw</i></b>, which emits a Console.WriteLine statement. Take a look at that built-in snippet if you want to see the way it is used. Unfortunately there is no way to add your own custom functions. If you want more power to do more custom things in your snippets, you might want to take a look at third party tools such as Developer Express CodeRush (</span><a href="http://www.devexpress.com/CodeRush"><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">http://www.devexpress.com/CodeRush</span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">) or JetBrains ReSharper (</span><a href="http://www.jetbrains.com/resharper/">http://www.jetbrains.com/resharper/</span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">). They have things called templates that are like code snippets on steroids and are also extensible.</span> </p>
<h2>Wrap Up</h2>
</p>
<p>Now you should have a good idea what code snippets are, how they work, and how to go beyond what you get out of the box. Let me share with you my approach to code snippets: If I find myself typing some code and I get that &quot;déjà vu&quot; feeling of &quot;I know I&#8217;ve written this little chunk of code before&quot;, I stop myself, write it as a code snippet, and then I won&#8217;t ever have to write it again. Too often developers take the longer path because they think they will be &quot;wasting time&quot; by going and doing something like authoring a code snippet for the code they are writing. Many times, it is not authoring the code snippet that is the real waste of time. </p>
<p>&#8212;-</p>
</p>
</p>
<p><em>Brian Noyes is chief architect of IDesign (</span></em><a href="http://www.idesign.net"><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;"><em>www.idesign.net</em></span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;"><em>), a Microsoft Regional Director and MVP, writer and speaker. He is the author of Developing Applications with Windows Workflow Foundation, Smart Client Deployment with ClickOnce, and Data Binding with Windows Forms 2.0. He is also a frequent speaker at conferences worldwide including Microsoft TechEd, DevConnections, VSLive!, DevTeach and others. You can contact him through his blog at </em></span><a href="http://briannoyes.net"><em>http://briannoyes.net</span></em></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;"><em>. </em></span></p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
