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

<channel>
	<title>Brian On Software</title>
	<atom:link href="http://blog.briandicroce.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.briandicroce.com</link>
	<description>On the infinite search for the silver bullet...</description>
	<lastBuildDate>Mon, 19 Oct 2009 03:04:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Book Review #11: The Bug, by Ellen Ullman</title>
		<link>http://blog.briandicroce.com/2009/10/18/book-review-11-the-bug-by-ellen-ullman/</link>
		<comments>http://blog.briandicroce.com/2009/10/18/book-review-11-the-bug-by-ellen-ullman/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 02:06:54 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/?p=679</guid>
		<description><![CDATA[Written in both a narrative and story-like style, The Bug centers on the story of a software glitch, a defective feature, a non-desired behavior of a computer program, commonly known to us (software developers) as a bug. A bug which a novice tester found and reported to the software engineer in charge of the most [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.com/Bug-Ellen-Ullman/dp/1400032350%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dbrdicr-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1400032350"><img style="margin: 0px 0px 5px 10px; display: inline" border="0" alt="The Bug by Ellen Ullman" align="right" src="http://ecx.images-amazon.com/images/I/31C05VPPF7L._SL160_.jpg" /></a>Written in both a narrative and story-like style, <a href="http://www.amazon.com/Bug-Ellen-Ullman/dp/1400032350%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dbrdicr-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1400032350"><strong>The Bug</strong></a> centers on the story of a <em>software glitch</em>, a <em>defective feature</em>, a <em>non-desired behavior of a computer program</em>, commonly known to us (software developers) as a bug. A bug which a novice tester found and reported to the software engineer in charge of the most complicated layer of a software: the UI layer. The year? We’re talking 1984, baby! This was before windows, icons, menus and pointers were mainstream in UI-driven applications! But this bug was not like any other bugs. It’s one of those bugs that shows up when you least expect it: in a demo in front of potential customers, at a sales meeting, etc. It’s one of those bugs that are super hard to reproduce manually. Sometimes it shows itself plainly and other times, even though the same repetitive actions are executed (or so we think), it stays hidden. It’s one of those bugs that doesn’t let you sleep comfortably at night. It’s one of those bugs that makes you skip supper with your loved ones. It’s one of those bugs that causes you to question yourself, “<em>Am I made for this? Am I as smart as I thought I was? Am I competent? Do I know what I’m doing exactly?</em>”. It’s one of those bugs that can make your world dark, lonely, aggressive, egoist, godless. You know what I’m talking about? The bug. THE bug.</p>
<p>Sooner or later, a professional software developer will encounter one of these, and will have to deal with it. Not only the technical aspects surrounding the bug, but also all the human interactions involved in finding the cause and the solution for it. In this story, we get acquainted with Ethan Levin, the software engineer responsible of the UI layer for a database application at software company. Throughout the book, we are invited to see how his life will change to chaos as the lifespan of this bug goes from days to months. A bug which he was assigned to fix. A bug which he doesn’t know how to fix because of several reasons very well explained by the author, Ellen Ullman. The story also introduces us to Roberta Walton, a novice tester who first made acquaintance with the bug. Not very much interested in the technicalities of programming (compilers, languages, operating systems, device drivers, etc.), she is somewhat obliged to learn and get familiar with them because without those tools and knowledge, the bug will get her too. Together, they will try to hunt and kill the bug.</p>
<p>What I found interesting about the story, is how familiar a computer bug can be. For example, a bug could have such a personality and character that it reminds us of someone. Sometimes it can resemble ourselves. That’s when it gets scary! I had a lot of trouble putting the book down. I read it when I was doing my physio exercises, on the way to church, while my wife was shopping at Wal-Mart, etc. Whenever I had free time, I would dive right into it. The story reads itself so well that I could easily imagine the scenarios, the faces and the emotions each character was going through. </p>
<p>In one instance, after having found the solution to get rid of the bug, Roberta reflects on how much trust we put into other software-driven appliances (machines, Web applications, vehicles, etc.) that surround our lives :</p>
<blockquote><p>“Which led me to the question: Was it still a bug? <strong>If a flaw is there and you don’t see it, is it still a software error?</strong> Or is it more like a defective gene, waiting for an environmental trigger to come along and kill you? […] This latent, dormant, sleeping bug could then awake, and everything – a program thought to be working – would suddenly crash. <strong>I think about this every time I get on a plane, or make an electronic payment, or get an X ray, or step into an elevator.</strong> Code, all around me. Working, as far as anyone can tell. Somewhere in there – hiding, biding its times, only waiting for a software patch, or a device-driver update, or a network installation, or a well-intentioned programmer to come along and tweak the code – is a bug […] ready to bring the system down.</p>
</blockquote>
<p>What’s funny about this observation is that I ask myself the same thing whenever I step into a plane, a car or an elevator! Just the thought about it makes me think that one day we might read in the front page of a newspaper the following headline: “He died because of a bug. A <em>glitch</em>.” A person born from a mother, nourished and well educated, died (if not to say ‘murdered’) by a couple of misbehaving ones and zeroes. What a thought!</p>
<p>I highly recommend this book to CS and SWE students that would like to get a taste of what the industry is like. What kind of human and professional behaviors to avoid, and which ones to adopt, because there is SO much more to software development than code. I also recommend it to software developers, testers, consultants, managers and teachers too. It’s superbly well-written! A great entertainment for the geek in us.</p>
<p>P.S. There is actually a small bug in the book. Look at page 176. Can you guess what it is?</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/10/18/book-review-11-the-bug-by-ellen-ullman/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing your command prompt</title>
		<link>http://blog.briandicroce.com/2009/10/16/customizing-your-command-prompt/</link>
		<comments>http://blog.briandicroce.com/2009/10/16/customizing-your-command-prompt/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 12:27:26 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[How To...]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/10/16/customizing-your-command-prompt/</guid>
		<description><![CDATA[Yesterday, I was playing around with customizing my command prompt. I wanted to rapidly know in which directory I was in at all times. At the same time, I wanted to display the date and time, so that whenever I had to do some comparison with a file’s timestamp, I would could know the current [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I was playing around with customizing my command prompt. I wanted to rapidly know in which directory I was in at all times. At the same time, I wanted to display the date and time, so that whenever I had to do some comparison with a file’s timestamp, I would could know the current date and time from within the same window. Basically, I wanted something like this:</p>
<p><strong>[$CURRENT_DIRECTORY$]      <br />$DATE$ $TIME$ &gt;</strong></p>
<p>While searching around the Web to get an idea how to do this, I landed on <a href="http://guides.wkbw.com/How_to_Create_a_Custom_Windows_Command_Prompt-a991369.html">this page</a> which does a great job explaining how to do exactly this. I’ll resume the steps here, but feel free to visit that site for even more information on this matter:</p>
<ol>
<li>Launch a Command Prompt which will be used a sandbox to try out your new prompt. </li>
<li>Use the table below as a reference to the symbols you can use to define your new prompt. </li>
<li>Type <strong>prompt</strong> followed by the symbols as they appear and press &lt;Enter&gt;. </li>
</ol>
<p> <center><br />
<table border="1" cellspacing="0" cellpadding="2" width="572">
<tbody>
<tr>
<td valign="top" width="204">
<p><strong>Symbol</strong></p>
</td>
<td valign="top" width="366">
<p><strong>Value</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="204">
<p>$$</p>
</td>
<td valign="top" width="366">
<p>$</p>
</td>
</tr>
<tr>
<td valign="top" width="207">
<p>$_ </p>
</td>
<td valign="top" width="363">
<p>(Causes a CR/LF; new line)</p>
</td>
</tr>
<tr>
<td valign="top" width="210">
<p>$A</p>
</td>
<td valign="top" width="361">
<p>&amp; (ampersand)</p>
</td>
</tr>
<tr>
<td valign="top" width="212">
<p>$B</p>
</td>
<td valign="top" width="359">
<p>| (pipe)</p>
</td>
</tr>
<tr>
<td valign="top" width="214">
<p>$C</p>
</td>
<td valign="top" width="358">
<p>(</p>
</td>
</tr>
<tr>
<td valign="top" width="215">
<p>$D </p>
</td>
<td valign="top" width="357">
<p>Current date</p>
</td>
</tr>
<tr>
<td valign="top" width="216">
<p>$E</p>
</td>
<td valign="top" width="356">
<p>Escape code</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$F</p>
</td>
<td valign="top" width="356">
<p>)</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$G</p>
</td>
<td valign="top" width="356">
<p>&gt;</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$H</p>
</td>
<td valign="top" width="356">
<p>Erases the previous character</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$L </p>
</td>
<td valign="top" width="356">
<p>&lt;</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$N</p>
</td>
<td valign="top" width="356">
<p>Current drive</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$P</p>
</td>
<td valign="top" width="356">
<p>Current drive and path</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$Q</p>
</td>
<td valign="top" width="356">
<p align="left">=</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$S</p>
</td>
<td valign="top" width="356">
<p>(space)</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$T</p>
</td>
<td valign="top" width="356">
<p>Current time (HH:MM:SS.mm)</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>$V</p>
</td>
<td valign="top" width="356">
<p>Windows version number</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>[%computer_name%]              </p>
</td>
<td valign="top" width="356">
<p>Your computer name in between brackets</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>%environment_variable% </p>
</td>
<td valign="top" width="356">
<p>Displays the value of the environment variable</p>
</td>
</tr>
</tbody>
</table>
<p> </center>
<p>&#160;</p>
<p>For example, if I wanted to display only the current date followed by a greater-than symbol, I would type: <strong>prompt $D$G </strong>and press &lt;Enter&gt; (don’t close that window yet!):</p>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/10/image.png" width="377" height="191" /></p>
<p>At this point, you can view what prompt will be used. Unfortunately, this will not stay as the default prompt. If you close the window, you’ll see again the default prompt which is $P$G (show the current path, followed by the greater-than sign).</p>
<p>To permanently make the changes for all instances of the command prompt window, you will have to play in the registry. Trust me, this one is pretty easy:</p>
<ol>
<li>Fire up the registry editor (START, RUN, type <strong>regedit</strong> then press &lt;ENTER&gt;) </li>
<li>Navigate to the following node: <strong>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment</strong> </li>
<li>If it doesn’t already exist, create a new string value named <strong>Prompt</strong> </li>
<li>Assign your custom prompt as the data value to it (see the following screenshot for an example) and press OK. </li>
<li>At this point, you should <strong>restart</strong> your computer to affect the changes permanently. </li>
</ol>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/10/image1.png" width="398" height="179" /> </p>
<p>Next time you’ll launch a command prompt window, it’ll show your custom prompt.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/10/16/customizing-your-command-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapping a domain name to an IP address in the HOSTS file to bypass a DNS resolver.</title>
		<link>http://blog.briandicroce.com/2009/10/15/mapping-a-domain-name-to-an-ip-address-in-the-hosts-file-to-bypass-a-dns-resolver/</link>
		<comments>http://blog.briandicroce.com/2009/10/15/mapping-a-domain-name-to-an-ip-address-in-the-hosts-file-to-bypass-a-dns-resolver/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 16:15:57 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[How To...]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/10/15/mapping-a-domain-name-to-an-ip-address-in-the-hosts-file-to-bypass-a-dns-resolver/</guid>
		<description><![CDATA[When I made the switch to another hosting company, I had to point my domain name (briandicroce.com) to their DNS servers. After that, I couldn’t access my site from the domain because the DNS servers associated with my ISP had the previous DNS settings of my domain name in its cache.
My first though to verify [...]]]></description>
			<content:encoded><![CDATA[<p>When I made the switch to another hosting company, I had to point my domain name (briandicroce.com) to their DNS servers. After that, I couldn’t access my site from the domain because the DNS servers associated with my ISP had the previous DNS settings of my domain name in its cache.</p>
<p>My first though to verify that everything was working fine with the new DNS settings was to visit my site from another computer hooked up with another ISP. So I launched a remote desktop session with my PC at work, and from there I could clearly see that my site was accessible and running well. </p>
<p>I pinged my domain name from that PC and received an IP different from the one I got when pinging the same domain name from my own PC hooked up with my ISP. So which IP was the right one then? I visited <a href="http://network-tools.com/">Network-Tools.com</a> to get this answer. Bingo! My ISP’s DNS server was pointing my domain name to the previous (old) IP addresses. I called the technical support, but they told me that they cannot flush the cache of the DNS server for one site.</p>
<p>So I tried a couple of different things on my end:</p>
<ul>
<li>I tried to release and renew my network adapters with these commands:<strong> ipconfig /release</strong> and<strong> ipconfig /renew</strong>. </li>
<li>I tried to flush my local DNS resolver cache: <strong>ipconfig /flushdns</strong>. </li>
</ul>
<p>Nothing! Every time I would visit my site, I would still receive an error somewhat to a “Could not locate remote server” (I’m an Opera fan). Even after doing a <strong>tracert briandicroce.com</strong>, I could see that the route would break after a couple of hops.</p>
<p>Anyhow, I managed to fix my problem by modifying the local <strong><a href="http://en.wikipedia.org/wiki/Hosts_file">HOSTS</a></strong> file (see link to find the location of this file in your computer) and manually mapping my domain names (blog.briandicroce.com and briandicroce.com) to the IP addresses of my new hosting servers. Here are the steps to follow in case you’re having the same problem I did (Windows machine only):</p>
<ol>
<li>Edit the <strong>HOSTS </strong>file (no extension) with a text editor using Administrator privileges. The location of this file is in <strong>%SystemRoot%\system32\drivers\etc\ </strong>by default. Note that users of 64-bit versions of Windows Server 2003/2008, and Vista (Windows 7 excluded) cannot access the HOSTS file with a 32-bit editor. </li>
<li>Enter the mappings of your domain names to their corresponding IP addresses. The first value should be the IP address that the domain name (second value) should point at. Make sure to separate the IP Address from the domain name with a whitespace. </li>
<li>Save the file. </li>
</ol>
<p>And that’s it. Now when you access your domain name, Windows will check to see if there’s an entry for it in the HOSTS file and will use its corresponding IP address, therefore bypassing your ISP’s DNS server’s resolver to direct to the site.</p>
<p>After a couple of days/weeks/months, you could probably remove those entries in the HOSTS file in the case that the DNS server actually flushed its cache.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/10/15/mapping-a-domain-name-to-an-ip-address-in-the-hosts-file-to-bypass-a-dns-resolver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another jewel from Sysinternals: Desktops, a multiple virtual desktops manager</title>
		<link>http://blog.briandicroce.com/2009/10/10/another-jewel-from-sysinternals-desktops-a-multiple-virtual-desktops-manager/</link>
		<comments>http://blog.briandicroce.com/2009/10/10/another-jewel-from-sysinternals-desktops-a-multiple-virtual-desktops-manager/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 19:06:37 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/10/10/another-jewel-from-sysinternals-desktops-a-multiple-virtual-desktops-manager/</guid>
		<description><![CDATA[I love Windows 7. It makes me enjoy my ThinkPad T61 even more! It’s fast. It’s not too greedy for RAM. And it’s fast! Super fast!
Sure, there are some extra things I wish it had, such as the possibilities to extend the taskbar on external monitors and to pin windows on top of other windows, [...]]]></description>
			<content:encoded><![CDATA[<p>I love Windows 7. It makes me enjoy my ThinkPad T61 even more! It’s fast. It’s not too greedy for RAM. And it’s fast! Super fast!</p>
<p>Sure, there are some extra things I wish it had, such as the possibilities to extend the taskbar on external monitors and to pin windows on top of other windows, etc., but I understand that we can’t have it all…all the time. I’m a software developer too, so I get it.</p>
<p>Anyhow, as I was looking around for a good “multiple virtual desktops” application that I could use to load applications in various desktops, I managed to find a little jewel : <a href="http://technet.microsoft.com/en-us/sysinternals/cc817881.aspx"><strong>Windows Sysinternals Desktops</strong></a>. If you know about <a href="http://www.sysinternals.com/">Windows Sysinternals</a>, then you know that those guys don’t release crap. This little app is great because it’s small and it works! No hassles! Here’s a small description of it that I’ve taken from their website:</p>
<blockquote><p>Unlike other virtual desktop utilities that implement their desktops by showing the windows that are active on a desktop and hiding the rest, Sysinternals Desktops uses a Windows desktop object for each desktop. Application windows are bound to a desktop object when they are created, so Windows maintains the connection between windows and desktops and knows which ones to show when you switch a desktop. That making Sysinternals Desktops very lightweight and free from bugs that the other approach is prone to where their view of active windows becomes inconsistent with the visible windows.</p>
</blockquote>
<p><img style="border-bottom: 0px; border-left: 0px; margin: 0px auto 10px; display: block; float: none; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/10/image_thumb.png" width="394" height="300" />I kept the hotkeys by default, so that Desktop 1 is mapped to ALT+1, Desktop 2 is mapped to ALT+2, etc. That way, I can have Outlook and Opera sitting quietly on Desktop 1, Visual Studio on Desktop 2, a virtual machine on Desktop 3, and Word/Excel on Desktop 4. That’s right, you only get four virtual desktops with this little nifty tool (why would anyone need more than four?). And it works great with multiple monitors too!</p>
<p>What I like about it, is that each virtual desktop manages its own taskbar, system tray, and applications. Which means that when an application is loaded, it is running in its own desktop. Suppose you loaded Visual Studio on Desktop 3, but you’re currently working on Desktop 1. If you shutdown Windows, it will say that some apps are still running and you might not know why or where…the best guess is that it is running in one of the other desktops.</p>
<p>One little caveat of using this tool is that my favorite Windows application launcher, <a href="http://www.launchy.net/">Launchy</a>, will only work on Desktop 1. So, I’m kinda screwed if I’m in Desktop 2, 3 or 4 and trigger Launchy…because it won’t work! But like I have previously said, I’m a software developer too, so I get it that I can’t get it all…all the time. If you know how to make Launchy works on all virtual desktops, please tell me how you did it. It will be very much appreciated.</p>
<p>If you’re looking for a fast, free, small (62kb), simple virtual desktops manager that runs once downloaded (there’s no useless installer with this one!), look no more! Windows Sysinternals Desktops is the tool you need. I love it!</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/10/10/another-jewel-from-sysinternals-desktops-a-multiple-virtual-desktops-manager/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Migrating a Wordpress site from BlueHost to WebHost4Life</title>
		<link>http://blog.briandicroce.com/2009/10/10/migrating-a-wordpress-site-from-bluehost-to-webhost4life/</link>
		<comments>http://blog.briandicroce.com/2009/10/10/migrating-a-wordpress-site-from-bluehost-to-webhost4life/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 18:15:47 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[How To...]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/10/10/migrating-a-wordpress-site-from-bluehost-to-webhost4life/</guid>
		<description><![CDATA[This week, I migrated my blog which was hosted by BlueHost to WebHost4Life. I highly recommend BlueHost if you’re looking for a LAMP-based (Linux+Apache+MySQL+PHP) hosting package. Their pricing is fair, the quality of service is excellent and the technical support is unbelievable. The reason I made the switch was that I’m working more with ASP.NET [...]]]></description>
			<content:encoded><![CDATA[<p>This week, I migrated my blog which was hosted by <a href="http://www.bluehost.com">BlueHost</a> to <a href="http://www.webhost4life.com/">WebHost4Life</a>. I highly recommend BlueHost if you’re looking for a LAMP-based (Linux+Apache+MySQL+PHP) hosting package. Their pricing is fair, the quality of service is excellent and the technical support is unbelievable. The reason I made the switch was that I’m working more with ASP.NET MVC and I wanted a provider which supported .NET 3.5 so that I can publish some of my projects in the public domain. After reading some reviews online, I opted for WebHost4Life.</p>
<p>The transition of my blog, which is powered by Wordpress, didn’t go as smoothly as I hoped. While at BlueHost, the blog was being hosted in a LAMP environment. At WebHost4Life, the blog was going to be hosted in a Windows/IIS/ASP.NET environment. I thought it was just a matter of transferring some files from servers and importing a backed-up database script. Nope.</p>
<p>Here are the steps I followed to successfully transfer my Wordpress blog from a LAMP environment&#160; to a Windows/IIS/ASP.NET environment. Or more specifically, how to transfer your Wordpress blog from BlueHost to WebHost4Life:</p>
<p><strong><u>NOTE:</u></strong> Make sure you have properly configured DNS settings for the domain name that points to your blog or site.</p>
<p><strong>On the BlueHost (or LAMP environment) side :</strong></p>
<ol>
<li><a href="http://codex.wordpress.org/Upgrading_WordPress">Upgrade your current Wordpress to the latest version available</a>.</li>
<li>Make sure your plugins work with the latest upgrade. If they don’t, either upgrade or delete them.</li>
<li>Deactivate all your plugins to avoid any problems when migrating your current database.</li>
<li><a href="http://codex.wordpress.org/Backing_Up_Your_Database">Backup your current Wordpress database</a>. This should generate a SQL file which will be used to restore the database in another instance of MySQL.</li>
<li>Make a local copy of your Wordpress/blog directory for backup purposes. You’d normally do this with an FTP client. If you don’t have any FTP client, I highly recommend you use <a href="http://filezilla-project.org/">FileZilla</a>.</li>
</ol>
<p><strong>On the WebHost4Life (or Windows/IIS/ASP.NET environment) side :</strong></p>
<ol>
<li>Install whatever version of Wordpress available through their automated script installer. You want to do this because the automated installer will create the database for you, configure the correct values for the database server name and other settings which you don’t have to enter manually. If you aren’t lucky enough to have an automated script installer provided by your hosting company, simply <a href="http://codex.wordpress.org/Installing_WordPress">install the latest version of Wordpress</a> directly in the server (don’t forget to create the database for your Wordpress blog).</li>
<li><a href="http://codex.wordpress.org/Restoring_Your_Database_From_Backup">Import/restore the backed up database script</a> that was generated in step 4 of the previous section.</li>
<li>Copy all the content of the Wordpress directory that you backed up in step 5 of the previous section in your server. <strong><u>WARNING </u></strong>: Make sure not to overwrite the <strong>wp-config.php</strong> file in the root of your Wordpress installation directory.</li>
<li>Fire a browser and go in the admin page (\wp-admin) and log in to make sure everything is fine.</li>
<li>Activate any plugins that you have deactivated in the previous section.</li>
<li>If you’re using “pretty” permalinks for linking to your posts, they might no longer work because IIS 6.0 does not support <strong>mod_rewrite </strong>(<a href="http://www.ikailo.com/94/url-modrewrite-workaround-iis-60/">read this</a> to know more about this and why it doesn’t work anymore). In short, “pretty” permalinks result in HTTP 404 errors which is actually a false negative since it should work. The solution to make this work is to create a PHP file which provides a URL mod-rewrite workaround for IIS 6.0 and Wordpress (works with v2.8+). <a href="http://www.ikailo.com/94/url-modrewrite-workaround-iis-60/">Read the following article</a> to know how to do this (trust me, it’s very simple). I named that file ‘404-handler.php’ as was suggested in that article. Upload the file somewhere in your Wordpress directory (I just put it in the root installation directory).</li>
<li>Now you should configure the custom error pages handled by IIS so that when a request generates an HTTP 404 error, IIS will redirect to that ‘404-handler.php’ created in the previous page which will take care of the magic. So to this:</li>
<ol>
<li>Log in your WebHost4Life control panel. </li>
<li>Click on the “Site Admin” tab. </li>
<li>Under the “IIS Manager” section, click on “Set Custom Error”.</li>
<li>Click on the “Go” link associated with the domain name for your blog.</li>
<li>For the error associated with 404, click on the “Edit” link.</li>
<li>Specify the location of the ‘404-handler.php’ file (relative path).</li>
<li>That’s it! Your “pretty” permalinks should now work!</li>
</ol>
<li>Now you must set the proper security permissions to your Wordpress directory structure so that you can edit files from the dashboard. To do this, follow these steps:</li>
<ol>
<li>Log in your WebHost4Life control panel.</li>
<li>Click on the “Security” tab.</li>
<li>Under the “Site Access” section, click on “File Permission”.</li>
<li>Locate your Wordpress folder then click on the “Edit” link associated with it.</li>
<li>In the drop down list, select the NETWORK user and give it the following rights: READ, WRITE, EXECUTE and DELETE.</li>
<li>Make sure to tick the checkbox next to “Propagate changes to sub directories and files”.</li>
<li>Click on “Make Changes” (ONCE!!!) and be patient.</li>
</ol>
<li>You will also need to activate/configure the “Cimy Swift SMTP” plugin which comes with the installation to send email notifications from within your Wordpress site. It’s really not that hard. <a href="http://www.webhost4life.com/hostingKB/KnowledgebaseArticle50189.aspx">Read this article to know how to configure the plugin</a>.</li>
<li>That’s it! You’ve made it! See if everything works correctly…or post your question if it didn’t. I’ll try my best to help you out.</li>
</ol>
<p>Best of luck!</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/10/10/migrating-a-wordpress-site-from-bluehost-to-webhost4life/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Why you should use static read-only fields over constants when dealing with strings</title>
		<link>http://blog.briandicroce.com/2009/08/17/why-you-should-use-static-read-only-fields-over-constants-when-dealing-with-strings/</link>
		<comments>http://blog.briandicroce.com/2009/08/17/why-you-should-use-static-read-only-fields-over-constants-when-dealing-with-strings/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 19:40:45 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/08/17/why-you-should-use-static-read-only-fields-over-constants-when-dealing-with-strings/</guid>
		<description><![CDATA[It’s really wonderful when you learn something new once in a while. As I was discussing with another .NET developer about the importance of knowing when to use constants and fields, he corrected me when I mistakenly told him that String.Empty was implemented as a constant.  In fact, if you look inside the String class, [...]]]></description>
			<content:encoded><![CDATA[<p>It’s really wonderful when you learn something new once in a while. As I was discussing with another .NET developer about the importance of knowing when to use constants and fields, he corrected me when I mistakenly told him that String.Empty was implemented as a constant.  In fact, if you look inside the String class, you’ll actually see that String.Empty is not implemented as a constant, but as a field (a static read-only field to be precise).</p>
<p>As I was thinking about the reason behind this, something hit me: I should be conscious of my decision whether I should use a constant or a field when dealing with strings.  The reason is simple: <strong>avoid unnecessary performance hits</strong>.  Take note that the only valid reference type for a constant is either string or null because <strong>you cannot instantiate a reference type in compile-time</strong>, which is when constants are evaluated.</p>
<p>I made a simple test to verify my little theory, since I couldn’t find a good answer for it on the Web. The test consist of a class that declares both a constant string and a static read-only string field. In its entry method, I call various methods which iterate a certain number of times (10, 100, 1000, 10 000 and 100 000) and use either the constant or the static read-only field respectively.  Here’s a sample code I used for this test:</p>
<pre name="code" class="csharp">
class Program
{
  private const string Constant = "Constant";
  private static readonly string field = "field";

  private static void Main(string[] args)
  {
    // 10 iterations
    DoConstant10();
    DoField10();

    // 100 iterations
    DoConstant100();
    DoField100();

    // 1000 iterations
    DoConstant1000();
    DoField1000();

    // 10000 iterations
    DoConstant10000();
    DoField10000();

    // 100000 iterations
    DoConstant100000();
    DoField100000();
  }

  private static void DoField100000()
  {
    string s = string.Empty;
    for (int i = 0; i < 100000; i++)
	  s += field;
  }

  private static void DoConstant100000()
  {
    string s = string.Empty;
    for (int i = 0; i < 100000; i++)
	  s += Constant;
  }

  // And so on...
}
</pre>
<p>Next, I profiled the application with <a href="http://www.jetbrains.com/profiler/">JetBrains DotTrace</a> 3.1 (using the standard configuration options) and got the following results:</p>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image5.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb6.png" border="0" alt="image" width="675" height="240" /></a></p>
<p>We clearly see that for the same iterations, using a string as field rather than a constant has almost a <strong>2:1 performance improvement ratio</strong> over the latter.  Now, the title of the post is “Why you <em><strong>should</strong></em> use…” and not “Why you <strong><em>must</em></strong> use…” because there are some scenarios when using a string constant is better suited, such as when the constant is being used as a case in a switch statement (you cannot use a static readonly field as a case in a switch statement because its value will not be known until runtime).</p>
<p>Another thing you should be conscious of is <a href="http://blog.briandicroce.com/2008/12/30/rely-on-your-experience-and-knowledge-over-some-tools-recommendations/"><strong>relying on your experience rather than a tool’s recommendation</strong></a>. For example, when you declare a static read-only string variable, <a href="http://www.jetbrains.com/resharper/">ReSharper</a> will recommend you to transform it as a constant.  You shouldn’t always obey the tool unless you know exactly the <strong>tradeoffs</strong> and have considered the <strong>impact</strong> and <strong>risks</strong> associated with the decision.</p>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image6.png"><img style="border-right-width: 0px; margin: 0px auto 10px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb7.png" border="0" alt="image" width="505" height="103" /></a> Be a good judge over your codebase. Analyze the evidence and study the tradeoffs. There’s nothing like a good profiler to uncover the truths behind your code.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/08/17/why-you-should-use-static-read-only-fields-over-constants-when-dealing-with-strings/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Copy files from build events within Visual Studio without Windows UAC problems</title>
		<link>http://blog.briandicroce.com/2009/08/16/copy-a-file-in-from-a-build-event-in-visual-studio-without-windows-uac-nagging-you/</link>
		<comments>http://blog.briandicroce.com/2009/08/16/copy-a-file-in-from-a-build-event-in-visual-studio-without-windows-uac-nagging-you/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 01:01:25 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[How To...]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/08/16/copy-a-file-in-from-a-build-event-in-visual-studio-without-windows-uac-nagging-you/</guid>
		<description><![CDATA[In my projects, I often have to copy some files in either a pre-build or post-build event within Visual Studio.&#160; Since installing Windows 7, it seems that the copy command from those build events generates an error with a not very helpful message such as the following one:
The “..exited with code 1” leaves me dreaming [...]]]></description>
			<content:encoded><![CDATA[<p>In my projects, I often have to copy some files in either a pre-build or post-build event within Visual Studio.&#160; Since installing Windows 7, it seems that the copy command from those build events generates an error with a not very helpful message such as the following one:</p>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image2.png"><img style="border-right-width: 0px; margin: 0px auto 10px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb3.png" width="673" height="140" /></a>The “<em>..exited with code 1</em>” leaves me dreaming about a ton of causes for this error. When manually copying the file to the destination directory (as copying AmazonBookLinker.dll to C:\Program Files\Windows Live\Writer\Plugins\) via Windows Explorer, I get the following second strike:</p>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image3.png"><img style="border-right-width: 0px; margin: 0px auto 10px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb4.png" width="496" height="248" /></a>At this point, I’m playing around with security permissions for both files and destination directory without any positive result.&#160; The solution I came about this problem is to actually <strong>run Visual Studio with administrator privileges</strong> by right-clicking your Visual Studio file icon, and selecting <strong>“Run as administrator</strong>”. If you want to make this a permanent solution:</p>
<ol>
<li>Open the <strong>properties window</strong> for Visual Studio </li>
<li>Go to the <strong>Compatibility</strong> tab </li>
<li>Under the “Privilege Level” box, check the “<strong>Run this program as an administrator</strong>” </li>
<li>Click on OK </li>
<li>[Optionally] Click on the “<strong>Change settings for all users</strong>” button apply this privilege to all users on the machine. </li>
</ol>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image4.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb5.png" width="379" height="535" /></a></p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/08/16/copy-a-file-in-from-a-build-event-in-visual-studio-without-windows-uac-nagging-you/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing NVIDIA drivers on Windows 7</title>
		<link>http://blog.briandicroce.com/2009/08/10/installing-nvidia-drivers-on-windows-7/</link>
		<comments>http://blog.briandicroce.com/2009/08/10/installing-nvidia-drivers-on-windows-7/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 01:48:45 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[How To...]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/?p=621</guid>
		<description><![CDATA[I’ve recently installed the latest operating system from the good minds at Redmond, Windows 7 Ultimate, on my Lenovo Thinkpad T61.  The only problem that showed up after the installation (which I should add was the fastest and easiest installation of Windows so far) was that my video card’s drivers, a NVidia 140m card, wouldn’t [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve recently installed the latest operating system from the good minds at Redmond, Windows 7 Ultimate, on my Lenovo Thinkpad T61.  The only problem that showed up after the installation (which I should add was the fastest and easiest installation of Windows so far) was that my video card’s drivers, a NVidia 140m card, wouldn’t install successfully from <a href="http://www.nvidia.com/Download/index.aspx?lang=en-us">NVIDIA’s Windows 7 drivers download page</a>.  This meant that I couldn’t support neither the native resolution (1680&#215;1050), nor dual monitors on the dock station.</p>
<p>As a matter of fact, I would get the following error message when launching the drivers setup file (186.03_notebook_win7_32bit_international_whql.exe).</p>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb.png" border="0" alt="image" width="447" height="161" /></a></p>
<p>Therefore, I had to get my little act together and manually install the drivers.  The following instructions should help you install your NVIDIA’s video card drivers on Windows 7 if the setup file won’t do it for you…for some reason.</p>
<ol>
<li>Go to <a href="http://www.nvidia.com/Download/index.aspx?lang=en-us">NVIDIA’s Windows 7 drivers download page</a> and download the drivers specific to your video card.</li>
<li>Run the downloaded executable which should extract the files in a directory (by default C:\NVIDIA\DisplayDriver\###\International, where ### corresponds to the version of the setup file.  Mine was <strong>186.03</strong>, therefore the directory was C:\NVIDIA\DisplayDriver\<strong>186.03</strong>\International).</li>
<li>Browse to that directory (C:\NVIDIA\DisplayDriver\###\International) and in the <strong>Display</strong> folder, open the <strong>ListDevices.txt</strong> file with your favorite text editor.</li>
<li>At this time, you’re looking for the specific INF file which holds the drivers information for your card. This step should be easy if you know the model of your video card (check your PC manufacturer’s website if you have no clue what is the model of your video card). For example, my video card is a <strong>NVIDIA Quadro NVS 140M</strong>. Therefore, I would either search for “NVIDIA Quadro NVS 140M” precisely or simply “140M”.  The search result tells me that the INF file I should be interested in is <strong>NVDM.INF</strong> (the one in a red square in the following screenshot).</li>
</ol>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb1.png" border="0" alt="image" width="516" height="669" /></p>
<ol start="5">
<li>Now that we know which file holds the drivers information, we should locate it without problem. In my case its path is C:\NVIDIA\DisplayDriver\186.03\International\Display\NVDM.INF.</li>
<li>Now open your display settings (Control Panel\All Control Panel Items\Display\Screen Resolution) and click on the “<strong>Advanced settings</strong>” link.</li>
</ol>
<p><a href="http://blog.briandicroce.com/wp-content/uploads/2009/08/image1.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/08/image-thumb2.png" border="0" alt="image" width="800" height="640" /></a></p>
<ol start="7">
<li>
<div>On the<strong> Advanced Settings</strong> window, click on the <strong>Adapter</strong> tab, then on the <strong>Properties</strong> button.</div>
</li>
<li>
<div>From the adapter properties window, click on the <strong>Driver</strong> tab, then on the <strong>Update Driver…</strong> button.</div>
</li>
<li>
<div>On the “How do you want to search for updated driver software?” window, click on the “<strong>Browse my computer for driver software</strong>” button.</div>
</li>
<li>
<div>On the “Browse for driver software on your computer” window, click on the “Let me pick from a list of device drivers on my computer” button.</div>
</li>
<li>
<div>On the “Select the device driver you want to install for this hardware” window, click on the <strong>Have Disk…</strong> button.</div>
</li>
<li>
<div>On the “Copy manufacturer’s files from:” text box, simply specify the INF file path from <strong>step 5</strong> or simply browse for it, and press the <strong>OK</strong> button when done with this step.</div>
</li>
<li>
<div>Now simply select your NVIDIA video card model from the list and click on the <strong>Next</strong> button (click on <strong>YES</strong> if you get a warning from updating the driver). Windows 7 should now be installing the drivers for your video card.</div>
</li>
<li>
<div>Restart your computer to apply the new video settings.</div>
</li>
</ol>
<p>I hope this post helped you to install your NVIDIA video card drivers on Windows 7. Feel free to post you question or comments.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/08/10/installing-nvidia-drivers-on-windows-7/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Quick access to useful documents inside Visual Studio</title>
		<link>http://blog.briandicroce.com/2009/04/19/quick-access-to-useful-documents-inside-visual-studio/</link>
		<comments>http://blog.briandicroce.com/2009/04/19/quick-access-to-useful-documents-inside-visual-studio/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 03:22:56 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/04/19/quick-access-to-useful-documents-inside-visual-studio/</guid>
		<description><![CDATA[Here’s one of those little tips that can save you time and useless brain processing when the need arises.&#160; When developing in Visual Studio, does it ever happen that you need to read some documentation pertaining to your current work?&#160; In my case it happens a lot.&#160; I don’t like to switch context and fire [...]]]></description>
			<content:encoded><![CDATA[<p>Here’s one of those little tips that can save you time and useless brain processing when the need arises.&#160; When developing in Visual Studio, does it ever happen that you need to read some documentation pertaining to your current work?&#160; In my case it happens a lot.&#160; I don’t like to switch context and fire up a web browser to find the needed documentation, or even launch Windows Explorer to find that document in some obscure, deep directory hierarchy.&#160; No, instead I like to have everything I need, whether tools or documentations, under my fingers in one place: inside Visual Studio.&#160; </p>
<p>Two documentations that I use the most when programming in .NET are the <a href="http://www.jetbrains.com/resharper/documentation/documentation.html" target="_blank">ReSharper 4.5 Default Keymaps</a> and the <a href="http://www.ecma-international.org/publications/standards/Ecma-334.htm" target="_blank">C# Language Specification</a> (ECMA-334).&#160; In order to quickly access those two documents (and future documents I might need), I simply created an entry in the External Tools section of Visual Studio.&#160; In order to do this, you can follow these simple steps:</p>
<ol>
<li>Go to <strong>Tools </strong>-&gt;<strong> External Tools…</strong></li>
<li>In the External Tools window, click on the <strong>Add</strong> button to create a new entry</li>
<li>Fill in the required information, such as Title, Command, Arguments, etc.&#160; In my case, the Command is simply the path to the executable of my favorite PDF reader (Foxit Reader) and the Arguments is simply the path to the PDF file.&#160; You can also define a keyboard shortcut for them by prefixing the “<strong>&amp;”</strong> symbol to one of the letters that aren’t already defined as shortcuts.&#160; </li>
<li>When you’re done, click on <strong>OK</strong>.&#160; </li>
</ol>
<p>Take a look at the following screenshot:</p>
<p><img title="" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="458" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image19.png" width="471" border="0" /> </p>
<p>With this little setup, I don’t need to go to either the ReSharper or ECMA’s website, not even launch Windows Explorer to look for those files.&#160; I have access to those files directly from Visual Studio.&#160; No more context switching.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/04/19/quick-access-to-useful-documents-inside-visual-studio/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Three talks you should watch this week (Series #1)</title>
		<link>http://blog.briandicroce.com/2009/04/16/three-talks-you-should-watch-this-week-series-1/</link>
		<comments>http://blog.briandicroce.com/2009/04/16/three-talks-you-should-watch-this-week-series-1/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 11:00:00 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Media/Presentations]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/04/16/three-talks-you-should-watch-this-week-series-1/</guid>
		<description><![CDATA[Whenever I can’t get myself too tired to go to sleep, or I’m on the bus commuting to work or to a client, or plain simply have an hour or two open in my schedule, I often fill it up watching a few talks online.  Though I’m passionate about software development and technology in general, [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever I can’t get myself too tired to go to sleep, or I’m on the bus commuting to work or to a client, or plain simply have an hour or two open in my schedule, I often fill it up watching a few talks online.  Though I’m passionate about software development and technology in general, I don’t always focus entirely on it, as it isn’t very fulfilling for the imaginative mind.  Recently, I’ve listened to some great talks about technologists speaking about starting online businesses, marketing, life, faith, etc., which really pump me up to a level that makes me thankful to live, to dream and to achieve what I value most in life.  I’ll start making a series out of this one, as I believe it can be as helpful and motivating to you, as it is for me.  That being said, here are the first three talks I recommend you to watch this week:</p>
<ol>
<li value="1">David Heinemeier Hansson on <a href="http://www.omnisio.com/startupschool08/david-heinemeier-hansson-at-startup-school-08" target="_blank"><strong>The Secret To Making Money Online</strong></a> (less than 30 minutes)</li>
</ol>
<blockquote><p>In this talk given in 2008, David Heinemeier, the creator of the Ruby on Rails framework and partner at 37Signals, gives a very creative and down-to-earth talk on how to create a profitable startup company.</p></blockquote>
<ol>
<li value="2">Guy Kawasaki on <a href="http://www.zentation.com/viewer/index.php?passcode=epbcSNExIQr" target="_blank"><strong>The Art of Innovation</strong></a> (less than 60 minutes)</li>
</ol>
<blockquote><p>This is the first time I heard Guy Kawasaki speak.  This guy is a one of my “online heroes”.  He’s smart, he’s funny, and he speaks from experience.  In this talk, Guy talks about innovation about products, services and marketing.  He shares his experience from his work at Apple to now as a venture capitalist.  You’ll also learn about the 10-20-30 rule that can increase your chances in not only getting fund for your business, but most importantly, getting your point across.</p></blockquote>
<ol>
<li value="3">Joel Spolsky on <a href="http://vodpod.com/watch/1019132-joel-spolsky-at-railsconf-2008" target="_blank"><strong>What Makes a Great Software</strong></a> (less than 60 minutes)</li>
</ol>
<blockquote><p>This is a talk that Joel Spolsky, founder of Fogcreek Software, gave as a keynote at RailsConf 2008, where he shares his valuable insight in what <em>really</em> makes a great software, so much so that we can actually qualify the software as <em>great</em>.  His talk is fantastic: it’s entertaining, it’s focused and it’s without a doubt an eye-opener for software developers to watch out some common pitfalls that can make your software not being adopted at all by potential users.</p></blockquote>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/04/16/three-talks-you-should-watch-this-week-series-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Architectural Journal, a quarterly journal about software architecture</title>
		<link>http://blog.briandicroce.com/2009/04/15/the-architectural-journal-a-quarterly-journal-about-software-architecture/</link>
		<comments>http://blog.briandicroce.com/2009/04/15/the-architectural-journal-a-quarterly-journal-about-software-architecture/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 10:04:00 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/04/15/the-architectural-journal-a-quarterly-journal-about-software-architecture/</guid>
		<description><![CDATA[ Microsoft has many valuable resources for .NET developers under its MSDN umbrella: newsgroups, blogs, forums, magazines, etc.&#160; One very valuable resource from Microsoft that seems to be ignored by most developers is The Architectural Journal:
The Architectural Journal is an independent platform for free thinkers and practitioners of IT architecture.&#160; New editions are issued quarterly [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="135" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image17.png" width="102" align="right" border="0" /> Microsoft has many valuable resources for .NET developers under its MSDN umbrella: newsgroups, blogs, forums, magazines, etc.&#160; One very valuable resource from Microsoft that seems to be ignored by most developers is <a href="http://msdn.microsoft.com/en-us/architecture/bb410935.aspx" target="_blank"><strong>The Architectural Journal</strong></a>:</p>
<blockquote><p>The Architectural Journal is an independent platform for free thinkers and practitioners of IT architecture.&#160; New editions are issued quarterly with articles designed to offer perspective, share knowledge, and help you learn the discipline and pursue the art of IT architecture.&#160; The Architectural Journal reaches over 67,00 subscribers worldwide.</p>
</blockquote>
<p>I think one reason that it isn’t getting much attention is because its “feedless”, meaning you can’t directly subscribe to a feed, through your browser, that will notify you whenever a new issue has been published.&#160; So in order to get notified when a new issue has been published, you need to subscribe through a fairly simple, but tedious, <a href="http://msdn.microsoft.com/en-us/architecture/aa699369.aspx#Subscribe" target="_blank">subscription workflow</a>.&#160; Once subscribed, you’ll get the quarterly journal as a PDF as soon as it’s released.&#160; In short, here are the steps you should follow in order to subscribe to The Architectural Journal:</p>
<p>Go to the <a href="https://profile.microsoft.com/RegSysProfileCenter/SubCntDefault.aspx" target="_blank">e-newsletters page</a> in the <strong>Profile Center</strong>.&#160; You may be required to sign up for a Windows Live ID account if you don’t have one already.&#160; You may use your current email account or create a new one if you don’t have one.&#160; From there, you follow these three steps:</p>
<ol>
<li>Find <strong>The Architectural Journal</strong> in the list of available communications on the left and select it. </li>
<li>Press the <em>Subscribe</em> button.&#160; You should now see your choices in the list on the right. </li>
<li>Select your delivery format: HTML (suggested) or plain text. </li>
</ol>
<p><img title="" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="343" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image18.png" width="624" border="0" /> </p>
<p>(<u>Note</u>: it would’ve been much easier had someone put an RSS feed link instead.)</p>
<p>So that’s it! Now, you should be getting four issues per year.&#160; All great content about software architecture topics.&#160; All free.&#160; Past topics include:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/architecture/dd129905.aspx" target="_blank">Distributed Computing</a></li>
<li><a href="http://msdn.microsoft.com/en-us/architecture/cc505966.aspx" target="_blank">The Role of an Architect</a></li>
<li><a href="http://msdn.microsoft.com/en-us/architecture/bb985491.aspx" target="_blank">Mobile Architecture</a></li>
<li><a href="http://msdn.microsoft.com/en-us/architecture/bb906058.aspx" target="_blank">Software + Services</a></li>
<li><a href="http://msdn.microsoft.com/en-us/architecture/bb735857.aspx" target="_blank">Web Architecture</a></li>
<li>And more.</li>
</ul>
<p>You can also skip the whole subscription process and <a href="http://msdn.microsoft.com/en-us/architecture/bb201656.aspx" target="_blank">directly download the current and previous issues</a> in English, French, Spanish, etc., as a compressed (ZIP) file.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/04/15/the-architectural-journal-a-quarterly-journal-about-software-architecture/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>My five favorite features in ReSharper 4.5</title>
		<link>http://blog.briandicroce.com/2009/04/13/my-five-favorite-features-in-resharper-45/</link>
		<comments>http://blog.briandicroce.com/2009/04/13/my-five-favorite-features-in-resharper-45/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 22:37:42 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/04/13/my-five-favorite-features-in-resharper-45/</guid>
		<description><![CDATA[With the recent release of ReSharper 4.5, many productive features have been added to this already great product, such as performance and memory usage improvements, solution-wide code inspections, VB9 support, and many more.&#160; I’ve been using ReSharper as my pair programmer for many years as it helps me to enhance my C# refactoring whenever I [...]]]></description>
			<content:encoded><![CDATA[<p>With the recent release of <a href="http://www.jetbrains.com/resharper/index.html" target="_blank">ReSharper</a> 4.5, many productive <a href="http://www.jetbrains.com/resharper/features/index.html" target="_blank">features</a> have been added to this already great product, such as performance and memory usage improvements, solution-wide code inspections, VB9 support, and many more.&#160; I’ve been using ReSharper as my <em>pair programmer</em> for many years as it helps me to enhance my C# refactoring whenever I overlook some portions of code I’m writing, as well as to manage my unit tests.&#160; In this post, I will concentrate on sharing with you my five most favorite new features in ReSharper 4.5.</p>
<h4>1. Find dependent or referenced code</h4>
<p>Using this new feature, you can find dependent or referenced code at either a project or file level.&#160; Before going forward with this feature, it is important to understand what is meant by “dependent code” and “referenced code”.&#160; Both of these terms have to do with dependencies/coupling.&#160; When you ask ReSharper to tell you the <strong>dependent code at the project level</strong>, you are actually asking it to tell you which other projects (or assemblies) are dependent on that specific project.&#160; On the other hand, when you ask ReSharper to tell you the <strong>referenced code at the project level</strong>, you are asking it to tell you which other projects (or assemblies) that specific project depends on.&#160; For example, suppose the following UML class diagram:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="271" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image6.png" width="309" border="0" /> </p>
<p>In this scenario, I have four different assemblies: one client console executable (Client), and three class libraries (Provider, Utilities and SecurityManager).&#160; Our Client executable references the three other assemblies because it’ll make use of their services.&#160; Those references were created when I asked Visual Studio to add me the references for those three projects at the project level.&#160; Now, if I were to ask ReSharper to tell me the <strong>dependent code at the project level</strong> (which means that you right-click on the Client project in the Solution Explorer, and then click on the <em>Find Dependent Code</em> contextual menu item), a message box will appear telling me that “Code dependent on project ‘Client.csproj’ was not found”:&#160; </p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="171" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image7.png" width="422" border="0" /> </p>
<p>This is normal, because according to our class diagram above, Client has an <strong>afferent coupling</strong> of zero (Ca = 0).&#160; </p>
<blockquote><p><strong>Afferent coupling</strong>(s) is the number of class(es) outside this component (<em>i.e.,</em> Client) that depend on classes within this component (<em>i.e.,</em> Client).&#160; </p>
</blockquote>
<p>On the other hand, if we were to ask ReSharper to tell us the <strong>referenced code at the project level</strong> (for Client), by this time clicking on the <em>Find Referenced Code</em> contextual menu item, you will get the following results:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="570" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image8.png" width="463" border="0" /> </p>
<p>In this case, ReSharper is telling us the types which are being used in that specific project (Client), even .NET types and assemblies like mscorlib.dll.&#160; Since the Client class has a positive non-zero <strong>efferent coupling</strong> (Ce &gt; 0), we actually get a result.</p>
<blockquote><p><strong>Efferent coupling</strong>(s) is the number of classes inside this component (<em>i.e.,</em> Client) that depend on classes outside this component (<em>i.e.,</em> Client).</p>
</blockquote>
<p>These results could look daunting at first sight, but that’s because the results window is showing too much information.&#160; All you have to do to get a more simplistic and lighter view of the results is simply to click on the <em>Filter Usages</em> button (that’s the one surrounded in red in the screenshot below) and unselect the <em>Show Attribute Usage</em> from the proposed choices, as you don’t really care that you’re depending on the AssemblyAttribute class for example.&#160; Furthermore, you should also click on the <em>Merge Occurences on the Same Line</em> button (that’s the one surrounded in green in the screenshot below), so that you eliminate some of the redundancy usages on the same line.&#160; Finally, choose an appropriate view to group your results (in this case, I have chosen to group the results by Namespaces and Types.&#160; That’s the one surrounded in blue in the screenshot below).&#160; This will give you the following (much more compact) result view:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="324" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image9.png" width="463" border="0" /> </p>
<blockquote><p>NOTE: If you only reference external projects or assemblies in a project without actually using the public types or members in those external dependencies, ReSharper will not tell you that you are actually referencing those dependencies.&#160; This is pretty slick, as you can visually know whether or not a reference is unnecessary, and thus proceed with its removal.</p>
</blockquote>
<p>Ah, much better! And you know what?&#160; That’s pretty much right, because the Client class is actually using those classes (Provider, SecurityManager and Utilities), as shown in the screenshot below:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="172" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image10.png" width="574" border="0" /> </p>
<p>At the <strong>file level</strong>, you only get to ask ReSharer to tell you the referenced code, through the <em><strong>Find Referenced Code</strong></em> contextual menu item.&#160; In this case, the results will include any type being used in that file.&#160; Suppose for example that in the SecurityManager class, you’re using an instance of a Logging class to log some errors in a few critical methods.&#160; In that case, the results will include the Logging class, as it is a type being referenced in that file.&#160; So far, so smart.</p>
<h4>2. Go to implementation (Ctrl+Shift+Alt+B)</h4>
<p>This one is pretty simple and very much useful.&#160; Suppose you have the following class hierarchy:</p>
<p><img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="367" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image11.png" width="450" border="0" /> </p>
<p>In the previous versions of ReSharper, you couldn’t ask the tool to directly tell you which concrete classes derived from an abstract type.&#160; Instead, you had to select the abstract type and ask it to tell you the derived types (or inheritors) through the Ctrl+Alt+B shortcut, which would give you a list of all the types deriving from that abstract type…even if those derived types were abstract themselves.&#160; This was pretty frustrating when you had a hierarchy with a lot of abstract relationships amongst types, as it was the case in one WCF-driven project I was involved in, because you would lose a couple of seconds filtering out the concrete types from the abstract ones in the results list.&#160; Well, that’s an old story because now you can get the list of all the concrete types (or implementation types) directly from a new shortcut: <strong>Ctrl+Shift+Alt+B</strong>, or through the “Go to implementation” command in the “Go to” navigation menu.&#160; For example, if you asked ReSharper to tell you the concrete classes for IProvider, it’ll tell you that MembershipProvider and SecurityProvider are the only ones.&#160; It will ignore ProviderBase, as it is abstract.&#160; Well done!</p>
<h4>3. Naming styles</h4>
<p>With ReSharper 4.5, we get more flexibility in the naming style department.&#160; You can apply either a common naming style for both C# and VB code, or set one differently for each of them.&#160; It’s up to you.&#160; The UI interface to manage the naming styles is pretty simple.&#160; Here, for example, is the Naming Style window pertaining to both C# and VB:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="715" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image12.png" width="734" border="0" /> </p>
<p>So you get a list of predefined styles out-of-the-box which might suit most of your basic needs.&#160; The flexibility offered by these features comes in handy when you want to create new styles, modify some existing ones, etc.&#160; Suppose you want to add a new naming style for private fields only, and make them prefixed by an underscore.&#160; You would simply click on the “Add” button under the <em>User defined naming rules</em> panel, which will show the following window:</p>
<p>&#160;<img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="555" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image13.png" width="739" border="0" /> </p>
<p>All I have to do is select the affected entities, which in this case would be “Field” and “Readonly field”.&#160; Then I would specify the access modifiers, which sould be Private and Protected. Following that, I will specify the underscore as the name prefix and tick the lowerCamelCase naming style, as to be follow the <a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx" target="_blank">.NET design guidelines</a>.&#160; Finally, I would set the rule description to something explicit, such as “Private fields”.&#160; That’s it!&#160; Now I can save and share the code style file with my team.</p>
<h4>4. Collapse all project nodes (in Solution Explorer)</h4>
<p>Suppose you have the following project structure in Solution Explorer:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="612" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image14.png" width="277" border="0" /> </p>
<p>This would be a very typical folder structure in an ASP.NET MVC project, for example.&#160; Every time you collapse the node associated with the SecurityManager project, and you expand it later, you will see the same result as above.&#160; This can be very frustrating when you have dozens of projects with their child nodes expanded in a similar way in Solution Explorer, and God forbid you have also expanded the References node, because it tends to visually pollute the Solution Explorer.&#160; ReSharper 4.5 introduced a new feature that allows you to definitively collapse a node, be it a folder or the project node itself, so that the next time you expand the root node, the previously collapsed node stays closed!&#160; This might sound like nothing, but have you ever calculated how many seconds you probably were wasting trying to navigate the Solution Explorer when you have a multitude of projects with folders, or solution folders, all open at the same time even though you don’t really need them to be?&#160; That’s not being productive.&#160; That’s not being professional.</p>
<p>So here’s the trick: when you’re done with one node and you need to collapse it for good, simply right-click on it in the Solution Explorer, and choose “<strong>Collapse All</strong>”.&#160; This would permanently collapse the node until you explicitly expand it again.&#160; For example, if I decide to execute this “Collapse All” command on the Users folder, then each time I expand the SecurityManager node, the Users folder will stay collapsed, until I explicitly expand it.&#160; In my opinion, this should’ve been a standard option for Visual Studio.&#160; Good thinking once again!</p>
<h4>5. Inline field (Ctrl+Alt+N)</h4>
<p>The fifth of my favorite new features in ReSharper 4.5 is the possibility to inline a field.&#160; As to tweak the original <a href="http://www.refactoring.com/catalog/inlineTemp.html" target="_blank">Inline Temp</a> refactoring definition to suit this one: </p>
<blockquote><p><em>You have a <strong>field</strong> that is assigned to once with a simple expression, and the <strong>field</strong> is getting in the way of other refactorings.&#160; Replace all references to that <strong>field</strong> with the expression.</em></p>
</blockquote>
<p>I think that little definition sums it up well what Inline Field does.&#160; Simply highlight the field, then hit the <strong>Ctrl+Alt+N</strong> shortcut, and you’re done.&#160; Here’s a last example.&#160; Suppose you have the following class definition:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="258" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image15.png" width="530" border="0" /> </p>
<p>As you can see, the <em>someString</em> field is used only once in this class, which is pretty much useless.&#160; In that case, I will place my cursor on it, then hit Ctrl+Alt+N to have it inlined:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="230" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image16.png" width="454" border="0" /> </p>
<p>And that’s it! Now through all the code, the field’s expression will be inlined.&#160; This was a simple scenario, but I’m sure you can think how useful this refactoring could be in your codebase.</p>
<p><strong>[UPDATE: 2009-04-14]</strong> The <em>Inline Field</em> refactoring also works to inline methods and properties, not only fields! This is very sweet.&#160; They should call this command <em>Inline Construct</em> as it doesn’t just inline fields.</p>
<h4>But there’s more!!!</h4>
<p>Of course, my list of favorite new features in ReSharper 4.5 isn’t the whole story behind this new release.&#160; For a complete list of new feature in ReSharper 4.5, I’ll invite you to review its <a href="http://www.jetbrains.com/resharper/features/newfeatures.html" target="_blank">What’s New in ReSharper 4.5</a> features page. ReSharper is getting better and better through each release, and I can’t wait to see what the team is going to include in the 5.0 release.&#160; But meanwhile, if you haven’t upgraded to this new version, I urge you to do so.&#160; Plus, the upgrade is free if you’re already a ReSharper 4.x licensee, so you really have no excuse.&#160; And if you’re not currently using ReSharper at all, then I hope that the following post has incited you to ask your tech lead or manager to get a license for it (I would suggest to start with the trial, then demo it to your group).&#160; I’ll also invite you to read my post on <a href="http://blog.briandicroce.com/2008/05/12/the-case-for-resharper-in-the-enterprise/" target="_blank"><strong>The Case for ReSharper in the Enterprise</strong></a> which might give you some pointers on the necessity of this tool in your development toolbox.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/04/13/my-five-favorite-features-in-resharper-45/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Book Review #10: 97 Things Every Software Architect Should Know</title>
		<link>http://blog.briandicroce.com/2009/04/12/book-review-10-97-things-every-software-architect-should-know/</link>
		<comments>http://blog.briandicroce.com/2009/04/12/book-review-10-97-things-every-software-architect-should-know/#comments</comments>
		<pubDate>Sun, 12 Apr 2009 13:09:06 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/04/12/book-review-10-97-things-every-software-architect-should-know/</guid>
		<description><![CDATA[What started as an article a few years ago, titled 10 Things Every Software Architect Should Know, has morphed into a collection of essays written by software professionals and for software professionals. The author of the original article, Richard Monson-Haefel, did well on following the suggestion of Jay Zimmerman to publish a book as a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.com/Things-Every-Software-Architect-Should/dp/059652269X%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dbrdicr-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D059652269X" target="_blank"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" title="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image5.png" border="0" alt="image" width="180" height="260" align="right" /></a></p>
<p>What started as an article a few years ago, titled <a href="http://www.slideshare.net/oreillymedia/oreilly-webcast-ten-things-every-software-architect-should-know"><em>10 Things Every Software Architect Should Know</em></a>, has morphed into a collection of essays written by software professionals and for software professionals. The author of the original article, <a href="http://www.monson-haefel.com/" target="_blank">Richard Monson-Haefel</a>, did well on following the suggestion of Jay Zimmerman to publish a book as a collection of essays sharing pragmatic experience, wisdom, insight and diversity in the field of software architecture.  From the back cover, we can read the following:</p>
<blockquote><p><em>In this truly unique technical book, today’s leading software architects present valuable principles on key development issues that go way beyond technology.  More than four dozen architects &#8211; including Neal Ford, Michael Nygard, and Bill de hÒra &#8211; offer advice for communicating with stakeholders, eliminating complexity, empowering developers, and many more practical lessons they’ve learned from their years of experience.</em></p></blockquote>
<p>Published by O’Reilly, “<a href="http://www.amazon.com/Things-Every-Software-Architect-Should/dp/059652269X%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dbrdicr-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D059652269X"><strong>97 Things Every Software Architect Should Know</strong></a>” turns out to be authored by a multitude of different people from around the world and from diverse companies.  The common point of interest amongst all the contributors is their role on the job, that of a software architect.  As the title suggests, there are in fact 97 different essays written by many practicing software professionals that were willing to share their experience, wisdom, proven techniques, mistakes, lessons learned, etc., about their profession and role of that of a software architect.</p>
<p>But what exactly is a software architect?  Though there isn’t a specific definition for that title, there are many accepted ones.  According to Wikipedia (<a href="http://en.wikipedia.org/wiki/Chief_Software_Architect">source</a>),</p>
<blockquote><p>Despite the lack of an accepted overall definition, the role of software architect generally has certain common traits:</p>
<p><strong>Strategic thinking<br />
</strong>Architects address the technological aspects of business needs by considering <em>what</em> a given technology can contribute to the overall functions of the system, as distinct from <em>how</em> that technology will perform its own functions.  This encourages opportunities for re-use of the technology and ultimately contributes to the organization’s efficiency.</p>
<p><strong>System interactions<br />
</strong>Architects deal with the interactions of systems, whether between components written in different languages at different time and at different locations, or between components of the same software system that use the same coding language.</p>
<p><strong>Design<br />
</strong>The architect makes high-level design choices much more often that low-level choices.  In addition, the architect may sometimes dictate technical standards, including coding standards, tools, or platforms, so as to advance business goals rather than to place arbitrary restrictions on the choices of developers.</p>
<p><strong>Communication<br />
</strong>Architects also have to communicate effectively, not only to understand the business needs, but also to advance their own architectural vision.</p></blockquote>
<p>As there are many different traits pertaining to software architecture, there also many different roles a software architect can play, be it: Enterprise Architect, Application Architect, Infrastructure Architect, Security Architect, etc.  As the author acknowledges,</p>
<blockquote><p>“97 Things Every Software Architected Should Know” provides advice from software architects around the world on everything from how to avoid common pitfalls to how to build talented teams.  It’s a smorgasbord of advice from established software architects for other software architects or those who aspire to become software architects.</p></blockquote>
<p>Throughout the book, you will find many essays dealing with at least one of these traits and some specific to a role.  The essays aren’t categorized in any way, so each one you read can deal with something different than a previous one, which might be a good idea…or not.  I would’ve preferred if the essays were categorized by traits (or other subject) so that I can focus my reading on a group of them when I want or need to.  For example, if I want to read some essays pertaining to effective team building, I could focus on reading a subset of those essays.  As it stands, I have no much choice but to read each of them sequentially, which can become quite tedious as some essays can be lightly equivalent to another in terms of idea and interest.  Nevertheless, since the content of the essays are rich, insightful and resourceful, I don’t mind too much about the overall layout.  On a last note about the structure of the book, I was very pleased to see that each essay was presented on a two-page presentation (left page and right page).  In other words, if you turn a page, you get a new essay.  At the end of each essay, you also get a brief bio of the author who wrote it so that you can either stalk him…or Google him.  Finally, there aren’t only male authors who contributed in this project.  As a matter of fact, <a href="http://www.lohutok.net/">Allison Randal</a> and <a href="http://www.thoughtworks.ca/who-we-are/leadership-profiles/rebecca-parsons.html">Rebecca Parsons</a> (CTO of ThoughtWorks) also contributed their own insights.</p>
<p>Many of the co-authors that participated in this project were unknown to me, so reading this book actually allow me to plug into the world of some them through their personal websites or blogs.  Amongst the list of people who contributed to the book are <a href="http://erik.doernenburg.com/">Erik Doernenburg</a>, <a href="http://www.udidahan.com/">Udi Dahan</a>, <a href="http://niclasnilsson.se/">Niclas Nilsson</a>, <a href="http://www.nealford.com/">Neal Ford</a>, <a href="http://peripateticaxiom.blogspot.com/">Keith Braithwaite</a>, <a href="http://www.eaipatterns.com/ramblings.html">Gregor Hohpe</a>, and many, many more.  Funny thing is that two articles that I very much enjoyed  much reading were written by a guy I’ve never heard of before: <a href="http://timhigh.wordpress.com/">Timothy High</a>, a software architect working for Sakonnet Technologies.  In fact, he’s the author of two of my favorite essays in the book: the first one being <em><strong>Empower Developers</strong></em>, in which he urges managers and architects to essentially “<em>protect developers from nonessential parts of their job.  Too much paperwork and too many office chores add overhead and reduce their effectiveness.</em>” and making sure developers have the tools, the skills and the trust necessary to get the job done; and the second one being <em><strong>Record Your Rationale</strong></em> where he suggests that architects should write down their rationale behind some of their technical decisions, as it may come in handy in a number of situations, as he writes:</p>
<ul>
<li><em>As a means of communication to developers regarding important architectural principles that should be followed</em></li>
<li><em>To get the team “on the same page,” or even head off a mutiny, when developers question the logic behind the architecture (or even to humbly accept criticism if it turns out a decision doesn’t hold up under scrutiny)</em></li>
<li><em>To show managers and stakeholders exactly why the software is being built the way it is (such as why an expensive piece of hardware or software is necessary)</em></li>
<li><em>When handing off the project to a new architect (how many time have you inherited a piece of software and wondered exactly why the designers did it THAT way?)</em></li>
</ul>
<p>Another interesting thing about this book is that O’Reilly agreed that all of the <a href="http://oreilly.com/catalog/9780596522698/preview.html">content of the essays</a> should be freely available (<a href="http://en.wikipedia.org/wiki/Creative_Commons_licenses">Creative Commons</a>) for to anyone through the book’s website.  Excellent idea, O’Reilly!</p>
<p>In essence, this book is all about software architecture, but not <em>only</em> for software architecture, but rather anyone involved in the software development lifecycle (testers, developers, QA, analysts) and even up the corporate ladder (CIO or CTO).  The content being technology-agnostic, there’s no doubt that the audience for this book will be heterogeneous, so whether you’re a .NET, Java or Ruby fan, you will surely find the essays valuable.  And just like the <a href="http://headfirstlabs.com/" target="_blank">Head First</a> series, this book has sprung another series focused on a “97 Things Every [fill in the blank] Should Know” idea.  [Note from my wife, the environmentalist: “There goes more trees!”].  If you’re a developer aspiring to take on more responsibilities and challenges for a software project, I would highly recommend you get a copy of this book.  Furthermore, I would also recommend reading a good complement for this book: “<a href="http://download.microsoft.com/download/3/d/7/3d718c6c-e5f3-41e3-b346-6e9f114e2531/journal15_english.pdf.zip">The Role of an Architect</a>” published by <a href="http://msdn.microsoft.com/en-us/architecture/bb410935.aspx">The Architectural Journal</a><em></em> (Microsoft).</p>
<p><strong>[UPDATE: 2008-04-15]</strong> You can also read the <a href="http://97-things.near-time.net/wiki/other-things-software-architects-should-know">other proposals which didn&#8217;t make it to the final cut</a>.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/04/12/book-review-10-97-things-every-software-architect-should-know/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Published my first Windows Live Writer plugin: Amazon Book Linker v1.0</title>
		<link>http://blog.briandicroce.com/2009/04/06/published-my-first-windows-live-writer-plugin-amazon-book-linker-v10/</link>
		<comments>http://blog.briandicroce.com/2009/04/06/published-my-first-windows-live-writer-plugin-amazon-book-linker-v10/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 14:58:49 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[amazon book linker]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/04/06/published-my-first-windows-live-writer-plugin-amazon-book-linker-v10/</guid>
		<description><![CDATA[Well, after using Windows Live Writer as my default blogging platform for quite some time, I decided to play around with the provided API to build a little plugin which will speed up the process of finding and linking books from Amazon directly to a blog post.
Amazon Book Linker v1.0 is a free plugin for [...]]]></description>
			<content:encoded><![CDATA[<p>Well, after using <a href="http://download.live.com/writer">Windows Live Writer</a> as my default blogging platform for quite some time, I decided to play around with the provided API to build a little plugin which will speed up the process of finding and linking books from Amazon directly to a blog post.</p>
<p><a href="http://gallery.live.com/liveItemDetail.aspx?li=0aeb208a-81d1-4197-bbce-b4a42f5ec430&amp;pl=8&amp;bt=9">Amazon Book Linker v1.0</a> is a free plugin for Windows Live Writer which provides many innovative functionalities for searching and linking books from Amazon on your blog post.&#160; It allows you to specify your own Amazon Associate Tag to every link (you only have to provide this information once, and the plugin will remember it next time) so that you can make a sales commission out of a direct sale from one of the links.&#160; Furthermore, it empowers the HTML <em>ninja</em> (or <em>ninjette</em>) to also customize the HTML template to better match the link layout to your own design.&#160; In order to assist you with this process, the plugin also lists you a series of predefined macros to use.&#160; You can also choose your preferred Amazon locale (US, CA, CO.UK, JP, FR or DE) too!</p>
<p>For more information, I’ll invite you to check the brand new <a href="http://blog.briandicroce.com/projects/"><strong>Projects</strong></a> page on this site.&#160; Meanwhile, here are a few screenshots.</p>
<p><img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="648" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image3.png" width="516" border="0" /> </p>
<p><img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="265" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/04/image4.png" width="477" border="0" /> </p>
<p>Now that this little project is out the door, I can resume posting new ideas on our beloved craft and technologies. <img src='http://blog.briandicroce.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/04/06/published-my-first-windows-live-writer-plugin-amazon-book-linker-v10/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Dear VB Developer&#8230;</title>
		<link>http://blog.briandicroce.com/2009/03/10/dear-vb-developer/</link>
		<comments>http://blog.briandicroce.com/2009/03/10/dear-vb-developer/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 03:15:52 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/03/10/dear-vb-developer/</guid>
		<description><![CDATA[Whenever someone coming from a VB6 background asks me which language they should adopt to learn .NET, I recommend them C#.  I know that, intuitively, I should tell them to embark the journey with Visual Basic.NET (VB.NET) since that language should already be familiar to them.  However, the familiarity of the language is mostly a [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever someone coming from a VB6 background asks me which language they should adopt to learn .NET, I recommend them C#.  I know that, intuitively, I should tell them to embark the journey with Visual Basic.NET (VB.NET) since that language should already be familiar to them.  However, the familiarity of the language is mostly a costume on its own, since underneath it lives a totally different beast.  The VB.NET compiler now speaks a very different language than its VB6 cousin did: <strong>the language of the object-oriented paradigm</strong>.  Though the first release version of Visual Basic for the .NET platform was an increment of VB6’s, Visual Basic v7.0, I consider it to be more of a v1.0, because it’s much more than a major revision.  Yes, the language is very much the same, but not the compiler.  According to Microsoft,</p>
<blockquote><p>“<em>Visual Basic .NET is not Visual Basic 6 with inheritance tacked onto it.  Rather, Visual Basic .NET has been <strong>entirely rewritten</strong> to be fully object-oriented.”</em> <em>For a programming language to be a true OOP language, the language must meet the following criteria:</em></p>
<ul>
<li><em><strong>Abstraction</strong>: Abstraction manages the complexities of a business problem by allowing you to identify a set of objects involved with that business problem.</em></li>
<li><em><strong>Encapsulation</strong>: Encapsulation hides the internal implementation of an abstraction within the particular object.</em></li>
<li><em><strong>Polymorphism</strong>: Polymorphism provides for multiple implementations of the same method.  For example, different objects can have a Save method, each of which perform different processing.</em></li>
<li><em><strong>Inheritance</strong>: The excitement of Visual Basic .NET lies in inheritance.  Visual Basic 5 introduced the concept of interface inheritance, which allows you to reuse the interface of a class, but not its implementation.  Visual Basic .NET provides for true implementation inheritance whereby you can reuse the implementation of a class. </em>[Source: <a href="http://msdn.microsoft.com/en-us/library/aa289512.aspx">MSDN</a>]</li>
</ul>
</blockquote>
<p>Chances are that a VB6 veteran, that has been programming with VB since its earliest versions, will tend to write VB.NET code in a more traditional “procedural” way, instead of an “object-oriented” way. This is sad because .NET itself is not only built around, but also provides a rich object-oriented model for programming.  However, that doesn’t rattle my cage too much, because with enough practice and mentorship, they <em>could</em> learn to embrace the OO way of analysis, design and programming, as well as to appreciate its <a href="http://www.rtis.com/nat/user/jfullerton/BENEFIT.HTM">benefits</a>.  What rather tends to rattle my cage is that I said <em>could</em> (in <em>italic</em> in case you haven’t noticed) because there’s not much out there that benefit Visual Basic (VB6 and VB.NET) programmers to really appreciate the OO paradigm to its fullest.</p>
<p>C++ and Java programmers coming to .NET via C# tend to reap more of the benefits from the platform than VB6 developers do when jumping on the .NET wagon with VB.NET, because they are already minded to the OO way of doing things.  I think the main reason that makes this balance shift more on one side than the other is because there is just SO MUCH MORE resources (open-source projects, books, blog posts, articles, training, etc.) that focus on the C-family languages, such as C++, JavaScript and C# (for the sake of this post, I will also include Java in this family).  OO concepts such as inheritance, encapsulation, polymorphism, and constructs such as interfaces or abstract classes, tends to be very hard subjects to grasp for VB6 and VB.NET developers (on a professional level, I can only support this fact through those same comments I get from VB developers taking my VB.NET courses and from fellow developers at user groups).   The VB development community will be in a much better position to take advantage of the OO model, the same way that C++, Java and C# developers did, if for every OO book, article or blog posts, there would be the equivalent counterpart code samples in VB.  Last time I checked on Amazon, there wasn’t many books focused on <a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)">design patterns</a> for Visual Basic developers.  However, there is a ton available for C++, Java, C# and even Ruby developers.  It’s hard for VB developers to grasp the essence of design patterns if they struggle to understand the concept of inheritance and polymorphism.</p>
<p>You know what?  Let’s dig a little deeper on the programming books case.  Table 1 shows the total number of books found in Amazon for Visual Basic, C#, C++ and Java:</p>
<p style="text-align: center;"><strong>Table 1</strong>. Books results for various programming languages at Amazon.com (search made on March 10th, 2009)</p>
<div>
<table border="1" cellspacing="0" cellpadding="2" width="600" align="center">
<tbody>
<tr>
<td width="150" valign="top">
<p style="text-align: center;">Visual Basic</p>
</td>
<td width="149" valign="top">
<p style="text-align: center;">C#</p>
</td>
<td width="149" valign="top">
<p style="text-align: center;">C++</p>
</td>
<td width="150" valign="top">
<p style="text-align: center;">Java (not JavaScript)</p>
</td>
</tr>
<tr>
<td width="150" valign="top">
<p style="text-align: center;"><strong>12,788 </strong>books</p>
</td>
<td width="149" valign="top">
<p style="text-align: center;"><strong>16,527 </strong>books</p>
</td>
<td width="149" valign="top">
<p style="text-align: center;"><strong>22,248</strong> books</p>
</td>
<td width="151" valign="top">
<p style="text-align: center;"><strong>64,158</strong> books</p>
</td>
</tr>
</tbody>
</table>
</div>
<p><br/>&nbsp;<br />
This is very shocking, because if my memory serves me well, Visual Basic is much older than C# and Java! I’m going to spare you the proportions for each of these books, because I think the numbers speak for themselves.  However, I do want you to notice something important: if we consider C# and Java being almost equivalent (putting the CLR and JVM aside), that means that VB developers have close to <strong>68,000 </strong>books less to read from.  There’s a little “gotcha” in this study though: just for the programming related books for all these languages, <strong>Java</strong> comes first with <strong>12,241</strong> books, <strong>C++</strong> comes at second place with <strong>10,465</strong> books, <strong>Visual Basic</strong> comes at third position with <strong>6,351</strong> books and <strong>C#</strong> comes last with <strong>3,884</strong> books.  However, most of the VB books are for VB6 and older versions (and maybe VBA or VBScript).  Joking aside, <strong>there’s not even a Head First series on Visual Basic!!!</strong> There is though a <a href="http://www.amazon.com/exec/obidos/ASIN/0596514824/brdicr-20">Head First C#</a>, <a href="http://www.amazon.com/exec/obidos/ASIN/0596527748/brdicr-20">Head First JavaScript</a>, <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/brdicr-20">Head First Design Patterns</a> (Java), <a href="http://www.amazon.com/exec/obidos/ASIN/0596008678/brdicr-20">Head First Object-Oriented Analysis and Design</a> (Java), even a <a href="http://www.amazon.com/exec/obidos/ASIN/0596515774/brdicr-20">Head First Rails and Head First SQL</a>!  Here’s my point: some of the most influential books on software development and programming such as <a href="http://www.amazon.com/exec/obidos/ASIN/0201633612/brdicr-20">Design Patterns</a> (by the GoF), <a href="http://www.amazon.com/exec/obidos/ASIN/0321127420/brdicr-20">Patterns of Enterprise Application Architecture</a> (Fowler), <a href="http://www.amazon.com/exec/obidos/ASIN/0321213351/brdicr-20">Refactoring to Patterns</a> (Keriesky), <a href="http://www.amazon.com/exec/obidos/ASIN/0321413091/brdicr-20">Implementation Patterns</a> (Beck), <a href="http://www.amazon.com/exec/obidos/ASIN/0321125215/brdicr-20">Domain-Driven Design</a> (Evans), even hardcore .NET books like <a href="http://www.amazon.com/exec/obidos/ASIN/0735621632/brdicr-20">CLR via C#</a> (Richter) and the <a href="http://www.amazon.com/exec/obidos/ASIN/0321245660/brdicr-20">Effective C#</a> series (Wagner), without forgetting the books from the Robert C. Martin series (<a href="http://www.amazon.com/exec/obidos/ASIN/0131857258/brdicr-20">Agile Principles, Patterns, and Practices</a>; <a href="http://www.amazon.com/exec/obidos/ASIN/0132350882/brdicr-20">Clean Code</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/0131177052/brdicr-20">Working Effectively with Legacy Code</a>) hold a vast, deep and rich knowledge of the craft of software development that VB/VB.NET developers are mostly ignorant of.  No wonder that I also tend to see more jobs offering for C# developers than VB.NET when it comes to .NET positions.  I don’t know about you, but that’s very unfortunate.  However, this paragraph was mostly about books.  What about tools and open-source projects?</p>
<p>When I teach the intermediate and advanced VB.NET courses, I tell the students that they should take a look at <a href="http://www.codeplex.com/entlib">Enterprise Library</a> from the <a href="http://msdn.microsoft.com/en-us/practices/default.aspx">Patterns and Practices Group</a> at Microsoft (<a href="http://www.microsoft.com/video/en/us/details/84f1b261-7e30-4a05-83fd-6025b446e815">watch a video of it here</a>) for it can save them time, cost and effort in common development situations.  I even tell them that it is open source, which means they have access to the code.  However, I don’t get much excitement from their part, mostly because the code is written in C#.  Code reading aside, I haven’t even heard of one VB developer simply using Enterprise Library in his projects.  The same goes for projects like nHibernate, the Castle project, NUnit, even mocking tools like Rhino.Mocks and Moq.  That’s very sad, because by not using some of these tools, VB developers tend to stay far behind common practices such as TDD and technologies like ORM tools.  What doesn’t help also, is that most of the screencasts where you see someone showing a piece of code or an utility, such as TDD with NUnit and Rhino.Mocks for example, the language is more often than not…wait for it…C#.  The only screencasts that I see a couple of developers programming in VB is in <a href="http://www.dnrtv.com">DNRtv</a>.  Furthermore, I don’t recall ever seeing a VB example from the guys at <a href="http://www.codebetter.com">CodeBetter</a>.  Worse, I don’t even have one example written in VB in any of my posts!</p>
<p>One H U G E advantage that VB6 (or even VB.NET) developers tend to have when learning C# to program with .NET is that they can also be more comfortable in reading C# and Java books and source code (which the latter being as most important as reading books IMHO).  By doing this, for example, a VB developer having trouble understanding the implementation of a design pattern in VB, can easily learn it from another example written in C# or Java (and perhaps C++ if the VB developer understand the notion of pointers).  The last remark I said in parenthesis is important, because of all the .NET constructs, it seems to me that the one that causes much headaches to VB/VB.NET developers are delegates, which are essentially lists of method references.  It’s one of those concepts that I tend to spend more time on explaining to VB developers jumping to VB.NET, than C++ or Java developers learning C#.</p>
<p>Though there isn’t a simple and easy solution for VB developers in this situation, I can only encourage them to take a look at C#.  By doing that, they will also be more comfortable in exploring the world of Java, and perhaps even play around the JVM and see how it compares to .NET.  As Benjamin Franklin once said,</p>
<blockquote><p>“An investment in knowledge always pays the best interest”, and</p>
<p>“The only thing more expensive than education is ignorance”.</p></blockquote>
<p>In his article, “<a href="http://visualstudiomagazine.com/columns/article.aspx?editorialsid=2893"><strong>What VB Dev Should Know About C#</strong></a>”, Bill Wagner actually encourages VB developers to learn about C# (and C# developers to learn about VB) by saying that,</p>
<blockquote><p>&#8220;<em>Our discussion also covered the need for developers to get out of their comfort zones and learn something new.  Yes, the two languages are similar in features, but there are idiomatic differences between the languages, and some tasks are significantly easier in one language than the other.  In fact, you’ll find yourself much more skilled and much more in demand if you know the best features in both languages, as well as how to build applications that rely on both languages.”</em></p></blockquote>
<p>And if what I’m writing about isn’t encouraging you enough to learn a new language, then <a href="http://www.youtube.com/watch?v=kdxP85ouT9c"><strong>maybe President Barack Obama can persuade you to learn more than “one language”</strong></a>.</p>
<p>I personally speak three languages (Spanish, French and English) and I’m very grateful that I can speak and write in those languages as it gives me a lot of opportunity to learn and embrace foreign cultures, philosophies, relationships and literature.  On the more technical side, I’m also fluent in C++ (not as much as the other languages though), PHP, Java, C# and Visual Basic.  The same benefits apply here, as I can read code written in those languages and books using examples also written in any of those languages.   I can also more easily conduct code reviews with other developers as I’m in a better position to understand their intent, disregard the programming language that was used.</p>
<p>So next time you’re thinking about starting a new personal project, see to it that you write it in C# (if you’re targeting the .NET framework), or even in Java for a change.  If you struggle, don’t quit.  There are <a href="http://www.developerfusion.com/tools/convert/csharp-to-vb/">many</a> <a href="http://www.kamalpatel.net/ConvertCSharp2VB.aspx">online</a> <a href="http://www.carlosag.net/Tools/CodeTranslator/">language</a> <a href="http://tangiblesoftwaresolutions.com/">conversion</a> <a href="http://authors.aspalliance.com/sswafford/article625/CheatSheet.pdf">tools</a> out there to translate C# code to VB and vice-versa.  <a href="http://www.pragprog.com/">The Pragmatic Programmers</a> once advised developers that they should learn at least one new language every year.  This year, Dear VB developer, make a commitment to learn a new language (I’d recommend C#) as it will make you more valuable and employable, and also more comfortable in learning about new ideas emerging in the world of software development.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/03/10/dear-vb-developer/feed/</wfw:commentRss>
		<slash:comments>38</slash:comments>
		</item>
		<item>
		<title>Would Your Objects Praise You?</title>
		<link>http://blog.briandicroce.com/2009/03/04/would-your-objects-praise-you/</link>
		<comments>http://blog.briandicroce.com/2009/03/04/would-your-objects-praise-you/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 03:51:25 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/03/04/would-your-objects-praise-you/</guid>
		<description><![CDATA[In the book of Psalms, we can read a passage where King David thanks and praises the Lord for making the human body and soul a wonderful creation:
Oh yes, you shaped me first inside, then out;      you formed me in my mother’s womb.       I [...]]]></description>
			<content:encoded><![CDATA[<p>In the book of Psalms, we can read a passage where <a href="http://en.wikipedia.org/wiki/David">King David</a> thanks and praises the Lord for making the human body and soul a wonderful creation:</p>
<blockquote><p>Oh yes, you shaped me first inside, then out;      <br />you formed me in my mother’s womb.       <br />I thank you, High God &#8211; you’re breathtaking!       <br />Body and soul, I am marvelously made!       <br />I worship in adoration &#8211; what a creation!       </p>
<p><strong>You know me inside and out,        <br />you know every bone in my body;         <br />You know exactly how I was made, bit by bit,         <br />how I was sculpted from nothing into something.         <br />Like an open book, you watched me grow from conception to birth</strong>;       <br />all the stages of my life were spread out before you,       <br />The days of my life all prepared       <br />before I’d even lived one day.</p>
<p><a href="http://www.biblegateway.com/passage/?search=psalms%20139:13-16;&amp;version=65;">Psalm 139:13-16</a> (The Message)</p>
</blockquote>
<p>Though I am not perfect, I am perfectly made.&#160; I did not ask for either a working set of eyes or a complex and functional brain.&#160; Never did it occur to me that my heart would be responsible for pumping my blood or that my lungs would filter the oxygen from the air that I breathe.&#160; I am not a creator of life, but I do create a virtual kind of life, where instead of blood and oxygen there is a complex symbiosis of binary mathematics and electrical pulses.&#160; As a software developer, I do bring life to objects that are responsible for the inner workings of my system.&#160; <strong>I sometimes wonder whether the objects and components that we build for our software would thank and praise us in the same degree that King David praised his Creator.</strong></p>
<p>Lately, there has been a popular trend going on in blogs and podcasts around object-oriented programming, notably with the <a href="http://blog.objectmentor.com/articles/2009/02/12/getting-a-solid-start">SOLID principles</a>, a composite of acronyms about key design principles introduced by <a href="http://en.wikipedia.org/wiki/Robert_Cecil_Martin">Robert C. Martin</a>.&#160; One of the principles that I adhere stronger than the others is the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP).&#160; According to Wikipedia’s definition,</p>
<blockquote><p>The <strong>single responsibility principle</strong> states that every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility.&#160; [<a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Wikipedia</a>].</p>
</blockquote>
<p>In the words of Uncle Bob, <strong>aside from a type having a well defined responsibility, it should have one, and only one, reason to change</strong>.&#160; Going to back to the human body, we can clearly see that our inner components are highly cohesive and loosely coupled.&#160; For example, when I pick up a broken piece of glass from my kitchen’s floor and suddenly I cut myself on the finger of my right hand, I shouldn’t expect my right eye to go blind.&#160; Nor do I expect the other fingers to stop working!&#160; Of course not!&#160; That’s because I am wonderfully made!&#160; And so are you!&#160; When I cut myself, my body knows how to heal itself and within days I have no scar.&#160; Leaving the “cutting” aside, we can clearly see how the eyes, the nose, the tongue, the teeth, the nails, the hair, the skin, etc., all have a single and specific responsibility, and if one member should stop working, it can be replaced by another compatible member.&#160; For example, we can do heart transplants, lungs transplants, etc.&#160; Given that the human body is so much more complex than any software ever created, how come that when my printer driver stops working, the whole operating system goes down with it?&#160; How come when a component in an application stops working, it affects other components as well?&#160; Take, for example, a tree: it looks very simple on the outside, but inside of it, God has clearly encapsulated the inner workings of the tree.&#160; A tree that’s been around for decades can still grow and participate in photosynthesis.&#160; It’s incredible.&#160; Software systems tend to be replaced every year or two.&#160; Anyhow, I want to keep writing on the topic of single responsibility for an object.</p>
<p>Suppose you have the following simple <strong>Car</strong> class:</p>
<p><img title="" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="215" alt="" src="http://blog.briandicroce.com/wp-content/uploads/2009/03/image.png" width="187" border="0" /></p>
<p>Now, what do you think about the methods (or responsibilities) of this class?&#160; Does it violate the SRP principle?&#160; Here’s a simple test you can do to answer these questions (if you answer ‘NO’ to any of these questions, then you know that your class doesn’t adhere to the SRP principle.&#160; Even better: if the question doesn’t seem to make any sense to you, then it’s a ‘NO’ by default):</p>
<ul>
<li>Can a <strong>car</strong> <u>change gears</u> by itself? </li>
<li>Can a <strong>car</strong> <u>change a radio station</u> by itself? </li>
<li>Can a <strong>car</strong> <u>drive</u> itself? </li>
<li>Can a <strong>car</strong> <u>idle</u> by itself? </li>
<li>Can a <strong>car</strong> <u>park</u> itself? </li>
<li>Can a <strong>car</strong> <u>start</u> itself? </li>
</ul>
<p><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 0px 10px; border-right-width: 0px" height="174" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/03/image1.png" width="244" align="right" border="0" /> Unless you’re the lucky owner of the <a href="http://en.wikipedia.org/wiki/Knight_Rider_(1982_TV_series)">Knight Rider</a>, you probably answered ‘NO’ to a few of these questions.&#160; For every question or observation you have answered ‘NO’, you should more than likely refactor the class and move some of its responsibilities elsewhere.&#160; But where exactly can you move these responsibilities?&#160; Well, before we dive to some of these answers, let’s see if we can directly some of the original questions pertaining to our Car class:</p>
<ul>
<li>Can a <strong>car</strong> <u>change gears</u> by itself? <strong><u>ANSWER:</u></strong> Sure, if it has an automatic transmission.&#160; Otherwise, only the <strong>driver</strong> can shift gears. </li>
<li>Can a <strong>car</strong> <u>change a radio station</u> by itself?&#160; <strong><u>ANSWER:</u></strong> Nope, only the <strong>driver</strong> (or the passenger) can change the radio station. </li>
<li>Can a <strong>car</strong> <u>drive</u> itself? <strong><u>ANSWER:</u></strong> Not unless it’s KITT!&#160; Only a <strong>driver</strong> can drive a car. </li>
<li>Can a <strong>car</strong> <u>idle</u> by itself? <strong><u>ANSWER:</u></strong> Sure it can. </li>
<li>Can a <strong>car</strong> <u>park</u> itself? <strong><u>ANSWER:</u></strong> Nope! Only a <strong>driver</strong> can know how to park a car. </li>
<li>Can a <strong>car</strong> <u>start</u> itself? <strong><u>ANSWER:</u></strong> Uh, no.&#160; Once again, only the <strong>driver</strong> can start the car. </li>
</ul>
<p>Do you see how by answering such questions or observations you can better get through a cleaner design?&#160; The pattern is pretty simple too: <strong>Can a [<em>type</em>] [<em>action in the infinitive</em>] itself?</strong> Though I picked a very trivial example, you could do the same test for any of your classes, e.g., an Order class, an Employee class, an Account class, a Repository&lt;T&gt; class, even interfaces!&#160; Continuing with our beloved <strong>Car</strong> class, we see that it has a couple of responsibilities it cannot fulfill by itself, because either it doesn’t make sense that it should, or plainly that it isn’t logical for the class to offer those services in the first place.&#160; From our answers, we can also see that we need another class to make all well orchestrated: a <strong>Driver</strong> class.&#160; The driver should know how to fulfill some of these responsibilities.&#160; How would you associate the <strong>Driver</strong> to a <strong>Car</strong>?&#160; You can go with the Dependency Injection pattern on this one if you like, or pick another strategy. </p>
<p><img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="294" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/03/image2.png" width="517" border="0" /></p>
<p>So we talked about a few things here: the Bible, Knight Rider and Uncle Bob.&#160; But more importantly in this post, we talked about some easy and simple techniques we can use to determine whether methods or properties (responsibilities) actually belong in the right type.&#160; Unfortunately, following this technique isn’t a silver bullet for every situation!&#160; You must still rely on your experience and wisdom to better answer that question.&#160; If you are part of a team, that’s even better because it is those deep and insightful conversations that actually bring about a much suitable solution.&#160; For example, what if our <strong>Car </strong>needs some reparations?&#160; Should we create a <strong>Repair()</strong> method in the <strong>Car</strong> class or the <strong>Driver</strong> class?&#160; Well, you can first of all use our little technique to answer this question:</p>
<ul>
<li>Should a <strong>car</strong> be able to <u>repair</u> itself? <strong><u>ANSWER:</u></strong> Not in this world, that’s for sure! </li>
<li>Should a <strong>driver</strong> be able to <u>repair</u> a car? <strong><u>ANSWER:</u></strong> Well, it depends!&#160; Maybe the driver can repair the car, and if not, maybe we should introduce a <strong>Mechanic</strong> class that knows how to repair the car.&#160; This is when we should discuss it in front of a whiteboard. </li>
</ul>
<p>I hope this little tip will help you to better evaluate your design.&#160; And don’t stress too much about it if your design doesn’t follow the SRP or any of the SOLID principles.&#160; Just be thankful that you’re wonderfully made and that you have Google at your disposal.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/03/04/would-your-objects-praise-you/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Avoid calling a virtual or abstract method from a constructor in C#&#8230;especially in VB!</title>
		<link>http://blog.briandicroce.com/2009/02/22/avoid-calling-a-virtual-or-abstract-method-from-a-constructor-in-cespecially-in-vb/</link>
		<comments>http://blog.briandicroce.com/2009/02/22/avoid-calling-a-virtual-or-abstract-method-from-a-constructor-in-cespecially-in-vb/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 05:16:53 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/?p=513</guid>
		<description><![CDATA[Whenever I’m teaching our intermediate .NET development course, I mention to my students the pitfalls of calling a virtual method from a base class’ constructor.&#160; The major drawback of calling a virtual method in such a context is that if a derived class overrides that method, the overridden method will be invoked prior to the [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever I’m teaching our <a href="http://www.crim.ca/fr/services/Formation/Cours-inscription/index.html?uri=/ressources/fr/Services/formation/Cours-inscription/Application/point_net.html&amp;id=NET282">intermediate .NET development course</a>, I mention to my students the pitfalls of calling a virtual method from a base class’ constructor.&#160; The major drawback of calling a virtual method in such a context is that if a derived class overrides that method, the overridden method will be invoked prior to the derived class’ instance constructor.&#160; This is especially true in C#, and as you’ll see later in this post, it’s even more dangerous to do so in Visual Basic. </p>
<h4>The Marker class</h4>
<p>For this demonstration, I will be using a custom <strong>Marker</strong> class that will simply output text in the console.</p>
<p>This is the Marker class in C# (the class is equivalent in the Visual Basic module):</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">class</span> Marker
{
    <span style="color: #0000ff">public</span> Marker(<span style="color: #0000ff">string</span> text)
    {
        Console.WriteLine(text);
    }
}</pre>
</div>
<h4>The Base class</h4>
<p>Let’s kick off the idea with a single C# class (no inheritance whatsoever):</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">class</span> Base
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Marker staticField = <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base static field&quot;</span>);
    <span style="color: #0000ff">private</span> Marker instanceField = <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base instance field&quot;</span>);

    <span style="color: #0000ff">static</span> Base()
    {
        <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base static ctor&quot;</span>);
    }

    <span style="color: #0000ff">public</span> Base()
    {
        M();
        <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base instance ctor&quot;</span>);
    }

    <span style="color: #0000ff">protected</span>  <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> M()
    {
        Console.WriteLine( <span style="color: #006080">&quot;base M() was called&quot;</span> );
    }
}</pre>
</div>
<p>Here’s the same class in Visual Basic:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">Class</span> Base

    <span style="color: #0000ff">Private</span> <span style="color: #0000ff">Shared</span> staticField <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base static field&quot;</span>)
    <span style="color: #0000ff">Private</span> instanceField <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base instance field&quot;</span>)

    <span style="color: #0000ff">Shared</span> <span style="color: #0000ff">Sub</span> <span style="color: #0000ff">New</span>()
        <span style="color: #0000ff">Dim</span> temp <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base static ctor&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

    <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Sub</span> <span style="color: #0000ff">New</span>()
        M()
        <span style="color: #0000ff">Dim</span> temp <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base instance ctor&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

    <span style="color: #0000ff">Protected</span> <span style="color: #0000ff">Overridable</span> <span style="color: #0000ff">Sub</span> M()
        Console.WriteLine(<span style="color: #006080">&quot;base M() was called&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

<span style="color: #0000ff">End</span> Class</pre>
</div>
<p>When we instantiate the <strong>Base</strong> class, we get the following output in C# and Visual Basic.&#160; <strong>The main point is that the results are the same whether we instantiate the C# or Visual Basic version of the Base class</strong>.</p>
<div align="center">
<table cellspacing="0" cellpadding="2" width="700" border="0">
<tbody>
<tr>
<td valign="top" width="350">
<p align="left"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="119" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image7.png" width="325" border="0" /></p>
</td>
<td valign="top" width="350">
<p align="left"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="119" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image8.png" width="328" border="0" /></p>
</td>
</tr>
</tbody>
</table>
</div>
<p>&#160;</p>
<h4>Instantiating a child class of Base in C# and Visual Basic</h4>
<p>Here’s now the entire code in C# of the Base class (same as before) and a Derived class inheriting from it:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">class</span> Base
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Marker staticField = <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base static field&quot;</span>);
    <span style="color: #0000ff">private</span> Marker instanceField = <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base instance field&quot;</span>);

    <span style="color: #0000ff">static</span> Base()
    {
        <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base static ctor&quot;</span>);
    }

    <span style="color: #0000ff">public</span> Base()
    {
        M();
        <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;base instance ctor&quot;</span>);
    }

    <span style="color: #0000ff">protected</span>  <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> M()
    {
        Console.WriteLine( <span style="color: #006080">&quot;base M() was called&quot;</span> );
    }
}

<span style="color: #0000ff">class</span> Derived : Base
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Marker staticField = <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;derived static field&quot;</span>);
    <span style="color: #0000ff">private</span> Marker instanceField = <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;derived instance field&quot;</span>);

    <span style="color: #0000ff">static</span> Derived()
    {
        <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;derived static ctor&quot;</span>);
    }

    <span style="color: #0000ff">public</span> Derived()
    {
        <span style="color: #0000ff">new</span> Marker(<span style="color: #006080">&quot;derived instance ctor&quot;</span>);
    }

    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> M()
    {
        Console.WriteLine( <span style="color: #006080">&quot;derived M() was called&quot;</span> );
    }
}</pre>
</div>
<p>And here are the same classes in Visual Basic:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">Class</span> Base

    <span style="color: #0000ff">Private</span> <span style="color: #0000ff">Shared</span> staticField <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base static field&quot;</span>)
    <span style="color: #0000ff">Private</span> instanceField <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base instance field&quot;</span>)

    <span style="color: #0000ff">Shared</span> <span style="color: #0000ff">Sub</span> <span style="color: #0000ff">New</span>()
        <span style="color: #0000ff">Dim</span> temp <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base static ctor&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

    <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Sub</span> <span style="color: #0000ff">New</span>()
        M()
        <span style="color: #0000ff">Dim</span> temp <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;base instance ctor&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

    <span style="color: #0000ff">Protected</span> <span style="color: #0000ff">Overridable</span> <span style="color: #0000ff">Sub</span> M()
        Console.WriteLine(<span style="color: #006080">&quot;base M() was called&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

<span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>

<span style="color: #0000ff">Class</span> Derived : <span style="color: #0000ff">Inherits</span> Base

    <span style="color: #0000ff">Private</span> <span style="color: #0000ff">Shared</span> staticField <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;derived static field&quot;</span>)
    <span style="color: #0000ff">Private</span> instanceField <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;derived instance field&quot;</span>)

    <span style="color: #0000ff">Shared</span> <span style="color: #0000ff">Sub</span> <span style="color: #0000ff">New</span>()
        <span style="color: #0000ff">Dim</span> temp <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;derived static ctor&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

    <span style="color: #0000ff">Sub</span> <span style="color: #0000ff">New</span>()
        <span style="color: #0000ff">Dim</span> temp <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> Marker(<span style="color: #006080">&quot;derived instance ctor&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

    <span style="color: #0000ff">Protected</span> <span style="color: #0000ff">Overrides</span> <span style="color: #0000ff">Sub</span> M()
        Console.WriteLine(<span style="color: #006080">&quot;derived M() was called&quot;</span>)
    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span>

<span style="color: #0000ff">End</span> Class</pre>
</div>
<p>Here’s the output generated when instantiating the Derived class in C# and Visual Basic (<strong>This is where the fun begins!&#160; Try to notice any difference between the C# and Visual Basic execution before continuing.</strong>) :</p>
<div align="center">
<table cellspacing="0" cellpadding="2" width="702" border="0">
<tbody>
<tr>
<td valign="top" width="350"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image9.png" width="335" border="0" /> </p>
<p align="left">
</td>
<td valign="top" width="350"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="186" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image10.png" width="333" border="0" /> </p>
<p align="left">
</td>
</tr>
</tbody>
</table>
</div>
<p>As you can see, the order of execution in an inheritance relationship is both different in C# and Visual Basic (regardless of whether or not a virtual/abstract method was called from either constructor).&#160; <strong>The danger of calling a virtual or abstract method from a constructor (in C# or Visual Basic) is that the overridden method will be invoked prior to the derived class’ constructor</strong>.&#160; If that method tries to access or modify the state of the yet-to-be-fully-constructed object, you might end up with a corrupt or invalid instance.&#160; The worse scenario is in Visual Basic.&#160; As you can see from the results, the overridden method gets called not only before the derived class’ constructor (as it was with the C# version), but also gets called before the class’ instance fields get initialized.&#160; Now, that’s even more dangerous if the overridden method tries to access any of its instance fields because chances are that they will default to Nothing (null in C#) for reference types since they haven’t even been initialized yet!</p>
<h4>Referencing a Visual Basic assembly from a C# application and instantiating the Visual Basic version of the Derived type</h4>
<p>Here’s another potential dangerous scenario: From a C# application, you reference a Visual Basic assembly that holds the exact same code as the Base and Derived classes shown above.&#160; You then decide to instantiate the Visual Basic version of the Derived class:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)
{
    Console.WriteLine( <span style="color: #006080">&quot;From C# Program&quot;</span> );
    Console.WriteLine(  );
    <span style="color: #0000ff">new</span> VBModule.Derived();
}</pre>
</div>
<p>Here’s the output generated from instantiating the Visual Basic version of the Derived class in the C# application: </p>
<p>&#160;<img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="183" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image11.png" width="327" border="0" />As you can see, it is no longer having the same execution cycle as when instantiating the C# version of the Derived class from the C# code itself.</p>
<h4>Referencing a C# assembly from a Visual Basic application and instantiating the C# version of the Derived type</h4>
<p>This time, we’ll switch the roles and from a Visual Basic application, we’ll reference a C# assembly that holds the exact same code as the Base and Derived classes shown above.&#160; We then decide to instantiate the C# version of the Derived class:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">Sub</span> Main()
    Console.WriteLine(<span style="color: #006080">&quot;From VB Program&quot;</span>)
    Console.WriteLine()

    <span style="color: #0000ff">Dim</span> d <span style="color: #0000ff">As</span> <span style="color: #0000ff">New</span> CSharp.Derived
<span style="color: #0000ff">End</span> Sub</pre>
</div>
<p>Here’s the output generated from instantiating the C# version of the Derived class in the Visual Basic application:</p>
<p><img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="181" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image12.png" width="322" border="0" />As you can see, it is no longer having the same execution cycle as when instantiating the Visual Basic version of the Derived class from the Visual Basic code itself.</p>
<h4>Conclusion</h4>
<p>You should never be calling a virtual or abstract method from a constructor in C#, and even more in Visual Basic, because in either case a derived type will invoke an instance method prior to the object being fully constructed!&#160; In other words, the dynamic linking edition kicks in as soon as you instantiate a class.&#160; According to Microsoft,</p>
<blockquote>
<p>When a virtual method is called, the actual type that executes the method is not selected until run time.&#160; When a constructor calls a virtual method, it is possible that the constructor for the instance that invokes the method has not executed.&#160; <strong>The constructor should be redesigned to eliminate the call to the virtual method.</strong></p>
</blockquote>
<p>As you saw, the execution cycle is different for both languages even with the same codebase.&#160; It gets even more interesting when referencing either assembly (C# or Visual Basic) in either language, because you just wouldn’t know what you’ll get at the other end.&#160; Having a tool like <a href="http://www.jetbrains.com/resharper/">ReSharper</a> can be of a greater benefit in this circumstance as it’ll catch this kind of code and notify you about it.&#160; By default, this situation will be treated as a warning, but I’ll suggest you to treat it as an error as to make it stand out better (ReSharper-&gt;Options…-&gt;Inspection Severity): </p>
<p><img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="461" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image14.png" width="472" border="0" /></p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/02/22/avoid-calling-a-virtual-or-abstract-method-from-a-constructor-in-cespecially-in-vb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Fuhrer&#8217;s take on Scrum, Agile and CI</title>
		<link>http://blog.briandicroce.com/2009/02/19/the-fuhrers-take-on-scrum-agile-and-ci/</link>
		<comments>http://blog.briandicroce.com/2009/02/19/the-fuhrers-take-on-scrum-agile-and-ci/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 03:03:11 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Humor/Funny]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/02/19/the-fuhrers-take-on-scrum-agile-and-ci/</guid>
		<description><![CDATA[I hope you’re not working in this kind of organization.&#160; Period.



                        
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free [...]]]></description>
			<content:encoded><![CDATA[<p>I hope you’re not working in this kind of organization.&#160; Period.</p>
<div class="wlWriterEditableSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:b66d9cd0-2896-4ae9-8404-047262ae3ad9" style="padding-right: 0px; display: block; padding-left: 0px; float: none; padding-bottom: 0px; margin-left: auto; width: 425px; margin-right: auto; padding-top: 0px">
<div><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/Azl4nqLn4-Y&amp;hl=en"></param><embed src="http://www.youtube.com/v/Azl4nqLn4-Y&amp;hl=en" type="application/x-shockwave-flash" width="425" height="355"></embed></object></div>
</div>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/02/19/the-fuhrers-take-on-scrum-agile-and-ci/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The History of Microsoft on Channel9</title>
		<link>http://blog.briandicroce.com/2009/02/16/the-history-of-microsoft-on-channel9/</link>
		<comments>http://blog.briandicroce.com/2009/02/16/the-history-of-microsoft-on-channel9/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 01:54:38 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Media/Presentations]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/02/16/the-history-of-microsoft-on-channel9/</guid>
		<description><![CDATA[Recently, the folks over at Channel9 have put on a couple of episodes on the history of Microsoft since its inception.&#160; At the time of this writing, two episodes have been uploaded so far, but we can expect more to be released in the near future.&#160; Each episode is pretty interesting as it also introduces [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, the folks over at <a href="http://channel9.msdn.com">Channel9</a> have put on a couple of episodes on the <a href="http://channel9.msdn.com/shows/History"><strong>history of Microsoft</strong></a> since its inception.&#160; At the time of this writing, two episodes have been uploaded so far, but we can expect more to be released in the near future.&#160; Each episode is pretty interesting as it also introduces other things that were major at that specific time, such as the unveiling of the Shugart 5.25-inch floppy disk drive for $390.&#160; Ouch.&#160; Anyways, since I wasn’t born in the 1970s, it’s a nice thing to see what was going in the computing industry in that period.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/02/16/the-history-of-microsoft-on-channel9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TweetDeck not showing up in primary monitor</title>
		<link>http://blog.briandicroce.com/2009/02/11/tweetdeck-not-showing-up-in-primary-monitor/</link>
		<comments>http://blog.briandicroce.com/2009/02/11/tweetdeck-not-showing-up-in-primary-monitor/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 22:53:59 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[How To...]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/02/11/tweetdeck-not-showing-up-in-primary-monitor/</guid>
		<description><![CDATA[I’m currently using TweetDeck v0.21b as my default Tweeter client for my Windows Vista machine.&#160; Having a dual-monitor setup at home (laptop and an external monitor), I always move TweetDeck to the external monitor as a personal preference.&#160; While I’m waiting in the airport for my next flight, the sudden idea to tweet that specific [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently using <a href="http://www.tweetdeck.com/beta/">TweetDeck</a> v0.21b as my default <a href="http://www.tweeter.com">Tweeter</a> client for my Windows Vista machine.&#160; Having a dual-monitor setup at home (laptop and an external monitor), I always move TweetDeck to the external monitor as a personal preference.&#160; While I’m waiting in the airport for my next flight, the sudden idea to <em>tweet</em> that specific fact came to my mind.&#160; Unfortunately, TweetDeck’s UI doesn’t show up when I launch the application.&#160; I know it’s running because I can see the little black bird icon on the system tray, but it still thinks it’s suppose to show on my external monitor.&#160; That’s not good, because I don’t normally carry my external monitor with me when I’m on the go.&#160; Clearly this is a bug, but not one that will stop me from tweeting the fact that I’m waiting at the airport!&#160; </p>
<p>So these are the steps you should follow if your TweetDeck doesn’t show up in the primary (and only) monitor because it’s thinking it should display on an external monitor:</p>
<ol>
<li>If you’re using <strong>Windows Vista</strong>, browse to the following directory: C:\Users\&lt;username&gt;\AppData\Roaming.&#160; If you’re using <strong>Windows XP</strong>, browse to the following directory instead: C:\Documents and Settings\&lt;username&gt;\AppData.</li>
<li>Locate the folder that begins with <strong>TweetDeckFast</strong>.&#160; For example, the complete name of that folder on my PC is TweetDeckFast.F9107117265DB7542C1A806C8DB837742CE14C21.1.</li>
<li>Open the <strong>Local Store</strong> folder.</li>
<li>Edit the <strong>preferences_&lt;username&gt;.xml</strong> configuration file with your favorite text editor.</li>
<li>Change the <strong>windowState</strong> XML element’s <strong>x</strong> attribute to <strong>0</strong>.&#160; If you don’t see the TweetDeck UI, it is because the <strong>x</strong> attribute’s value is negative (which makes sense if the external monitor was on the left side).</li>
<li>Save your changes to that file and load TweetDeck.&#160; You should now be able to see the UI.</li>
</ol>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/02/11/tweetdeck-not-showing-up-in-primary-monitor/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Broken Windows Theory in Software</title>
		<link>http://blog.briandicroce.com/2009/02/08/broken-windows-theory-in-software/</link>
		<comments>http://blog.briandicroce.com/2009/02/08/broken-windows-theory-in-software/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 02:30:12 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2009/02/08/broken-windows-theory-in-software/</guid>
		<description><![CDATA[ I first heard of the Broken Windows theory while reading Malcom Gladwell’s The Tipping Point.&#160; In a nutshell, the theory goes like this:
If a window is broken and left unrepaired, people walking by will conclude that no one cares and no one is in charge.&#160; Soon, more windows will be broken, and the sense [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="240" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2009/02/image.png" width="158" align="right" border="0" /> I first heard of the Broken Windows theory while reading Malcom Gladwell’s <a href="http://www.amazon.com/exec/obidos/ASIN/0316346624/brdicr-20">The Tipping Point</a>.&#160; In a nutshell, the theory goes like this:</p>
<blockquote><p>If a window is broken and left unrepaired, people walking by will conclude that no one cares and no one is in charge.&#160; Soon, more windows will be broken, and the sense of anarchy will spread from the building to the street on which it faces, sending a signal that anything goes.</p>
</blockquote>
<p>I couldn’t help myself thinking how this theory fits well with the current state of software development.&#160; When a software project first begins, it is void of any imperfection for no one began to write a single line of code for it.&#160; Then as the team forms and the tasks are being assigned and executed, the team members start to breath life into the software.&#160; That software could have a very different life depending on various factors of the team in charge of developing it, such as the team members’ experience, skills, knowledge, professionalism and passion for the craft.&#160; </p>
<p>From my days of consulting and working as a team member, I more than often see a lack of responsibility from the individual team member for the most important organ of the software: the source code.&#160; Once the source code has started to rot, it doesn’t matter how colorful or beautiful your design documents are or how detailed your development plan is…your software has a cancer and it will die unless you do something about it.&#160; The typical scenario of the Broken Windows theory in software development goes like this: Robert, a programmer, is working on a subroutine for the Shipping module.&#160; While at it, he notices that the NotifySupplier method is lacking a couple of guard clauses and the comments don’t reflect the code at all.&#160; Since he wasn’t the one to write that code, he ignores it and continues working on his work item.&#160; Will he remember to ask the developer who wrote the NotifySupplier method that it is in need of some fixing?&#160; Who knows.&#160; One meeting here, one phone call there, and chances are that Robert will forget about it.&#160; Upon that time, the software has started to confirm the theory.&#160; It now has a broken window.&#160; What’s even more dangerous is that this theory can cascade all over the core of the software if its internals show a high degree of coupling with many components.&#160; A good analogy would be like a tree burning in a forest.&#160; It doesn’t take much time for the fire to spread to the other trees.&#160; The outcome of this is simple: <strong>chaos</strong>.&#160; The remedy is also simple: <strong>you</strong>.&#160; What are you going to do about it?&#160; Are you going to run from it or are you going to take corrective actions to prevent the cancer to spread all over the system?</p>
<p>Another potential risky situation that may arise from the Broken Window theory in software development is that if you don’t take care of fixing the broken code as soon as humanly possible, your junior developers and interns (if you have any) will have the impression that all this is normal in our industry.&#160; The result of this is that as they continue to build and ship software in the future, those junior developers and interns will have shipped products with broken windows in them.&#160; The theory can also backfire on your organization as the developers, sooner or later, realize that the organization isn’t mature enough and might decide to work somewhere else that will provide them the proper environment to appreciate the craft of building software and pay attention to the smallest details.&#160;&#160; Here are a few examples of what I consider a broken window in the code and that should get fixed as soon as possible:</p>
<ul>
<li>Missing unit tests</li>
<li>Comments out-of-sync with the target code</li>
<li>A method, class or module that should be refactored</li>
<li>A complex piece of code that could be replaced with a design pattern to better express its intent</li>
<li>A broken build</li>
<li>Etc.</li>
</ul>
<p>There are also some solutions to prevent this theory to hinder the software project: </p>
<ul>
<li>Performing code reviews at least once a week</li>
<li>Encouraging pair programming, as four eyes are better than two <img src='http://blog.briandicroce.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
<li>Putting in place automated tools that will break the build when rules are broken (<a href="http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx">FxCop</a>, <a href="http://www.ndepend.com/">NDepend</a>, <a href="http://www.ncover.com/">NCover</a>, etc.)</li>
<li>Having the programmer check in her modifications in a private build such that if it succeeds then the modifications are merged to the repository.&#160; Have a look at <a href="http://www.jetbrains.com/teamcity/">Team City</a> for it does that very well.</li>
<li>Providing intelligent tools to your developers so that they can work more effectively with the codebase.&#160; A tool like <a href="http://www.jetbrains.com/resharper/index.html">ReSharper</a> quickly comes into mind.</li>
<li>Instilling an atmosphere that considers quality as the most important instrument to drive the organization to success.</li>
</ul>
<p>But the tools and processes aren’t the heart of the solution.&#160; What will either make or break the software is the <strong>attitude</strong> of every single individual affected to the project.&#160; To encourage the growth and maturity of this attitude, developers should educate themselves on the trends of software development.&#160; Developers should constantly thrive to do as much as possible to become <a href="http://blog.briandicroce.com/2007/12/29/ten-simple-tips-to-become-a-valuable-software-professional/">more valuable software professionals</a>.&#160;&#160; </p>
<p>Though he doesn’t talk about this theory explicitly in his book, <a href="http://www.amazon.com/exec/obidos/ASIN/0131857258/brdicr-20">Agile Principles, Patterns, and Practices in C#</a>, Robert C. Martin does reflect on the basis of the Broken Windows theory and also does provide a remedy to fight this problem in software.&#160; According to him, and I agree with his statement, it’s all about having an attitude of being professional:</p>
<blockquote><p>The attitude that agile developers have toward the design of the software is the same attitude that surgeons have toward sterile procedure.&#160; Sterile procedure is what makes surgery <em>possible</em>.&#160; Without it, the risk of infection would be far too high to tolerate.&#160; Agile developers feel the same way about their designs.&#160; <strong>The risk of letting even the tiniest bit of rot begin is too high to tolerate</strong>.</p>
<p>The design must remain clean.&#160; And since the source code is the most important expression of the design, it too must remain clean.&#160; <strong>Professionalism dictates that we, as software developers, cannot tolerate code rot</strong>.</p>
</blockquote>
<p>Remember: Only <strong>YOU</strong> can prevent broken windows in your software project from spreading like cancer.&#160; You have access to a grand vast of online resources and tools to help you fix those broken windows before it’s too late.&#160; Do you have the attitude to do as such?</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/02/08/broken-windows-theory-in-software/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting Familiar With Your Basic .NET Delegates</title>
		<link>http://blog.briandicroce.com/2009/02/01/getting-familiar-with-your-basic-net-delegates/</link>
		<comments>http://blog.briandicroce.com/2009/02/01/getting-familiar-with-your-basic-net-delegates/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 03:01:09 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/?p=490</guid>
		<description><![CDATA[The .NET framework is a complete toolbox of programming constructs that allows a developer to create any kind of software.&#160; You’re practically limited by your imagination and knowledge on the framework.&#160; Such a knowledge can be satisfied by getting more familiar with its interfaces, abstract classes, concrete types, events and delegates.&#160; In this post, I [...]]]></description>
			<content:encoded><![CDATA[<p>The .NET framework is a complete toolbox of programming constructs that allows a developer to create any kind of software.&#160; You’re practically limited by your imagination and knowledge on the framework.&#160; Such a knowledge can be satisfied by getting more familiar with its interfaces, abstract classes, concrete types, events and delegates.&#160; In this post, I want to concentrate on inviting you to get familiar with some basic and powerful delegates provided by the .NET framework.&#160; The reason of this post is that I often see developers create their own delegates that have the same semantics of the ones provided in the framework mostly because they aren’t aware of them.&#160; Instead of violating the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> principle of programming, let’s discover and learn about them so that we can avoid reinventing the wheel.</p>
<p>The set of basic delegates that I think .NET developers should be familiar and comfortable using are the generic <strong>Action&lt;T&gt;</strong>, <strong>Comparison&lt;T&gt;</strong>, <strong>Converter&lt;TInput, TOutput&gt; </strong>and <strong>Predicate&lt;T&gt;</strong> delegates.&#160; I will introduce you to each of these delegates by describing their usage with a simple example so that you can start using them in your codebase where needed.</p>
<h4>The Action&lt;T&gt; delegate</h4>
<p>In .NET 2.0, the Action delegate, declared in <strong>mscorlib.dll</strong>, has the following signature:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">void</span> Action&lt;T&gt;(T obj);</pre>
</p></div>
</div>
<p>When Microsoft released .NET 3.5, four more Action delegates were included in <strong>System.Core.dll</strong>:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">void</span> Action()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">void</span> Action&lt;T1,T2)(T1 arg1, T2 arg2)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">void</span> Action&lt;T1,T2,T3)(T1 arg1, T2 arg2, T3 arg3)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">void</span> Action&lt;T1,T2,T4)(T1 arg1, T2 arg2, T3 arg3, T4 arg4)</pre>
</p></div>
</div>
<p>Regardless of the different signatures of the Action delegate, its semantic is to <strong>encapsulate a method which doesn’t return a value, and which holds a variable number of parameters</strong>:</p>
<ul>
<li>If the method is parameterless, you should prefer the Action delegate. </li>
<li>If the method accepts one parameter, you should prefer the generic Action&lt;T&gt; delegate. </li>
<li>If the method accepts two parameters, you should prefer the generic Action&lt;T1, T2&gt; delegate. </li>
<li>If the method accepts three parameters, you should prefer the generic Action&lt;T1, T2, T3&gt; delegate. </li>
<li>If the method accepts four parameters, you should prefer the generic Action&lt;T1, T2, T3, T4&gt; delegate. </li>
<li>If the method accepts five (or more) parameters, you should prefer reviewing your design <img src='http://blog.briandicroce.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </li>
</ul>
<p>A quick analysis with <a href="http://www.red-gate.com/products/reflector/">Reflector</a> on the these delegates show that only the Action&lt;T&gt; delegate is being used by .NET in a couple of places, notably as a parameter for the ForEach method of both the static Array class and the generic List&lt;T&gt; class.&#160; The other locations where this delegate is being used aren’t publicly accessible, so we will restrain ourselves to understanding the use of this delegate with a simple usage with the ForEach method of the generic List&lt;T&gt;.</p>
<p>Suppose we have a collection of toys which we want to iterate to display each of its name on the console.&#160; A possible solution to satisfy this requirement without using the Action&lt;T&gt; delegate could be:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #008000">// Create our toys collection</span>
List&lt;Toy&gt; toys = Repository&lt;Toy&gt;.FetchAll(); <span style="color: #008000">// Not that important how we get our toys collection here</span>

<span style="color: #008000">// Iterate through the collection using a foreach statement and display each toy's name on the console</span>
<span style="color: #0000ff">foreach</span>(var toy <span style="color: #0000ff">in</span> toys)
{
  System.Console.WriteLine(toy.Name);
}</pre>
</div>
<p>The above solution works and your unit tests should pass.&#160; But does it warrant a check-in to the source code repository?&#160; This code we have written to satisfy the requirement seems to me very imperative, almost like how a robot would think.&#160; I am mixing both the “how” and the “what” in this context.&#160; For instance, I am iterating on the collection with a foreach statement (the “how”) in order to fulfill my intent which is to display the toy’s name on the console (the “what”).&#160; Since my brain is better at processing one information at a time, I rather focus on the intention rather than the implementation.&#160; Maybe we can hold on that check-in and see if there’s a better way to do this.</p>
<p>As a matter of fact, there is.&#160; The generic List&lt;T&gt; class defines an instance method which iterates the collection and performs an <em>action</em> for each of its element via an Action&lt;T&gt; delegate.&#160; The Array class defines the same method but it is added as an extension method on that type.&#160; That being said, if there’s another data structure that implements IEnumerable&lt;T&gt; or IEnumerable which doesn’t have an equivalent of a ForEach method, there’s nothing stopping you from creating one on the type via an extension method.&#160; This is the second solution that satisfy this requirement and which clearly brings out my intention forward because I don’t care how the collection is being iterated or what algorithm is used for the iteration, I’m just concerned of doing something with each of its element in a sequential manner.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #008000">// Create our toys collection</span>
List&lt;Toy&gt; toys = Repository&lt;Toy&gt;.FetchAll(); <span style="color: #008000">// Not that important how we get our toys collection here</span>

<span style="color: #008000">// Display each toy's name in the console (using a lambda expression)</span>
toys.ForEach(toy =&gt; System.Console.WriteLine(toy.Name));

<span style="color: #008000">// You can also use an anonymous delegate to do the same thing...only with more keyboard typing than is necessary</span>
toys.ForEach( <span style="color: #0000ff">delegate</span>(Toy toy)
              {
                System.Console.WriteLine(toy.Name);
              }
            );</pre>
</div>
<p>Ignoring the instruction using the anonymous delegate, and instead focusing on the one using the lambda expression, we can clearly see that our intention is much clearer.&#160; On a side note, I will use lambda expression whenever needed for the following examples to keep them shorter.&#160; I just added the anonymous delegate if you weren’t too familiar with lambdas.&#160; As you see by using the lambda expression that I don’t have to use the delegate keyword and I can safely rest on the compiler’s type inference&#160; functionality which are both a time saver.&#160; The bottom line with the Action delegate is that it is there for you to do something (perform an <em>action</em>) on an element.&#160; It is commonly used when some action must be performed on an element during an iteration over a collection, but keep in mind that it doesn’t have to be used <em>just</em> in the context of an iteration over a collection.&#160; The Action delegate is also a powerful move to consider when, for example, a collection doesn’t offer a specific iterator that you’re looking for, such as an EvenNumberIterator.&#160; In this case, for every iterated element, you’ll do something specific if the number is even, such as displaying it on the screen (notice that to use multiple statements with lambdas, those statements should be enclosed within braces):</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">List&lt;<span style="color: #0000ff">int</span>&gt; numbers = Generator&lt;<span style="color: #0000ff">int</span>&gt;.GenerateSequence( 10 ); <span style="color: #008000">// Generate a sequence of 10 random numbers</span>

numbers.ForEach( number =&gt;
                 {
                     <span style="color: #008000">// Display the number only if it's even</span>
                     <span style="color: #0000ff">if</span> ( number % 2 == 0 )
                        Console.WriteLine( number );
                 }
               );</pre>
</div>
<p>Something to keep in mind with all the parameterized Action delegates is that the <strong>parameters are passed by value</strong> which means that any modification to anyone of them will only be local to the scope of delegate’s body or the method being invoked by the delegate.&#160; In other words, the collection’s items won’t be modified.&#160; The multiple parameterized Action delegates introduced in .NET 3.5 allow you to use them in case you need to do something with either two, three or four parameters (they can all be of the same type if needed).</p>
<h4>The Comparison&lt;T&gt; delegate</h4>
<p>Introduced in .NET 2.0, the generic Comparison&lt;T&gt; delegate is mostly used for <strong>sorting elements within a collection</strong>.&#160; It is currently used by the Sort methods of both the Array and generic List&lt;T&gt; classes.&#160; This delegate has the following signature:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">int</span> Comparison&lt;T&gt;(T x, T y)</pre>
</div>
<p>The nature of the delegate is identical to the IComparer&lt;T&gt;.Compare method in that:</p>
<ul>
<li>if x &gt; y, then a positive value is returned (traditionally that value is 1); </li>
<li>if x &lt; y then a negative value is returned (traditionally that value is -1); </li>
<li>in the case where both x and y are equal, a value of 0 is returned. </li>
</ul>
<p>This shouldn’t be too much of a surprise because internally the Sort method of both the Array and generic List&lt;T&gt; classes use the generic FunctorComparer&lt;T&gt; to wrap the delegate so that it can be used as an IComparer&lt;T&gt; object internally.&#160; When the IComparer&lt;T&gt;.Compare method is invoked by .NET, it is actually the Comparison&lt;T&gt; delegate that is fired.&#160; Pretty neat, uh?&#160; The semantic of using this delegate (or the IComparer interface for that matter) is to define an <strong>alternative order</strong> for sorting elements within a collection.&#160; Getting back to our toys collection, suppose we want to sort them by name (a string).&#160; We could provide the following code to satisfy this requirement:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">List&lt;Toy&gt; toys = Repository&lt;Toy&gt;.FetchAll();  <span style="color: #008000">// Again it doesn't matter how we get our toys</span>

<span style="color: #008000">// using a lambda expression to define our sorting algorithm</span>
toys.Sort((x, y) =&gt;
           {
               <span style="color: #0000ff">if</span> (x == y) <span style="color: #0000ff">return</span> 0;

               <span style="color: #0000ff">if</span> (x == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">return</span> -1;

               <span style="color: #0000ff">if</span> (y == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">return</span> 1;

               <span style="color: #0000ff">return</span> x.Name.CompareTo(y.Name);

           }
        );</pre>
</div>
<p>This solution is great if you only have one sorting strategy and you foresee that it will not change in the future.&#160; But there’s a better way to use this delegate.&#160; In fact, the power of the Comparison&lt;T&gt; delegate is that…it’s a delegate!&#160; This means that we can do some crazy and complex sorting if we must by combining various strategies!&#160; What if we want to sort our toys first by name, then by price and also by a different criteria?&#160; Using a lambda expression is asking way too much trouble.&#160; Let’s see how we can take advantage of the Comparison&lt;T&gt; delegate in this case.&#160; Let us now sort our toys by name, then by price.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)
{
    List&lt;Toy&gt; toys = Repository&lt;Toy&gt;.FetchAll();  <span style="color: #008000">// Again it doesn't matter how we get our toys</span>

    <span style="color: #008000">// Instantiate our Comparison&lt;T&gt; delegate in a way that we want to sort our toys by name (first order), then by price (second order)</span>
    Comparison&lt;Toy&gt; comparison = CompareToysByName;
    comparison += CompareToysByPrice;

    toys.Sort(comparison);
}

<span style="color: #008000">// Compare two toys by name</span>
<span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> CompareToysByName( Toy x, Toy y )
{
    <span style="color: #0000ff">if</span> (x == y) <span style="color: #0000ff">return</span> 0;

    <span style="color: #0000ff">if</span> (x == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">return</span> -1;

    <span style="color: #0000ff">if</span> (y == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">return</span> 1;

    <span style="color: #0000ff">return</span> x.Name.CompareTo(y.Name);
}

<span style="color: #008000">// Compares two toys by price</span>
<span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> CompareToysByPrice( Toy x, Toy y )
{
    <span style="color: #0000ff">if</span> (x == y) <span style="color: #0000ff">return</span> 0;

    <span style="color: #0000ff">if</span> (x == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">return</span> -1;

    <span style="color: #0000ff">if</span> (y == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">return</span> 1;

    <span style="color: #0000ff">return</span> x.Price.CompareTo(y.Price);
}</pre>
</div>
<p>This is awesome because we can simply mix our sorting strategies through a simple delegate which will be used as a parameter to the Sort method of both an Array and generic List&lt;T&gt; classes.&#160; Once the Sort method has finished its job, your collection will be sorted by the criteria you have specified.&#160; </p>
<h4>The Converter&lt;TInput, TOutput&gt; delegate</h4>
<p>The next delegate that I would like to be familiar with is the generic Converter&lt;TInput, TOutput&gt; delegate which was introduced in .NET 2.0.&#160; Just like its other counterparts, this delegate is being used by both the Array and generic List&lt;T&gt; classes, more specifically in their ConvertAll&lt;TOutput&gt; methods.&#160; The signature of the Converter&lt;TInput, TOutput&gt; delegate is as follows:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> TOutput Converter&lt;TInput, TOutput&gt;(TInput input)</pre>
</div>
<p>This delegate is useful whenever you have to define a strategy to <strong>convert an object from one type to another type</strong>.&#160; For example, you might want to convert an object of type System.Int32 to a type System.String.&#160; Continuing with our toys example, let us consider the following simple requirement: we want to create a list containing only the names of every toy from our toys collection.&#160; </p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">List&lt;Toy&gt; toys = Repository&lt;Toy&gt;.FetchAll(); <span style="color: #008000">// Not really important how we get all our toys.</span>

List&lt;<span style="color: #0000ff">string</span>&gt; toysNames = toys.ConvertAll(toy =&gt; toy.Name);</pre>
</div>
<p>It’s that easy!&#160; Our strategy is basically that given a Toy (this is our closed type for TInput), we’ll return a String (this is our closed type for TOutput).&#160; Just like we have done with the Comparison&lt;T&gt; delegate example, you can also mix in some conversion strategies.&#160; Just remember that variance on delegates isn’t supported in C# 3.0.&#160; For more information on this topic, I strongly suggest you to read <a href="http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx">Eric Lippert’s eleven-part posts on covariance and contravariance</a>. </p>
<h4>The Predicate&lt;T&gt; delegate</h4>
<p>Though being used once again by the Array and generic List&lt;T&gt; classes, the generic Predicate&lt;T&gt; delegate is more used by the framework than the three other ones.&#160; This delegate represents a method that <strong>defines a set of criteria and determines whether the specified object meets those criteria</strong>.&#160; You can also see this delegate acting like a filter on a collection. The signature of the Predicate&lt;T&gt; delegate is as follows:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">delegate</span> <span style="color: #0000ff">bool</span> Predicate&lt;T&gt;(T obj)</pre>
</div>
<p>Like I said, the .NET framework uses this delegate in both the Array and generic List&lt;T&gt; classes in many of their methods (Exists, Find, FindAll, FindIndex, etc.).&#160; In short, you provide an evaluation context for a parameter where you either return true if the condition for that parameter is met, false otherwise.&#160; Continuing with our toys example, suppose that we want to 1) find out whether we have a toy with a specific name and year, and 2) find all the toys that were made in Canada.&#160; We could simply use the provided methods (Exists and FindAll) and passing them a Predicate&lt;T&gt; delegate that will contain our logic for evaluating whether or not the condition is satisfied.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">List&lt;Toy&gt; toys = Repository&lt;Toy&gt;.FetchAll(); <span style="color: #008000">// We're not particularly interested in knowing how we got our toys.</span>

<span style="color: #008000">// 1. We're interested in knowing whether we have my favorite Transformer of all: Megatron!  </span>
<span style="color: #008000">// I have a couple of them, but I'm specifically interested in the one my parents bought me in 1985.</span>
<span style="color: #0000ff">bool</span> containsMy1985Megatron = toys.Exists( toy =&gt; toy.Name == <span style="color: #006080">&quot;Megatron&quot;</span> &amp;&amp; toy.Year = 1985 );

<span style="color: #008000">// 2. We're now interested in retrieving all our toys made in Canada.</span>
List&lt;Toy&gt; myDecepticons = toys.FindAll( toy =&gt; toy.Country == <span style="color: #006080">&quot;Canada&quot;</span> );</pre>
</div>
<p>By simply invoking those methods and passing them a Predicate&lt;T&gt; delegate, I can concentrate on what I want in a more declarative way because I don’t have to explicitly iterate/loop over the collection with either a foreach statement, a for loop, a while loop, etc.&#160; I just want to iterate over my toys, and either find out if a match was found or retrieve all my toys that satisfy some condition.&#160; This last statement is also true for any of the other delegates that I presented here. </p>
<h4>Conclusion</h4>
<p>In this post, I wanted to familiarize you with some of the basic delegates that are offered by the .NET framework BCL (outside of ASP.NET, ADO.NET, etc.) that allow you more flexibility in specifying different strategies for various contexts (such as in finding, retrieving, converting or performing an action to an element of a collection).&#160; In other words, these delegates allow me to concentrate more on the business logic rather than, for example, writing the plumbing necessary to iterate over a collection, because they are commonly used as a parameter to a method which already implements the plumbing on my behalf. </p>
<p>Whether I’m consulting with companies or training developers, more often than not I see these kind of delegates redefined in code because the developers were completely ignorant of the existence of these delegates!&#160; That’s too bad, because violating the DRY principle forces you to document, test and maintain more code than you would actually need in your codebase.&#160; Remember that the .NET framework isn’t solely C#.&#160; The .NET framework is a huge toolbox that provides you with many tools (interfaces, classes, events, delegates, etc.) to build various applications.&#160; The more you know about those tools and how to use them properly, the better your code will be and the more valuable you will become to your organization and team.</p>
<p>For more information on delegates, I highly recommend you the following links:</p>
<ul>
<li>Get a copy of <a href="http://www.amazon.com/exec/obidos/ASIN/0321485890/brdicr-20"><strong>More Effective C#: 50 Specific Ways to Improve Your C#</strong></a> by Bill Wagner.&#160; More specifically, take a look at Item 18: Decouple Iterations from Actions, Predicate, and Functions to get a better understanding of Action, Predicate and Func delegates. </li>
<li>Get a copy of <a href="http://www.amazon.com/exec/obidos/ASIN/1933988363/brdicr-20"><strong>C# in Depth</strong></a> by Jon Skeet.&#160; This is one guy who has been very helpful in the .NET community by writing many <a href="http://www.yoda.arachsys.com/csharp/">useful articles</a> and also published, in my opinion, the best practical book on C# 2.0/3.0 to date. </li>
<li>Check out dnrTV’s episode on <a href="http://www.dnrtv.com/default.aspx?showNum=40">events and delegates with Mark Miller</a>. </li>
</ul>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2009/02/01/getting-familiar-with-your-basic-net-delegates/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Rely on your experience and knowledge over some tools&#8217; recommendations</title>
		<link>http://blog.briandicroce.com/2008/12/30/rely-on-your-experience-and-knowledge-over-some-tools-recommendations/</link>
		<comments>http://blog.briandicroce.com/2008/12/30/rely-on-your-experience-and-knowledge-over-some-tools-recommendations/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 20:49:19 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/?p=481</guid>
		<description><![CDATA[Whenever .NET developers tell me they know C# really well, I can’t help myself but to wonder whether they mean 1) the programming language itself (its syntax, its grammar and its constructs), 2) the compiler or 3) both. The compiler is the heart of any programming language and if you respect it enough when you [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever .NET developers tell me they know C# really well, I can’t help myself but to wonder whether they mean 1) the programming language itself (its syntax, its grammar and its constructs), 2) the compiler or 3) both. The compiler is the heart of any programming language and if you respect it enough when you code, you can create some very optimal and productive things with it. On the other hand, if you only focus on the programming language and disregard the inner workings of the compiler, chances are that you risk affecting the health of your software…negatively.</p>
<p>For example, I tend to see a severe chronic problems amongst C# developers towards operations related to <a href="http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx">boxing and unboxing</a>. Reading some newsgroups posts, blog posts’ comments and working with many .NET developers that have never heard these terms before made me aware that this chronic problem was pretty much widespread. This phenomena is even more real for developers that are too keen on using coding/refactoring tools, like <a href="http://www.jetbrains.com/resharper/">ReSharper</a>, because they tend to rely more on the suggestions of those tools instead of relying on deeper knowledge to decide whether or not a change in the code is due. Before building on this idea, I think it’ll be a good idea to refresh our concepts of boxing and unboxing in .NET. For this, I’ll share some notes from the excellent book on .NET programming, <a href="http://www.amazon.com/exec/obidos/ASIN/0735621632/brdicr-20">CLR via C#</a> (second edition), by Jeffrey Richter. </p>
<p>Let us consider what happens when <strong>boxing</strong> occurs:</p>
<blockquote><p>It&#8217;s possible to convert a value type to a reference type by using a mechanism called boxing. Internally, here&#8217;s what happens when an instance of a value type is boxed:</p>
<ol>
<li>Memory is allocated from the managed heap. The amount of memory allocated is the size required by the value type&#8217;s fields plus the two additional overhead members (the type object pointer and the sync block index) required by all objects on the managed heap.</li>
<li>The value type&#8217;s fields are copied to the newly allocated heap memory.</li>
<li>The address of the object is returned. This address is now a reference to an object; the value type is now a reference type.</li>
</ol>
<p>The C# compiler automatically produces the IL code necessary to box a value type instance, <strong><span style="text-decoration: underline;">but you still need to understand what&#8217;s going on internally so that you&#8217;re aware of code size and performance issues</span></strong>.</p></blockquote>
<p>You see that I have put the last sentence in bold. What Jeffrey is saying there is extremely important, because some tools aren’t smart enough to handle every single mechanisms of the compiler, therefore “you still need to understand what’s going on internally so that you’re aware of code size and performance issues”. Let us move on. </p>
<p>Let us now consider what happens during <strong>unboxing</strong>:</p>
<blockquote><p>Unboxing is <em>not</em> the exact opposite of boxing. The unboxing operation is much less costly than boxing. Unboxing is really just the operation of obtaining a pointer to the raw value type (data fields) contained within an object. In effect, the pointer refers to the unboxed portion in the boxed instance. So, unlike boxing, unboxing doesn’t involve the copying of any bytes in memory. Having made this important clarification, it is important to note than an unboxing operation is typically followed by copying the fields. Frequently, however, an unboxing operation is followed immediately by copying its fields. Internally, here’s exactly what happens when a boxed value type instance in unboxed:</p>
<ol>
<li>If the variable containing the reference to the boxed value type is <span style="font-family: courier new;">null</span>, a <span style="font-family: courier new;">NullReferenceException</span> is thrown.</li>
<li>If the reference doesn’t refer to an object that is a boxed instance of the desired value type, an <span style="font-family: courier new;">InvalidCastException</span> is thrown.</li>
</ol>
</blockquote>
<p>Now that we have a general idea of what boxing and unboxing are and how they work, we should consider the following as an important principle:</p>
<blockquote><p>Obviously, boxing and unboxing/copy operations hurt your application’s performance in terms of both speed and memory, <strong>so you should be aware of when the compiler generates code to perform these operations automatically and <span style="text-decoration: underline;">try to write code that minimizes this code generation</span></strong>.</p></blockquote>
<p>In a nutshell you get boxing whenever a conversion from a value type to a reference type occurs. Conceptually, unboxing is the reverse operation where a conversion from a reference type (such as an instance of <span style="font-family: courier new;">Object</span>) to a value type occurs. In .NET, value types are any object that derives from <span style="font-family: courier new;">System.ValueType</span>, such as your familiar primitive types (<span style="font-family: courier new;">Int32</span>, <span style="font-family: courier new;">Boolean</span>, <span style="font-family: courier new;">Char</span>, <span style="font-family: courier new;">Decimal</span>, etc.), structures and enumerations. Reference types are classes, interfaces, events, delegates, exceptions, etc. (basically anything that doesn’t derive from <span style="font-family: courier new;">System.ValueType</span>). The key element to understand is not necessary the mechanisms involved by the compiler, but rather to be conscious that there is a performance hit whenever boxing and unboxing occurs. Since the introduction of generics with .NET 2.0, we don’t have to deal as much with such concepts because generics provide type safety and better performance by avoiding boxing/unboxing operations. But not everything in .NET is generic and it’s extremely important to discover those potential “danger zones” where boxing/unboxing operations can occur within the framework, especially if you’re using development tools like ReSharper.</p>
<p>I’m a big fan of <a href="http://www.jetbrains.com">JetBrains</a> and all their products that help me be more effective as a developer. Their continuous integration tool, <a href="http://www.jetbrains.com/teamcity/">TeamCity</a>, is in my opinion the most innovative and complete software to set up a CI environment. Their <a href="http://www.jetbrains.com/profiler/index.html">dotTrace</a> profiling application is also one of those tools that is installed whenever I set up a fresh development environment. But the one tool that I cannot code without is none other than <a href="http://www.jetbrains.com/resharper/">ReSharper</a>. I believe so much in that tool for making me more productive and effective when I code that I even wrote a post about <a href="http://blog.briandicroce.com/2008/05/12/the-case-for-resharper-in-the-enterprise/">reasons why the enterprise should seriously consider ReSharper for its developers</a>. Unfortunately, ReSharper isn’t perfect. Excellent yes, but not perfect. Which means that developers should still use their knowledge about the programming language and the compiler to really take advantage of the .NET platform. This statement is even more pronounced when potential boxing/unboxing situations can occur. For example, consider the following code:</p>
<pre class="csharp" name="code">private static void Main()
{
  string name = "Brian";
  int age = 27;
  System.Console.WriteLine("Hello, my name is " + name + " and I'm " + age + " years old.");
}</pre>
<p>At first sight, it seems that the above code is right.  I would agree with you at some extend.  I agree that it compiles just fine and that it outputs the correct message on the console.  If you had ReSharper installed, it will also say that the code is well formatted and that you’re good to check it in (the feedback for this is a little green icon on the top right side of the code window in Visual Studio).  The underlying problem with this code is that your application gets a performance hit because a boxing operation has occurred.  In fact, if you view the IL emitted by the compiler you will see a <span style="font-family: Courier New;">box</span> instruction which forces the CLR to perform boxing for the value type.  The reason for this is that implicitly, this overloaded version of <span style="font-family: Courier New;">Console.WriteLine()</span> calls one of the overloaded versions <span style="font-family: Courier New;">String.Concat()</span> which accepts <span style="font-family: Courier New;">Object</span> parameters (a tool like <a href="http://www.red-gate.com/products/reflector/">Reflector</a> or <a href="http://msdn.microsoft.com/en-us/library/f7dy01k1(VS.80).aspx">ILDASM</a> can help you to see this for yourself).</p>
<p>This is the point where knowledge and experience kicks in.  The proper way to write the above code is as following:</p>
<pre class="csharp" name="code">private static void Main()
{
  string name = "Brian";
  int age = 27;
  System.Console.WriteLine("Hello, my name is " + name + " and I'm " + age.ToString() + " years old.");
}</pre>
<p>The reason the code above is superior is that you get all the benefits of the first version without the performance hit because no boxing occurs (again you can check this for yourself by viewing the IL emitted by the compiler).  As a matter of fact, calling a <span style="font-family: Courier New;">ToString()</span> method on a value type will tell the CLR to invoke that method instead on that particular instance instead of boxing the value type as a reference type and then calling <span style="font-family: Courier New;">Object</span>’s <span style="font-family: Courier New;">ToString()</span> on it.  If you call Console.WriteLine( age ) instead, you might be surprised to know that no boxing occurs because one of the overloaded versions of that method actually accepts an Int32 as a parameter.  </p>
<p>This is why I said that knowledge and experience counts because unless you know these nifty facts you could blindly follow your tools’ recommendations which might propose some false positives.  An example of a false positive can be ReSharper graying out the <span style="font-family: Courier New;">ToString()</span> method call on the <span style="font-family: Courier New;">age</span> variable and recommending you to remove it.  Most developers using ReSharper will blindly follow such recommendation and actually remove the invocation of <span style="font-family: Courier New;">ToString()</span> on the value type by either hitting ALT+ENTER on the line and let ReSharper remove the invocation or by reformatting the whole code file by hitting CTRL+ALT+SHIFT+F.  Though the above example seems pretty negligent, you can imagine this kind of operation in a loop or anywhere else a boxing/unboxing operation might occur in a library or within the framework itself.  A simple profiling on the application should detect such a performance bottleneck.  In other words, don’t checkmate yourself by blindly following your tools recommendations!</p>
<p>Even though I only touched on the subject of boxing and unboxing, it’s important to understand that there are many other areas that this post’s ideas apply.  For example, knowing when to explicitly implement an interface, deciding when to favor composition over inheritance, justifying the encapsulation level of a class and its members, etc.  These are strategies that some tools can’t provide a hint for, therefore you should rely on your knowledge and experience on the platform and the programming paradigms (object-oriented, functional, etc.) currently used.  If you have the chance of working in a larger team, I highly recommend your team to conduct code review sessions so that each team member can leverage each other’s knowledge and experience on the platform.  On the other hand, if you don’t have the privilege of participating in code reviews, or even worse if your organization doesn’t encourage such a practice, then you’re pretty much on your own, but you’re not alone!  In that case, I recommend you to read the following books which will no doubt make you a better C#/.NET programmer:</p>
<ul>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/0735621632/brdicr-20"><strong>CLR via C# (Second Edition)</strong></a> by Jeffrey Richter</li>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/1933988363/brdicr-20"><strong>C# in Depth</strong></a> by Jon Skeet</li>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/0321245660/brdicr-20"><strong>Effective C#</strong></a> by Bill Wagner</li>
</ul>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/12/30/rely-on-your-experience-and-knowledge-over-some-tools-recommendations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Mapping a shortcut to execute unit test in current context with ReSharper</title>
		<link>http://blog.briandicroce.com/2008/11/30/mapping-a-shortcut-to-execute-unit-test-in-current-context-with-resharper/</link>
		<comments>http://blog.briandicroce.com/2008/11/30/mapping-a-shortcut-to-execute-unit-test-in-current-context-with-resharper/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 02:34:53 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2008/11/30/mapping-a-shortcut-to-execute-unit-test-in-current-context-with-resharper/</guid>
		<description><![CDATA[It shouldn’t be a surprise to you that I’m a big fan of ReSharper since its early versions.&#160; Its productivity boost and refactoring possibilities should alone convey a reason to learn and master it to be more effective when programming with C#.&#160; 
One thing that I wish came out of the box, but is currently [...]]]></description>
			<content:encoded><![CDATA[<p>It shouldn’t be a surprise to you that I’m a big fan of <a href="http://www.jetbrains.com/resharper/">ReSharper</a> since its early versions.&#160; Its productivity boost and refactoring possibilities should alone convey a reason to learn and master it to be more effective when programming with C#.&#160; </p>
<p>One thing that I wish came out of the box, but is currently missing, is a shortcut that allows me to run my unit test in the current context (by context I mean where my cursor is currently located.&#160; If my cursor is currently in the body of TestA, then that shortcut will run the TestA unit test).&#160; This will allow me to use the mouse even less, because right now I’m using the mouse to tell ReSharper to execute the current test. It turns out that though this functionality is lacking in ReSharper 4.1, the possibility to do so still exists.&#160; </p>
<p>Here are the steps to follow if you want to map a keyboard shortcut that will run a test in the current context:</p>
<ol>
<li>In Visual Studio, go to <strong>Tools</strong> –&gt; <strong>Options</strong></li>
<li>Expand the <strong>Environment</strong> node, then go to <strong>Keyboard</strong></li>
<li>In the commands list, search for <strong>Resharper.Resharper_UnitTest_ContextRun</strong></li>
<li>Assign a shortcut for that command.&#160; In my case, I have decided to map it with <strong>Alt + X</strong> in the <strong>Global</strong> context.&#160; That shortcut is intuitive (X for &quot;execute”) and easy to trigger.&#160; You can choose whatever mapping you want, just make sure there are no conflicts with other existing shortcuts.</li>
</ol>
<p>Here’s a screenshot of the above steps in action:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: block; float: none; margin: 0px auto 10px; border-left: 0px; border-bottom: 0px" height="386" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2008/11/image2.png" width="648" border="0" /> </p>
<p>Now you can place your cursor in a test context (test body or test method name) and hit that shortcut (Alt + X in my case) and that test will get executed.&#160; A good tip to follow is to save your Visual Studio keyboard settings (and ReSharper settings too) under a backup repository (such as <a href="http://www.mesh.com">Live Mesh</a> for example) so that you (or your team) can re-use those same settings in your Visual Studio sessions in other machines.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/11/30/mapping-a-shortcut-to-execute-unit-test-in-current-context-with-resharper/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Being professional at work: an interview with Uncle Bob, Michael Feathers and Pete McBreen (JAOO)</title>
		<link>http://blog.briandicroce.com/2008/11/18/being-professional-at-work-an-interview-with-uncle-bob-michael-feathers-and-pete-mcbreen-jaoo/</link>
		<comments>http://blog.briandicroce.com/2008/11/18/being-professional-at-work-an-interview-with-uncle-bob-michael-feathers-and-pete-mcbreen-jaoo/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 01:14:05 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Media/Presentations]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2008/11/18/being-professional-at-work-an-interview-with-uncle-bob-michael-feathers-and-pete-mcbreen-jaoo/</guid>
		<description><![CDATA[I just finished watching a special interview with Robert “Uncle Bob” Martin, Michael Feathers and Pete McBreen (author of one of my favorite books “Software Craftsmanship”) that was recorded at last year’s JAOO (check out more JAOO videos here).&#160; The subject is one my dearest and often misunderstood in the industry: being professional at work [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished watching a special <a href="http://blog.jaoo.dk/2008/06/19/is-it-professional-to-cheat-the-boss/">interview</a> with Robert “Uncle Bob” Martin, Michael Feathers and Pete McBreen (author of one of my favorite books “<a href="http://www.mcbreen.ab.ca/SoftwareCraftsmanship/index.html">Software Craftsmanship</a>”) that was recorded at last year’s JAOO (check <a href="http://jaoo.blip.tv/">out more JAOO videos here</a>).&#160; The subject is one my dearest and often misunderstood in the industry: being professional at work by writing clean code.&#160; I’ve studied software engineering as an undergraduate and one thing that was clearly missing in the curriculum was a class or two about how to write good, solid, simple, clean code that works.&#160; Instead we were taught a lot about software processes and how the processes determines whether or not your project will succeed.&#160; That’s sad because deep inside I know that the process is the to support your project, not drive it.&#160; What drives the project at the end of the line is the code.&#160; Your software is ALL about code, not processes.</p>
<p>A couple of things that made me jump while listening to this interview were when Michael Feathers says that in one company, in which he consulted, none of the developers have heard of the “Design Patterns” book by the GoF.&#160; Even a story that Pete McBreen told us about a developer who didn’t checked in his code after working on it more than four weeks.&#160; This is a very important interview that the JAOO folks have made public and even more important for managers and developers to watch it together.&#160; A special thanks for this discovery goes to <a href="http://redsquirrel.com/dave/">Dave Hoover</a> who started a thread on “apprenticeship” over at the <a href="http://groups.google.com/group/software_craftsmanship/">Software Craftsmanship Google group</a>. </p>
<p>&#160;</p>
<div align="center">
<div class="wlWriterEditableSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:82c66866-e1a1-40af-aa2f-165bb2aacd5c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div><embed src="http://blip.tv/play/6Va55HGJtgc" type="application/x-shockwave-flash" width="800" height="480" allowscriptaccess="always" allowfullscreen="true"></embed></div>
</div></div>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/11/18/being-professional-at-work-an-interview-with-uncle-bob-michael-feathers-and-pete-mcbreen-jaoo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pluralsight&#8217;s screencasts on WCF, WF, AtomPub services, Oslo and more!</title>
		<link>http://blog.briandicroce.com/2008/11/08/pluralsights-screencasts-on-wcf-wf-atompub-services-oslo-and-more/</link>
		<comments>http://blog.briandicroce.com/2008/11/08/pluralsights-screencasts-on-wcf-wf-atompub-services-oslo-and-more/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 04:26:32 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Media/Presentations]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2008/11/08/pluralsights-screencasts-on-wcf-wf-atompub-services-oslo-and-more/</guid>
		<description><![CDATA[For your information, Pluralsight (a training firm specialized in Microsoft technologies) recently recorded some pretty useful screencasts about new and important .NET technologies such as WCF, WF, AtomPub services, Oslo and more.&#160; These screencasts are available at no cost and is open to the public.&#160; You can download these screencasts here.
     [...]]]></description>
			<content:encoded><![CDATA[<p>For your information, <a href="http://www.pluralsight.com">Pluralsight</a> (a training firm specialized in Microsoft technologies) recently recorded some pretty useful screencasts about new and important .NET technologies such as WCF, WF, AtomPub services, Oslo and more.&#160; These screencasts are available at no cost and is open to the public.&#160; You can <a href="http://www.pluralsight.com/main/screencasts/default.aspx"><strong>download these screencasts here</strong></a>.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/11/08/pluralsights-screencasts-on-wcf-wf-atompub-services-oslo-and-more/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Edsger Dijkstra on the &#8220;Discipline in Thought&#8221;</title>
		<link>http://blog.briandicroce.com/2008/11/07/edsger-dijkstra-on-the-discipline-in-thought/</link>
		<comments>http://blog.briandicroce.com/2008/11/07/edsger-dijkstra-on-the-discipline-in-thought/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 16:19:00 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Media/Presentations]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2008/11/07/edsger-dijkstra-on-the-discipline-in-thought/</guid>
		<description><![CDATA[I first heard of Professor E.D. Dijkstra in an algorithm course during my undergraduate studies.&#160; I&#8217;ve enjoyed reading his paper The Humble Programmer which he wrote as an acceptance speech for receiving a Turing Award in 1972.&#160; While reading that paper, I couldn’t help myself to notice some key associations between what he suggested was [...]]]></description>
			<content:encoded><![CDATA[<p>I first heard of Professor <a href="http://en.wikipedia.org/wiki/Edsger_W._Dijkstra">E.D. Dijkstra</a> in an algorithm course during my undergraduate studies.&#160; I&#8217;ve enjoyed reading his paper <em><a href="http://www.cs.utexas.edu/~EWD/transcriptions/EWD03xx/EWD340.html">The Humble Programmer</a></em> which he wrote as an acceptance speech for receiving a Turing Award in 1972.&#160; While reading that paper, I couldn’t help myself to notice some key associations between what he suggested was needed to improve the art and science of software development and what the Agile philosophy was bringing forward.&#160; I don’t want to go too much into the detail of that paper, but I strongly suggest you print a copy for yourself and read it.&#160; Now to the essence of this post…I tend to get carried away sometimes…</p>
<p>A couple of years ago, Edsger Dijkstra was interviewed for sharing his thoughts on the discipline of programming.&#160; For example, he mentions the beauty of code being measured in terms of <strong>quality</strong>, <strong>correctness</strong> and <strong>elegance</strong>.&#160; That’s a key value which I hold tight in my heart when programming:&#160; Always having that struggle with my ego, my mind and my intuition about how to write or change an implementation to make it just right…not perfect, but <em>just right</em>.&#160; But what is <em>just right</em>?&#160; See…that’s the kind of struggle I constantly have.&#160; I love it.</p>
<p>While watching the interview, he seemed pretty sad or disappointed with the current state of computer science in the industry, as well as in the academia.&#160; No doubt that we still have a long way to go to bring beauty in code, but I think that we have also make a strong progress since that first 1968 NATO conference on <a href="http://en.wikipedia.org/wiki/Software_engineering">software engineering</a>.&#160; I believe that we’re living in exciting times in this industry.&#160; I don’t know if Dijkstra would agree with that statement.&#160; What do you think?&#160; </p>
<div class="wlWriterEditableSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:3b8f9112-0ec4-43e2-9da6-b28b2f1d9c85" style="padding-right: 0px; display: block; padding-left: 0px; float: none; padding-bottom: 0px; margin-left: auto; width: 400px; margin-right: auto; padding-top: 0px">
<div><embed style="width:400px; height:326px;" wmode="transparent" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-6873628658308030363&amp;hl=en" flashvars=""> </embed></div>
</div>
<p>NOTE:<strong> </strong>I came about this video from the most excellent blog of <a href="http://www.catonmat.net/"><strong>Peteris Krumins</strong></a>.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/11/07/edsger-dijkstra-on-the-discipline-in-thought/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sessions from PDC2008 now available!</title>
		<link>http://blog.briandicroce.com/2008/11/06/sessions-from-pdc2008-now-available/</link>
		<comments>http://blog.briandicroce.com/2008/11/06/sessions-from-pdc2008-now-available/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 03:17:45 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Media/Presentations]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2008/11/06/sessions-from-pdc2008-now-available/</guid>
		<description><![CDATA[This year’s Microsoft Professional Developer Conference is a thing of the past.&#160; For those unlucky ones (like me) who didn’t attend this event, you can now have full and free access to the sessions and the keynotes.&#160; Suddenly life just got better!
Mike Swanson has prepared for us a simple list of all keynotes and sessions [...]]]></description>
			<content:encoded><![CDATA[<p>This year’s Microsoft Professional Developer Conference is a thing of the past.&#160; For those unlucky ones (like me) who didn’t attend this event, you can now have full and free access to the sessions and the keynotes.&#160; Suddenly life just got better!</p>
<p><a href="http://blogs.msdn.com/mswanson">Mike Swanson</a> has prepared for us a <a href="http://blogs.msdn.com/mswanson/pages/PDC2008Sessions.aspx"><strong>simple list of all keynotes and sessions by code and title from this year’s PDC</strong></a>.&#160; This is some really cool stuff!</p>
<p>Remember that you can also browse the sessions by tag on the <a href="http://channel9.msdn.com/pdc2008/">Channel 9 PDC 2008 page</a>.</p>
<p>Now, let’s see what <a href="http://www.microsoft-watch.com/content/web_services_browser/azure_windows_becomes_the_web.html?kc=MWRSS02129TX1K0000535">Azure</a> is really all about…</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/11/06/sessions-from-pdc2008-now-available/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Evolving your design with the Principle of Least Knowledge</title>
		<link>http://blog.briandicroce.com/2008/11/02/evolving-your-design-with-the-principle-of-least-knowledge/</link>
		<comments>http://blog.briandicroce.com/2008/11/02/evolving-your-design-with-the-principle-of-least-knowledge/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 03:05:49 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[.NET Programming]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/?p=405</guid>
		<description><![CDATA[One of the principles of object-oriented development is that you should strive for a design that favors low coupling among objects.&#160; Given that change is constant in a software project, this principle provides many benefits when rightfully applied:

Code is easier to maintain because any change can be isolated to a very limited number of objects [...]]]></description>
			<content:encoded><![CDATA[<p>One of the principles of object-oriented development is that you should strive for a design that favors low coupling among objects.&#160; Given that change is constant in a software project, this principle provides many benefits when rightfully applied:</p>
<ul>
<li>Code is easier to <strong>maintain</strong> because any change can be isolated to a very limited number of objects (hopefully only one) </li>
<li>Code is easier to <strong>use</strong> and <strong>reuse</strong> because your objects are not tightly related to a web of other objects </li>
<li>Code is easier to <strong>understand</strong> because we can focus on the behavior of a very limited number of objects (hopefully only one) </li>
<li>Code is easier to <strong>test</strong> because of the reasons stated above (it also reduces the use of mock objects in our tests which simplifies writing and maintaining them) </li>
</ul>
<p>The last point is very important.&#160; Code that is easier to test favors the writing of unit tests, which avoids potential bugs infesting the system, and embraces change as we have confidence in refactoring the codebase because our unit tests act as a safety net so that if we should brake something in the system, we’ll be notified in a short time…if the tests were well written of course, but that’s a subject for another post.</p>
<p>The <strong>Principle of Least Knowledge</strong>, also known as the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>, is one of those principles which favor low coupling in your codebase.&#160; The most excellent book “<strong><a href="http://blog.briandicroce.com/2008/01/01/book-review-3-head-first-design-patterns/">Head First Design Patterns</a></strong>” defines this principle as the following:</p>
<blockquote><p><em>The Principle of Least Knowledge guides us to reduce the interactions between objects to just a few close “friends”.</em></p>
<p><em>But what does this mean in real terms?&#160; It means when you are designing a system, for any object, be careful of the <strong>number of classes</strong> it interacts with and also how it comes to interact with those classes.</em></p>
<p><em>This principle prevents us from creating designs that have a <strong>large number of classes</strong> coupled together so that changes in one part of the system cascade to other parts.&#160; When you build a lot of dependencies between many classes, you are building a fragile system that will be costly to maintain and complex for others to understand</em>.&#160; &#8212; “<strong>Head First Design Patterns</strong>”, pages 265-266.</p>
</blockquote>
<p>In other words, this principle states that given an object, any of its method should only invoke methods that belong to:</p>
<ul>
<li><strong>The object itself</strong> </li>
<li><strong>Objects passed in as a parameter to the method</strong> </li>
<li><strong>Any object the method creates or instantiates</strong> </li>
<li><strong>Any components of the object</strong> </li>
</ul>
<p>Least but nonetheless important is another pointer which makes the whole difference (like spreading Nutella on whole wheat bread): <strong>methods should avoid invoking methods belonging to objects that were returned from some other call</strong>.</p>
<p>For example, you should avoid writing the following:.</p>
<pre class="csharp" name="code">public void DepositAmount(int accountId, double amount)
{
  Account account = bank.GetAccount( accountId );
  account.Deposit( amount );
}</pre>
<p>
  <br />And instead opt for this: </p>
<p></p>
<pre class="csharp" name="code">public void DepositAmount(int accountId, double amount)
{
  bank.Deposit( accountId, amount );
}</pre>
<p>You should see that the latter implementation is easier to maintain, reuse, understand and test.&#160; We haven’t changed the interface of the method, just its implementation.&#160; This implementation is easier to reuse and test because we don’t have to rely on another external (whether concrete or abstract) object, in this case an Account, to fulfill the service.&#160; We only have one object, the Bank, to deal with.</p>
<p>One of my mentors refers to this principle as the “<strong>Tell, don’t ask</strong>” principle.&#160; It makes a lot of sense.&#160; Alec Sharp, author of <em>Smalltalk by Example</em>, summarizes this principle in other words:</p>
<blockquote>
<p><em>Procedural code gets information then makes decisions.&#160; Object-oriented code <strong>tells</strong> objects to do things. &#8212; <strong>Alec Sharp</strong></em></p>
</blockquote>
<p>That was the theory behind the principle.&#160; Let’s see a concrete and simple example which demonstrates what we should avoid and what we should strive for in our design.</p>
<p>This example doesn’t follow the Principle of Least Knowledge:</p>
<pre class="csharp" name="code">namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      var car = new Car();
      var driver = new Driver( car );

      driver.Car.Engine.Start(); // Not good.
                                 // Changing the signature of Start() will force to change
                                 // the callers too because of the strong coupling.
                                 // It also breaks the encapusalation of CarEngine because
                                 // the Driver knows too much about that class.
    }
  }

  class Driver
  {
    public Driver( Car car )
    {
      Car = car;
    }

    public Car Car { get; private set; }
  }

  class Car
  {
    private readonly CarEngine _engine;

    public Car()
    {
      _engine = new CarEngine();
    }

    public CarEngine Engine
    {
      get
      {
        return _engine;
      }
    }

    public class CarEngine
    {
      public void Start()
      {
        Console.WriteLine( &quot;vroum! vroum!&quot; );
      }
    }
  }
}</pre>
<p>
  <br />Whereas this same example does respect the Principle of Least Knowledge: </p>
<p></p>
<pre class="csharp" name="code">namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      var car = new Car();
      var driver = new Driver( car );

      driver.Car.Start(); /* Much better.&#160; Why?
                             Now, we can also make CarEngine private to the Car class.
                             Even though CarEngine.Start is public doesn't mean we should
                             use it!&#160; Here we decided to remove the Engine property in the Car
                             class.

                             With this approach, the Driver could care less HOW the Car gets
                             started.&#160; Now, CarEngine can evolve on its own without impacting
                             the rest of the system.

                             In other words...just because there's a gun laying on the ground
                             outside Montreal East and it's past 2AM...doesn't mean you should
                             take it!&#160; Not everything that's public is good to take.
                             */
    }
  }

  class Driver
  {
    public Driver( Car car )
    {
      Car = car;
    }

    public Car Car { get; private set; }
  }

  class Car
  {
    private readonly CarEngine _engine;

    public Car()
    {
      _engine = new CarEngine();
    }

    class CarEngine
    {
      public void Start()
      {
        Console.WriteLine( &quot;vroum! vroum!&quot; );
      }
    }

    public void Start()
    {
      _engine.Start();
    }
  }
}</pre>
<p>As you can see from the preceding example, adopting the Principle of Least Knowledge in your design allows you to easily evolve other components in your system since they’re not tightly coupled with other components in other areas of the system.&#160; In this instance, I can easily evolve <strong>CarEngine</strong> without impacting the rest of the system.&#160; For example, I can change the way an engine is started.&#160; Furthermore, I can even make the <strong>CarEngine</strong> class abstract, then have various implementations of it and still avoid rippling changes to my system.&#160; The Principle of Least Knowledge gives me so much ease, flexibility and possibilities to evolve my design and test my system.</p>
<p>Using a tool like <a href="http://www.ndepend.com/">NDepend </a>can help you to pinpoint locations in your code where coupling is strong so that you leverage this principle and step-by-step eliminate the unnecessary coupling among components.</p>
<p>In conclusion, I urge you to not get too personal with objects.&#160; Don&#8217;t be too polite with them neither.&#160; With people yes, but not with objects.&#160; If you need an object to do something for you, tell it to do so.&#160; Another variation of &quot;tell&quot; is &quot;command&quot;.&#160; Don&#8217;t tell the object to ask another object to do some job for you.&#160; If you see that happening, you should remove the intermediate object and have the one that knows more about the underlying operation to execute the job for you.&#160; In his book <em>Applying UML and Patterns</em>, Craig Larman refers to the <a href="http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class4/InformationExpert.html"><strong>Information Expert pattern</strong></a> for this kind of situation.&#160; According to that pattern, you should &quot;<em>assign a responsibility to the information expert &#8211; the class that has the information necessary to fulfill the responsibility</em>&quot;.</p>
<p>As you can see, there are many different interpretations for this principle.&#160; Whether you know it as the Principle of Least Knowledge, the Law of Demeter or the Information Expert pattern, what&#8217;s important is that you understand and apply the essence behind it.&#160; It&#8217;s all about doing object-oriented development right.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/11/02/evolving-your-design-with-the-principle-of-least-knowledge/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The road towards MCPD-EAD: I passed exam 70-528, 70-526 and 70-549. Mission Accomplished!</title>
		<link>http://blog.briandicroce.com/2008/10/22/the-road-towards-mcpd-ead-i-passed-exam-70-528-70-526-and-70-549-mission-accomplished/</link>
		<comments>http://blog.briandicroce.com/2008/10/22/the-road-towards-mcpd-ead-i-passed-exam-70-528-70-526-and-70-549-mission-accomplished/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 01:51:00 +0000</pubDate>
		<dc:creator>Brian Di Croce</dc:creator>
				<category><![CDATA[Certifications]]></category>

		<guid isPermaLink="false">http://blog.briandicroce.com/2008/10/22/the-road-towards-mcpd-ead-i-passed-exam-70-528-70-526-and-70-549-mission-accomplished/</guid>
		<description><![CDATA[Some readers were asking me about the status of my self-imposed challenge to achieve the MCPD-EAD (Microsoft Certified Professional Developer: Enterprise Application Developer) certification before the end of the year.&#160; Well, it turns out that I’ve completed and passed the last exam of the series this morning.&#160; When I first started out with this insane [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 10px 0px; border-right-width: 0px" height="79" alt="image" src="http://blog.briandicroce.com/wp-content/uploads/2008/10/image9.png" width="328" align="left" border="0" />Some readers were asking me about the status of my self-imposed challenge to achieve the MCPD-EAD (Microsoft Certified Professional Developer: Enterprise Application Developer) certification before the end of the year.&#160; Well, it turns out that I’ve completed and passed the last exam of the series this morning.&#160; When I first started out with this insane challenge of doing one certification every three to four weeks, I had no idea of the amount of time I would have to dedicate to complete it in the initial timeframe.&#160; So just to recap, I passed <a href="http://www.microsoft.com/learning/exams/70-536.mspx">70-536</a> (Fundamentals) back in <strong>June</strong>, <a href="http://www.microsoft.com/learning/en/us/exams/70-529.mspx">70-529</a> (Distributed Applications) in <strong>July</strong>, <a href="http://www.microsoft.com/learning/exams/70-528.mspx">70-528</a> (Web Applications) in <strong>August</strong>, <a href="http://www.microsoft.com/learning/en/us/exams/70-526.mspx">70-526</a> (Windows Applications) in <strong>September</strong> and <a href="http://www.microsoft.com/learning/exams/70-549.mspx">70-549</a> (Design and Development) this morning (<strong>October</strong>).&#160; All of these exams focus on various technologies targeting .NET 2.0.</p>
<p>For each of these exams, I have used the “self-paced” book series (the same series used for 70-536 and 70-529), which are:</p>
<ul>
<li><a href="http://www.microsoft.com/mspress/books/9982.aspx">MCTS Self-Paced Training Kit (Exam 70-528): Microsoft .NET Framework 2.0 Web-Based Client Development</a></li>
<li><a href="http://www.microsoft.com/mspress/books/9983.aspx">MCTS Self-Paced Training Kit (Exam 70-526): Microsoft .NET Framework 2.0 Windows-Based Client Development</a></li>
<li><a href="http://www.microsoft.com/mspress/books/10094.aspx">MCPD Self-Paced Training Kit (Exam 70-549): Designing and Developing Enterprise Applications Using the Microsoft .NET Framework</a></li>
</ul>
<p>Now, I’m going to say it straight and simple: all of these books suck.&#160; The only good thing about them is that the authors have done a pretty good job copy/pasting the right MSDN documentation so that we wouldn’t waste our time doing it ourselves.&#160; The major drawbacks I have against these books are the <em>really</em> poor coding style of the authors towards their examples.&#160; A big portion of the examples contain errors, are sometimes misleading and some of them clearly won’t compile if you copy/paste the code in Visual Studio.&#160;&#160; It’s sad to see so many mistakes like these, especially when each book is written by so many authors, because the credibility of these individuals take a pretty bad hit in the public’s eye.&#160; I’m sure they were pushed by the deadline, but I wished they’ve polished the examples a little bit more.&#160; I know…I know…Nobody’s perfect…I know.&#160; But that’s still no reason to deliver mediocre products.</p>
<p>All right.&#160; So just in case you’re thinking (or already started) to get your MCPD-EAD credentials, I am going to tell you how I’ve managed my time to pass each of these exams in hope that it might help you to pass them successfully.&#160; In average, I dedicated about 40 hours per exam:</p>
<ul>
<ul>
<li>15 hours to read the book (1 hour in the morning and 1 hour in the evening should be enough for you to read the whole book within a week.&#160; You don’t have to read EVERYTHING.&#160; If you’re proficient in C#, then skip all the VB.NET examples.&#160; Same idea goes if you’re more proficient in VB.NET than C#.&#160;&#160; Whenever the authors mentions something in a box with the title “Real-World Scenario” you SKIP it.&#160; It doesn’t matter.&#160; The “real-world scenario” isn’t in the book…it’s in the REAL WORLD.&#160; Don’t waste your time.&#160; Also, don’t do all of the activities at the end of each chapter.&#160; Just get through the book because you don’t want to waste too much of your time.&#160; Here we’re really trying to cram our brain with tons of information.&#160; The exams are roughly 80% based on memorization of the content and 20% on actual thinking.&#160; Feel free to write notes and underline important ideas as you read along.&#160; I used to write “EXAM” in red ink on the margin so that I can review it at a later time.) </li>
<li>20 hours to practice with <a href="http://www.transcender.com/">Transcender</a> or <a href="http://www.measureup.com/">MeasureUp</a> exams.&#160; I have only used Transcenders, so I can’t say much about MeasureUp.&#160; I’ve heard they’re quite good too.&#160; Whichever you decide to do, I strongly suggest you to do all of the questions more than once.&#160; Then when you’re confident enough about the material, take a moment to answer the questions in the “certification” mode to give you a better feel for the exam.&#160; By the way, none of the questions of either Transcenders or MeasureUp were in the exams I’ve taken.&#160; To be honest, the mock exams’ goals are to deepen your knowledge with the various technologies.&#160; That’s it, that’s all.&#160; Don’t put too much confidence in them, because what I’ve concluded at the end of each real exam is that the mock exams only helped me about 20% on the “knowledge” stuff and 80% on the actual “feeling” for the exam.&#160; Take a week or two to practice these.</li>
<li>5 hours to practice some things I didn’t fully understand (proper use of some class, understanding the use behind some interfaces, getting comfortable with the security model, etc.) inside Visual Studio.&#160; I strongly suggest you use <a href="http://www.red-gate.com/products/reflector/">Reflector</a> to assist you in quickly discovering the information behind some objects in the framework.</li>
</ul>
</ul>
<p>I also suggest you to book your exam in advance.&#160; For example, suppose you start studying for an exam today, then you will book an exam date four weeks from now.&#160; This should motivate you to stay on course and respect your schedule.&#160; And if that’s not enough, then mention to your boss and your fellow co-workers that you intend to pass this exam by that date.&#160; Feel free to also share you exam date as a comment on this post! That should put enough pressure to get the job done…wouldn’t it? <img src='http://blog.briandicroce.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In closing, I found that the 70-526 (Windows Applications) exam was the easiest exam of the bunch.&#160; As for the hardest, I must say that it was the 70-529 (Distributed Applications) exam, mostly because I’ve never done .NET Remoting in the past.&#160; And now that WCF is in the neighborhood, I doubt I’ll even have to touch .NET Remoting in the future!&#160; It all depends on your experience with these technologies.&#160; The more you practice and learn, the greater your chance in passing.</p>
                        <p><br />
My name is Brian Di Croce, a passionate software developer living, coding, blogging and working in Montreal, Canada.  Feel free to visit my <a href="http://www.briandicroce.com">website/blog</a> for more great content related to software development with .NET! &copy;2007-2008<br />
</p>                        ]]></content:encoded>
			<wfw:commentRss>http://blog.briandicroce.com/2008/10/22/the-road-towards-mcpd-ead-i-passed-exam-70-528-70-526-and-70-549-mission-accomplished/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
