<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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" version="2.0">

<channel>
	<title>The .Net frog</title>
	
	<link>http://www.thedotnetfrog.com</link>
	<description>Thoughts (ok, rants!) about programming and .NET from the land of frogs!</description>
	<pubDate>Fri, 30 May 2008 19:43:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/thedotnetfrog" type="application/rss+xml" /><item>
		<title>Choosing composition over inheritance: yet another example!</title>
		<link>http://www.thedotnetfrog.com/2008/05/30/choosing-composition-over-inheritance-yet-another-example/</link>
		<comments>http://www.thedotnetfrog.com/2008/05/30/choosing-composition-over-inheritance-yet-another-example/#comments</comments>
		<pubDate>Thu, 29 May 2008 23:01:08 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[code quality]]></category>

		<category><![CDATA[composition]]></category>

		<category><![CDATA[inheritance]]></category>

		<category><![CDATA[SoC]]></category>

		<guid isPermaLink="false">http://www.thedotnetfrog.com/?p=55</guid>
		<description><![CDATA[A friend of mine recently asked me if I though that having an inheritance hierarchy with a depth of 10 classes was acceptable or not. I don't think it is. If you take a class at the bottom, any change to the 9 classes above can introduce a breaking change. It can quickly become a [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Choosing composition over inheritance: yet another example!", url: "http://www.thedotnetfrog.com/2008/05/30/choosing-composition-over-inheritance-yet-another-example/" });</script>]]></description>
			<content:encoded><![CDATA[<p>A friend of mine recently asked me if I though that having an inheritance hierarchy with a depth of 10 classes was acceptable or not. I don't think it is. If you take a class at the bottom, any change to the 9 classes above can introduce a breaking change. It can quickly become a maintenance nightmare! </p>
<p>Most of the time, if you have that kind of hierarchy in your application, it means that you chose inheritance over composition. It's probably also a code-smell indicating that your class is doing too much. To avoid that, let me show you how it is possible to extend a class without using inheritance.</p>
<p>Let's assume that we start with the following code. I know it's very basic, however it's still enough for our demonstration.</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">abstract <span style="color: #FF0000;">class</span> TripBase</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> _from;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> _to;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> From</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _from; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> To</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _to; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">protected</span> TripBase<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_from = from;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_to = to;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> abstract DateTime CalculateEstimatedArrivalTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> CarTrip : TripBase</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> CarTrip<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		: <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>from, to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> DateTime CalculateEstimatedArrivalTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">return</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">AddHours</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">15</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> PlaneTrip : TripBase</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> PlaneTrip<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		: <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>from, to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> DateTime CalculateEstimatedArrivalTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">return</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">AddHours</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>We have a BaseTrip abstract class and we want to extend it by having 2 sub classes: CarTrip and PlaneTrip. In a real application, we would have some kind of algorithm to calculate or fetch the transportation time and it would probably be very different for each class. In this example, to simplify, we just return a hard coded value.<br />
This code is not bad, however it's not perfect either. For instance, are we sure that calculating the transportation time between 2 city is the responsibility of the CarTrip and the PlaneTrip class? As a matter of fact, if we want to add a new MotocycleTrip class that would have different properties but the same algorithm than CarTrip to calculate the transportation time, we would need to extract a new abstract superclass that we would probably call RoadTrip... It doesn't look good...</p>
<p>Here is a refactored version of these class, using composition instead of inheritance:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> Trip</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> _from;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> _to;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> ITransportationMode _transportationMode;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> From</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _from; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> To</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _to; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">protected</span> Trip<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to, ITransportationMode transportationMode<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_from = from;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_to = to;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_transportationMode = transportationMode;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> DateTime CalculateEstimatedArrivalTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">return</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_transportationMode.<span style="color: #0000FF;">CalculateTransportationTimeBetween</span><span style="color: #000000;">&#40;</span>_from, _to<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">interface</span> ITransportationMode</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	TimeSpan CalculateTransportationTimeBetween<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> CarTransportationMode : ITransportationMode</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> TimeSpan CalculateTransportationTimeBetween<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">15</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> PlaneTransportationMode : ITransportationMode</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> TimeSpan CalculateTransportationTimeBetween<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> to<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>In a few words, I've extracted the calculations in their own classes that implement an ITransportationMode interface. They are injected in the Trip class through the constructor (but we could also use a setter injection or a method injection).</p>
<p>This implementation improves the code in several way:</p>
<ul>
<li>The calculation of the transportation time is done in a new class, it's not polluting our Trip class any more.  It's a lot easier to add new algorithms, we just need to add a new implementation of ITransportationMode. It can evolves independently of the Trip class. It's called the separation of concern principle.</li>
<li>We can more easily configure our Trip class. Before, wa had to create a new instance of a TripBase sub class to change the way the calculation was done, now we can just change the instance of ITransportationMode while keeping the same Trip object.</li>
<li>We can have several subclasses of Trip using the same algorithm without introducing a new abstract class (such as RoadTrip). Therefore, composition helps us to keep our hierarchy of class flat.</li>
</ul>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.thedotnetfrog.com%2f2008%2f05%2f30%2fchoosing-composition-over-inheritance-yet-another-example%2f" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.thedotnetfrog.com%2f2008%2f05%2f30%2fchoosing-composition-over-inheritance-yet-another-example%2f');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.thedotnetfrog.com%2f2008%2f05%2f30%2fchoosing-composition-over-inheritance-yet-another-example%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Choosing+composition+over+inheritance%3A+yet+another+example%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F05%2F30%2Fchoosing-composition-over-inheritance-yet-another-example%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Choosing+composition+over+inheritance%3A+yet+another+example%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F05%2F30%2Fchoosing-composition-over-inheritance-yet-another-example%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/05/30/choosing-composition-over-inheritance-yet-another-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The null singleton</title>
		<link>http://www.thedotnetfrog.com/2008/05/24/the-null-singleton/</link>
		<comments>http://www.thedotnetfrog.com/2008/05/24/the-null-singleton/#comments</comments>
		<pubDate>Sat, 24 May 2008 13:08:00 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[code quality]]></category>

		<guid isPermaLink="false">http://www.thedotnetfrog.com/?p=44</guid>
		<description><![CDATA[I'll say it straight: I really don't like the Singleton pattern. I think this pattern was great a few years ago, before we started to use things such as dependency injection or dependency lookup. Unfortunately for me, as of today, it's probably the most used pattern in software development. To be fair, this pattern can [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "The null singleton", url: "http://www.thedotnetfrog.com/2008/05/24/the-null-singleton/" });</script>]]></description>
			<content:encoded><![CDATA[<p>I'll say it straight: I really don't like the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Singleton_pattern');">Singleton pattern</a>. I think this pattern was great a few years ago, before we started to use things such as <a href="http://martinfowler.com/articles/injection.html" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://martinfowler.com/articles/injection.html');">dependency injection</a> or <a href="http://xunitpatterns.com/Dependency%20Lookup.html" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://xunitpatterns.com/Dependency%20Lookup.html');">dependency lookup</a>. Unfortunately for me, as of today, it's probably the most used pattern in software development. To be fair, this pattern can be useful in several scenarios, but please... not everywhere! (For the record, the a singleton is a pattern that ensures that you have only one instance for a specific class. This instance is created "on demand", the first time the class is used.)</p>
<p>The main problems with singletons are obvious:</p>
<ul>
<li>You don't control when an instance is created, so if you have lot of them you have no idea in which order the instances will be created. Not an issue on small software, but more worrying when you have a few hundred thousands of lines.</li>
<li>99% of the time, the implementation that is used binds us to a specific class. Therefore you have either to refactor the singleton or use a tool like <a href="http://www.typemock.com/" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.typemock.com/');">TypeMock</a> if you want to unit test your code. (Or you can have a empty class that holds the static instance and returns it casted as an interface... But doing that results in a multiplication of almost useless classes)</li>
<li>Dependencies are not obvious (which can also be a problem with dependency lookup). If you have type A that uses a singleton of type B, nothing in A signature will show you the dependency. It might be a problem if you're not aware that using A results in a hundred of round trips to the database through B!</li>
</ul>
<p>However, I recently discovered a pattern that is even worse than the Singleton: I call it the "Null Singleton"! It's a very simple concept: the singleton can fail to instantiate itself if some dependency (a config file, a database, or whatever) is not available. In that case, the instance returned by the singleton will be null. Let's see an example:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> NullSingleton</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> NullSingleton _instance;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> NullSingleton Instance</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        get</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_instance == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>CanAccessMyDependency<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                    _instance = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> NullSingleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0600FF;">return</span> _instance;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> CanAccessMyDependency<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #008080; font-style: italic;">// check if you can connect to the database for instance.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #0600FF;">private</span> NullSingleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p><strong>So what is wrong with that?</strong></p>
<ul>
<li>If the dependency is unavailable, the NullSingleton will check again the dependency each time something will access it. It will probably be very slow (usually, it will be an out of process call).</li>
<li>Each time you try to get an instance, you'll have to check for a null reference which makes the code noisy</li>
<li>And finally, it's breaking the usual expectation about singletons (that it should return 1 instance)
</ul>
<p>Do yourself a favor: don't use a "Null Singleton" :-)</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.thedotnetfrog.com%2f2008%2f05%2f24%2fthe-null-singleton%2f" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.thedotnetfrog.com%2f2008%2f05%2f24%2fthe-null-singleton%2f');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.thedotnetfrog.com%2f2008%2f05%2f24%2fthe-null-singleton%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=The+null+singleton&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F05%2F24%2Fthe-null-singleton%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=The+null+singleton&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F05%2F24%2Fthe-null-singleton%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/05/24/the-null-singleton/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Spec#: should we really wait for it?</title>
		<link>http://www.thedotnetfrog.com/2008/05/13/spec-should-we-really-wait-for-it/</link>
		<comments>http://www.thedotnetfrog.com/2008/05/13/spec-should-we-really-wait-for-it/#comments</comments>
		<pubDate>Tue, 13 May 2008 17:52:24 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[code quality]]></category>

		<category><![CDATA[dbc]]></category>

		<category><![CDATA[defensive programming]]></category>

		<category><![CDATA[Spec#]]></category>

		<guid isPermaLink="false">http://www.thedotnetfrog.com/?p=49</guid>
		<description><![CDATA[A friend of mine just posted about his desire to have Spec# ready for production. Funnily, many people in Alt.net expressed a similar desire in the past weeks (See that or that).
I don't have any problem with Spec#. On the contrary, I believe that it's a great addition to the language, one that would result [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Spec#: should we really wait for it?", url: "http://www.thedotnetfrog.com/2008/05/13/spec-should-we-really-wait-for-it/" });</script>]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://nomorehacks.wordpress.com/2008/05/13/spec-a-framework-i-can-get-behind/" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://nomorehacks.wordpress.com/2008/05/13/spec-a-framework-i-can-get-behind/');">friend of mine</a> just posted about his desire to have Spec# ready for production. Funnily, many people in Alt.net expressed a similar desire in the past weeks (See <a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/04/28/making-spec-a-priority.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://codebetter.com/blogs/matthew.podwysocki/archive/2008/04/28/making-spec-a-priority.aspx');">that</a> or <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/28/i-want-spec.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://codebetter.com/blogs/gregyoung/archive/2008/04/28/i-want-spec.aspx');">that</a>).</p>
<p>I don't have any problem with <a href="http://research.microsoft.com/SpecSharp/" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://research.microsoft.com/SpecSharp/');">Spec#</a>. On the contrary, I believe that it's a great addition to the language, one that would result in a significant improvement in code quality. However, I also don't think we should expect anything soon for various reasons:</p>
<ul>
<li>Spec# is only a research project. Even if Microsoft did publish previews, I don't think they've communicated any plan to have a RTM yet.</li>
<li>Spec# is an addition to C#. However, there's now a lot more than C# in .NET, and I think that it would be problematic to add this feature to C# and not to vb.net for instance (which is, believe it or not, more used than C#). Actually, what happens if you build an assembly with Spec# and use it from C#/Vb.net? I don't have any clue but it would be interesting to know.</li>
<li>It will be difficult for Spec# to enter mainstream, enterprise will be reluctant to migrate to it. Moving from .NET 2.0 to .NET 3.5 is absolutely painless and still, do you know many companies that are actively working with .NET 3.5?</li>
<li>Spec# adds several new keywords which will be as many new concepts to understand before being able to work with it. With the recent additions to the framework, I'm starting to consider that there's already too much to learn for the average developer! Just an example: how many of your colleagues know what's the meaning of the "??" operator in C#? Or another one: I had to explain what was a Nullable only a few days ago :). If you add Linq, WCF, WPF, Entity Framework...</li>
</ul>
<p>The bottom line is that I believe that we should focus our efforts on improving the way we do <a href="http://en.wikipedia.org/wiki/Defensive_programming" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Defensive_programming');">defensive programming</a> with the framework as it is, and not hope for a white knight. Even if they are not perfect solutions, I think we can cover a lot of ground by systematically checking the inputs, checking the post conditions/side effects with unit testing, and checking that our object are <a href="http://www.thedotnetfrog.com/2008/04/12/keep-your-objects-in-a-consistent-state/" target="_blank" >satisfying invariants</a>.</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Spec%23%3A+should+we+really+wait+for+it%3F&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F05%2F13%2Fspec-should-we-really-wait-for-it%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Spec%23%3A+should+we+really+wait+for+it%3F&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F05%2F13%2Fspec-should-we-really-wait-for-it%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/05/13/spec-should-we-really-wait-for-it/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Performance impact of the readonly keyword</title>
		<link>http://www.thedotnetfrog.com/2008/04/22/performance-impact-of-the-readonly-keyword/</link>
		<comments>http://www.thedotnetfrog.com/2008/04/22/performance-impact-of-the-readonly-keyword/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 06:04:35 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[clr]]></category>

		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.thedotnetfrog.com/?p=46</guid>
		<description><![CDATA[I've heard from a co-worker that the readonly keyword was optimizing memory access and therefore quicker than a normal field access. I must admit that I found this assertion a bit surprising. My first bet would have been that the performance are identical with or without it (it's just a compiler check, but it doesn't [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Performance impact of the readonly keyword", url: "http://www.thedotnetfrog.com/2008/04/22/performance-impact-of-the-readonly-keyword/" });</script>]]></description>
			<content:encoded><![CDATA[<p>I've heard from a co-worker that the readonly keyword was optimizing memory access and therefore quicker than a normal field access. I must admit that I found this assertion a bit surprising. My first bet would have been that the performance are identical with or without it (it's just a compiler check, but it doesn't result in any change at execution). If wrong, I then would have bet on a slower access (I would expect the CLR to do some checks when accessing the field). Let's see if I'm mistaken :).</p>
<p>Unfortunately for me, I didn't find anything to confirm or not my theories, so I decided to do my own micro-benchmark.</p>
<p>I measured the time spent for 3 main scenarios, with a read only field and without it:<br />
- Scenario 1: Creating an object<br />
- Scenario 2: Accessing the field<br />
- Scenario 3: Creating an object and accessing the field</p>
<p>I executed this code 1,000,000,000 times for each scenario and ran each scenario 10 times on 4 different PC to get a meaningful average. For each test, I excluded the top and bottom result.<br />
The code (in C#, .NET 2.0) is available <a href="http://www.thedotnetfrog.com/resources/readonly_benchmark/Program.cs" >here</a> (It is duplicated for each test, I know it's horrible, my apologies :)), and the results <a href="www.thedotnetfrog.com/resources/readonly_benchmark/benchmark.xls">here</a>.</p>
<p>Each test looks like that (with sligh modifications of course):</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_streamWriter.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_streamWriter.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Obj creation &amp; access in loop&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j = <span style="color: #FF0000;">0</span>; j &lt; _numberOfTestExecutions; j++<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Poco poco = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> Poco<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Stopwatch watch = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> Stopwatch<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	watch.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i = <span style="color: #FF0000;">0</span>; i &lt; _numberOfIterationPerTest; i++<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		ReadOnlyField objReadOnlyField = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> ReadOnlyField<span style="color: #000000;">&#40;</span>poco<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		Poco myCopyOfPoco = objReadOnlyField.<span style="color: #0000FF;">Poco</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	watch.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	_streamWriter.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>watch.<span style="color: #0000FF;">ElapsedMilliseconds</span> + <span style="color: #808080;">&quot;;&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	watch.<span style="color: #0000FF;">Reset</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	watch.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i = <span style="color: #FF0000;">0</span>; i &lt; _numberOfIterationPerTest; i++<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		ReadWriteField objReadWriteField = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> ReadWriteField<span style="color: #000000;">&#40;</span>poco<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		Poco myCopyOfPoco = objReadOnlyField.<span style="color: #0000FF;">Poco</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	watch.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	_streamWriter.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>watch.<span style="color: #0000FF;">ElapsedMilliseconds</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p><strong>Here are the raw numbers:</strong></p>
<p>Scenario 1: Creating an object<br />
- read only: 25172ms<br />
- read write: 24983ms<br />
Read only is in average 0.76% slower than read write.</p>
<p>Scenario 2: Accessing the field<br />
- read only: 6437ms<br />
- read write: 6449ms<br />
Read only is in average 0.19% quicker than read write.</p>
<p>Scenario 3: Creating an object and accessing the field<br />
- read only: 19929ms<br />
- read write: 19761ms<br />
Read only is in average 0.85% slower than read write.</p>
<p><strong>However, you can see greater differences by looking at the results for each computer. For instance, on the first PC I used (a core 2 duo, 2,8Ghz, DDR2 800Mhz),  I have the following results:</strong></p>
<p>- Scenario 1: Creating an object<br />
Read only is in average <strong>5.72% quicker</strong> than read write.<br />
- Scenario 2: Accessing the field<br />
Read only is in average <strong>0.94% quicker</strong> than read write.<br />
- Scenario 3: Creating an object and accessing the field<br />
Read only is in average <strong>2.28% quicker</strong> than read write.</p>
<p>And on a laptop (centrino 1,7ghz, DDR):<br />
- Scenario 1: Creating an object<br />
Read only is in average <strong>6.34% slower</strong> than read write.<br />
- Scenario 2: Accessing the field<br />
Read only is in average <strong>0.21% quicker</strong> than read write.<br />
- Scenario 3: Creating an object and accessing the field<br />
Read only is in average <strong>3.01% slower</strong> than read write.</p>
<p><strong>My (own) conclusions:</strong><br />
There seem to be a difference of performance involved by using the readonly keyword. The problem is that the impact is highly dependant on the hardware. On a PC with decent hardware, accessing a read only field is between 1% and 2.5% quicker than accessing a normal field. Object creation can be as much as 6% quicker. On lower-end hardware, the results are the opposite.<br />
However, the tests have been made with very simple class, so keep in mind that in reality, the speed difference in object creation would have been smaller with classes that have more members.</p>
<p>As far as I'm concerned, I would not used the readonly keyword just for optimization purposes except for very demanding situations, and only after checking that it's actually quicker on the specifics machines where the software is going to run. So unless your application is:<br />
- running on a very controlled environment with a very limited set of installations<br />
- pseudo real-time or extremely sensible to performances<br />
I don't think you should even wonder about the performance impact of readonly. There's probably hundreds of optimization that will be more effective before this one!</p>
<p>Finally, if you have any clue showing a different behaviour, please tell me about it and I will edit this post in consequence :). I'm specially interested in having benchmark with Xeon processors (unfortunately, I don't have access to one)</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Performance+impact+of+the+readonly+keyword&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F22%2Fperformance-impact-of-the-readonly-keyword%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Performance+impact+of+the+readonly+keyword&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F22%2Fperformance-impact-of-the-readonly-keyword%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/04/22/performance-impact-of-the-readonly-keyword/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Keep your objects in a consistent state</title>
		<link>http://www.thedotnetfrog.com/2008/04/12/keep-your-objects-in-a-consistent-state/</link>
		<comments>http://www.thedotnetfrog.com/2008/04/12/keep-your-objects-in-a-consistent-state/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 12:17:52 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[General development]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[code quality]]></category>

		<guid isPermaLink="false">http://www.thedotnetfrog.com/?p=43</guid>
		<description><![CDATA[Part of a good object oriented design is to keep the objects in a correct state.
For instance, in the financial world, we have financial instruments called "derivatives". According to Wikipedia:
Derivatives are financial instruments whose value changes in response to the changes in underlying variables. The main types of derivatives are futures, forwards, options, and swaps.
In [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Keep your objects in a consistent state", url: "http://www.thedotnetfrog.com/2008/04/12/keep-your-objects-in-a-consistent-state/" });</script>]]></description>
			<content:encoded><![CDATA[<p>Part of a good object oriented design is to keep the objects in a correct state.</p>
<p>For instance, in the financial world, we have financial instruments called "derivatives". According to <a href="http://en.wikipedia.org/wiki/Derivative_(finance)" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Derivative_(finance)');">Wikipedia</a>:</p>
<blockquote><p>Derivatives are financial instruments whose value changes in response to the changes in underlying variables. The main types of derivatives are futures, forwards, options, and swaps.</p></blockquote>
<p>In the real world, a derivative can't exist if there isn't an underlying financial instrument associated. A <a href="http://en.wikipedia.org/wiki/Futures_contract" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Futures_contract');">future</a> on the dow jones could not have been created without the dow jones in the first place. If we want to represent a derivative in a software, we must make sure that we follow the same rule.</p>
<p>So let's assume that I have the following class:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">class</span> Derivative</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _name;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">private</span> Instrument _underlyingInstrument ;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _name; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> Instrument UnderlyingInstrument</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _underlyingInstrument; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">public</span> Derivative<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, Instrument underlyingInstrument<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_name = name;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_underlyingInstrument = underlyingInstrument;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>Any new developer on a project that uses this Derivative class will mentally map the Derivative class to the corresponding financial concept. He will expect the UnderlyingInstrument property to return a non-null object. However, this is not guaranteed in the current implementation. As a matter of fact, this class can currently be used to only convey the name of a Derivative. If we wanted to do that, we would need to create a new class for that purpose only. So in the mean time, if we want our code to stay in a maintainable state, we need to make sure that each Derivative object will be constructed correctly. If not, different people will make different usage of the class.</p>
<p>In that case, ensuring the correctness of the object can be done very easily. We just need to do a bit of <a href="http://en.wikipedia.org/wiki/Design_by_contract" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Design_by_contract');">Design By Contract</a>. So our constructor will become:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> Derivative<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, Instrument underlyingInstrument<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>name == <span style="color: #0600FF;">null</span> || name.<span style="color: #0000FF;">Length</span> == <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;The name of the derivative can't be null or empty&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>underlyingInstrument == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;A derivative must have an underlying instrument&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        _name = name;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        _underlyingInstrument = underlyingInstrument ;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>Now you're sure that a fellow developer won't use your object to do a weird thing :).</p>
<p>Of course, you can improve the clarity of this piece of code significantly by using various techniques and frameworks(including Debug.Assert or your own Assert class). For instance, I would write something like that:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> Derivative<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, Instrument underlyingInstrument <span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Guard.<span style="color: #0000FF;">Against</span><span style="color: #000000;">&#40;</span>name == <span style="color: #0600FF;">null</span>, <span style="color: #808080;">&quot;The name of the derivative can't be null&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Guard.<span style="color: #0000FF;">Against</span><span style="color: #000000;">&#40;</span>name.<span style="color: #0000FF;">Length</span> == <span style="color: #FF0000;">0</span>, <span style="color: #808080;">&quot;The name of the derivative can't be empty&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Guard.<span style="color: #0000FF;">Against</span><span style="color: #000000;">&#40;</span>underlyingInstrument == <span style="color: #0600FF;">null</span>, <span style="color: #808080;">&quot;The underlying instrument of the derivative can't be null&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	_name = name;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	_underlyingInstrument = underlyingInstrument;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>But that's a topic for another day!</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Keep+your+objects+in+a+consistent+state&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F12%2Fkeep-your-objects-in-a-consistent-state%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Keep+your+objects+in+a+consistent+state&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F12%2Fkeep-your-objects-in-a-consistent-state%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/04/12/keep-your-objects-in-a-consistent-state/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AutoResetEvent vs ManualResetEvent: beware!</title>
		<link>http://www.thedotnetfrog.com/2008/04/07/autoresetevent-vs-manualresetevent-beware/</link>
		<comments>http://www.thedotnetfrog.com/2008/04/07/autoresetevent-vs-manualresetevent-beware/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 09:12:52 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[multithreading]]></category>

		<guid isPermaLink="false">http://www.lavigneducadet.com/wordpress/?p=37</guid>
		<description><![CDATA[I did load testing last week on the component I am developing and found out that the performances were just miserable... It was barely capable of handling 60 messages per seconds which was: 1) bad and 2) surprising! (Basically it's doing some kind of real-time caching/transformation/redirection of messages).
I then spent 5mins in dotrace trying to [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "AutoResetEvent vs ManualResetEvent: beware!", url: "http://www.thedotnetfrog.com/2008/04/07/autoresetevent-vs-manualresetevent-beware/" });</script>]]></description>
			<content:encoded><![CDATA[<p>I did load testing last week on the component I am developing and found out that the performances were just miserable... It was barely capable of handling 60 messages per seconds which was: 1) bad and 2) surprising! (Basically it's doing some kind of real-time caching/transformation/redirection of messages).</p>
<p>I then spent 5mins in dotrace trying to get a picture of what was going on. I found that in the thread that is monitoring the queue and sending messages, I had used an instance of ManualResetEvent instead of AutoResetEvent. </p>
<p>If you never used them, these 2 classes allow you to send signals between 2 threads. That way, the "monitor" thread is not wasting any resources until it's notified by the other thread that there is something in the queue. For instance, Thread 1 will wait for a queue to be filled with something like that:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> AutoResetEvent _mySignal = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> AutoResetEvent<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> MonitorQueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>!monitorQueue<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		_mySignal.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		GetItemsInTheQueueAndDoStuff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>And thread 2 will inject data in the queue:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> EnqueueItem<span style="color: #000000;">&#40;</span>Item myItem<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	InsertInQueue<span style="color: #000000;">&#40;</span>myItem<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	_mySignal.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>As I said, my problem is that I used a ManualResetEvent instead of an AutoResetEvent. These 2 classes are almost the same except that when you use ManualResetEvent, you need to reset the signal manually with mySignal.Reset();. In my case, instead of blocking on mySignal.WaitOne(); the code was constantly looping and using a lot of CPU!</p>
<p>I fixed it and reran the load testing: now I'm at 5000 messages per seconds at 40% CPU. Much better!</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=AutoResetEvent+vs+ManualResetEvent%3A+beware%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F07%2Fautoresetevent-vs-manualresetevent-beware%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=AutoResetEvent+vs+ManualResetEvent%3A+beware%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F07%2Fautoresetevent-vs-manualresetevent-beware%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/04/07/autoresetevent-vs-manualresetevent-beware/feed/</wfw:commentRss>
		</item>
		<item>
		<title>lock(this): don’t!</title>
		<link>http://www.thedotnetfrog.com/2008/04/04/lockthis-dont/</link>
		<comments>http://www.thedotnetfrog.com/2008/04/04/lockthis-dont/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 19:53:11 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[multithreading]]></category>

		<guid isPermaLink="false">http://www.lavigneducadet.com/wordpress/?p=28</guid>
		<description><![CDATA[I've seen that kind of things in several codebases recently:
lock&#40;this&#41;&#123;    // Do stuff...&#125;
Even if it perfectly works, this is a bad idea. You should never (or at least I can't think of a good reason!) lock on a public type, therefore including "this". There is a simple reason: you don't know what [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "lock(this): don&#8217;t!", url: "http://www.thedotnetfrog.com/2008/04/04/lockthis-dont/" });</script>]]></description>
			<content:encoded><![CDATA[<p>I've seen that kind of things in several codebases recently:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #008080; font-style: italic;">// Do stuff...</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>Even if it perfectly works, this is a bad idea. You should never (or at least I can't think of a good reason!) lock on a public type, therefore including "this". There is a simple reason: you don't know what expectations the caller is doing.</p>
<p>Let's take a simple example. In the following code, we have a class that uses a lock on this (ClassThatLocksItself) and another class that is going to call it (CallerClass). When CallerClass calls ClassThatLocksItself, it's going to lock on the instance of ClassThatLocksItself.</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #FF0000;">class</span> Program</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            ClassThatLocksItself myObj = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> ClassThatLocksItself<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            CallerClass caller = <a href="http://www.google.com/search?q=new+msdn.microsoft.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.google.com/search?q=new+msdn.microsoft.com');"><span style="color: #008000;">new</span></a> CallerClass<span style="color: #000000;">&#40;</span>myObj<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            caller.<span style="color: #0000FF;">LockTheObjectInAThread</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            myObj.<span style="color: #0000FF;">LockMe</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #FF0000;">class</span> CallerClass</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0600FF;">private</span> ClassThatLocksItself _myObj;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0600FF;">public</span> CallerClass<span style="color: #000000;">&#40;</span>ClassThatLocksItself myObj<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            _myObj = myObj;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> LockTheObjectInAThread<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            ThreadPool.<span style="color: #0000FF;">QueueUserWorkItem</span><span style="color: #000000;">&#40;</span>LockTheObject<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> LockTheObject<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> state<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Acquiring lock on the object&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0600FF;">lock</span> <span style="color: #000000;">&#40;</span>_myObj<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">100000</span><span style="color: #000000;">&#41;</span>; <span style="color: #008080; font-style: italic;">// Do a long computation</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Releasing lock on the object&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ClassThatLocksItself</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> LockMe<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;ClassThatLocksItself -- Trying to acquire lock on this&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0600FF;">lock</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;ClassThatLocksItself -- lock on this acquired&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;ClassThatLocksItself -- lock on this released&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>If you try to execute this code, you'll notice that Console.WriteLine("ClassThatLocksItself -- lock on this acquired"); is only executed when LockTheObject() returns (here it takes 100 seconds). By using lock(this), you became dependant on externals and unknowns factors (in that case, the caller of your code decided to lock on your class). The situation is even a lot worse for developers who want to reuse  your ClassThatLocksItself: they have no way of knowing that there can be a synchronisation problem unless they read the code of your class!</p>
<p>Now, try to guess what is going to happen if you change the implementation of LockTheObject with the following:</p>
<pre class="csharp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> LockTheObject<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> state<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Acquiring lock on the object&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0600FF;">lock</span> <span style="color: #000000;">&#40;</span>_myObj<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                _myObj.<span style="color: #0000FF;">LockMe</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008080; font-style: italic;">// will not lock!</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Releasing lock on the object&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000;">&#125;</span></div></li></ol></pre>
<p>Most of you will bet on a deadlock I guess. However, the CLR is intelligent enough to detect that when LockMe executes lock(this), the lock was already acquired by the caller. Therefore, it doesn't block on it. It makes the whole lock(this) thing very subtile: you'll only see a problem in some specific cases.</p>
<p><span id="more-28"></span></p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=lock%28this%29%3A+don%26%238217%3Bt%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F04%2Flockthis-dont%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=lock%28this%29%3A+don%26%238217%3Bt%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F04%2Flockthis-dont%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/04/04/lockthis-dont/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Yet another blogger on earth!</title>
		<link>http://www.thedotnetfrog.com/2008/04/04/yet-another-blogger-on-earth/</link>
		<comments>http://www.thedotnetfrog.com/2008/04/04/yet-another-blogger-on-earth/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 19:51:55 +0000</pubDate>
		<dc:creator>Julien</dc:creator>
		
		<category><![CDATA[various stuff]]></category>

		<category><![CDATA[welcome]]></category>

		<guid isPermaLink="false">http://www.thedotnetfrog.com/?p=42</guid>
		<description><![CDATA[Hi guys, welcome on this new blog!
Like many people, I learned a lot by reading posts from various very good writers (Think Jeremy Miller, JP Boodhoo, Martin Fowler, and dozens of others!). Since I discovered their blogs, their knowledge has always been useful to me. Even if I don't know them personally, they helped me [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Yet another blogger on earth!", url: "http://www.thedotnetfrog.com/2008/04/04/yet-another-blogger-on-earth/" });</script>]]></description>
			<content:encoded><![CDATA[<p>Hi guys, welcome on this new blog!</p>
<p>Like many people, I learned a lot by reading posts from various very good writers (Think <a href="http://codebetter.com/blogs/jeremy.miller/" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://codebetter.com/blogs/jeremy.miller/');">Jeremy Miller</a>, <a href="http://codebetter.com/blogs/jean-paul_boodhoo/" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://codebetter.com/blogs/jean-paul_boodhoo/');">JP Boodhoo</a>, <a href="http://martinfowler.com/bliki/" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://martinfowler.com/bliki/');">Martin Fowler</a>, and dozens of others!). Since I discovered their blogs, their knowledge has always been useful to me. Even if I don't know them personally, they helped me a lot in improving my skills as a developer. In a few words, I owe them a lot!</p>
<p>Starting this blog is a way to try repaying my debt to the community as a whole at my humble level. As a matter of fact, I feel that there's still a lot to do to improve the way we do software development. Even if there are some excellent books and articles available on internet, most people I've been working with have always been foreigners to topics such as design patterns, testing, agiles methodologies, etc. Even if I don't have the expertise of all the people above (and I also don't claim to master any of these topics myself!), hopefully, I'll be able to convey good practices and ideas too. Who knows, maybe I'll even make a small difference around me! :)</p>
<p>Let me also give you a quick background about myself. I'm a French guy working as a .NET developer in the financial industry. I spent the last year in London working for a hedge-fund, and I moved back to Paris in February where I started a new job in a consulting company. Before that, I was studying computer science in a French engineering school which also lead me to do several internships. I also started a company during that time but it's an old story! Bottom line: I'm still very young and I still have a lot to learn!</p>
<p>Finally, while I'm here, I also apologize in advance for all the spelling mistakes that I'll do in my posts. Please feel free to correct me whenever you spot one (that is probably every 5 words!).</p>
<p>See you!</p>
<p>By the way, I'm also opening a french version of this blog here: <a href="http://www.thedotnetfrog.Fr" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.thedotnetfrog.Fr');">www.thedotnetfrog.fr</a></p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Yet+another+blogger+on+earth%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F04%2Fyet-another-blogger-on-earth%2F" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://sharethis.com/item?&wp=2.5.1&amp;publisher=2aa33dcc-c52b-495d-9bca-8f87976a616b&amp;title=Yet+another+blogger+on+earth%21&amp;url=http%3A%2F%2Fwww.thedotnetfrog.com%2F2008%2F04%2F04%2Fyet-another-blogger-on-earth%2F');">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thedotnetfrog.com/2008/04/04/yet-another-blogger-on-earth/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
