<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tag 'Design/Coding Guidance'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=Design/Coding+Guidance&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'Design/Coding Guidance'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>The Rat-hole of Object-oriented Mapping</title><link>http://msmvps.com/blogs/peterritchie/archive/2012/02/10/the-rat-hole-of-object-oriented-mapping.aspx</link><pubDate>Fri, 10 Feb 2012 06:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1805879</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;&lt;a href="http://bit.ly/waD5m6"&gt;Mark Seemann&lt;/a&gt; recently had a &lt;a href="http://bit.ly/xHRYMz"&gt;great post&lt;/a&gt; that, as most of his posts seem to do, challenges the average reader to re-think their reality a bit.&amp;#160; The post is titled “Is Layering Worth the Mapping”.&amp;#160; In the post Mark essentially details some of the grief and friction that developers face when they need to decouple two or more concepts by way of an abstraction.&lt;/p&gt;  &lt;p&gt;Mark takes the example of layering.&amp;#160; Layering is a one way coupling between two “layers” where the “higher-level” layer takes a dependency on abstractions in a “lower-level” layer.&amp;#160; Part of his example is a UI layer communicates with a domain layer about musical track information.&amp;#160; That track information that is communicated lives in a hand-crafted Track abstraction.&amp;#160; Typically this abstraction would live with the lower-level layer to maintain the unidirectional coupling.&amp;#160; Of course the UI layer needs a Track concretion for it to do its job and must map between the higher-level layer and the lower-level layer.&amp;#160; To further complicate things other decoupling may occur within each layer to manage dependencies.&amp;#160; The UI may implement an MVx pattern in which case there may be a specific “view-model” track abstraction, the data layer may employ object-relational mapping, etc. etc.&lt;/p&gt;  &lt;p&gt;Mark goes on to describe some “solutions” that often fall out of scenarios like this in a need to help manage the sheer magnitude of the classes involved: shared DTOs as cross-cutting entities, POCO classes, classes with only automatic properties, etc.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s not just layering.&amp;#160; Layering lives in this grey area between in-memory modules and out-of-process &amp;quot;tiers&amp;quot;.&amp;#160; Layering, I think, is an attempt to get the benefits of out-of-process decoupling without the infrastructure concerns of connecting and communicating between out-of-process processes. Of course, over and above the module/assembly separation, the only thing enforcing this decoupling in layers is skill and being methodical.&lt;/p&gt;  &lt;p&gt;I&amp;#39;m convinced layering is often, or often becomes, a &amp;quot;speculative generality&amp;quot; to give some &amp;quot;future proofing&amp;quot; to the application since layering so closely resembles &amp;quot;tiering&amp;quot; (not to be confused with the eventual response of &amp;quot;tearing&amp;quot;) as to make it easy to make it tiered should there ever be a need for it.&amp;#160; To be clear, this is the &lt;strong&gt;wrong&lt;/strong&gt; impetus to design an software.&amp;#160; You’re effectively setting yourself up to fail by essentially “making up” requirements that are more than likely going to be wrong.&amp;#160; If the requirements for the design are based on fallacies, they too are designed wrong.&amp;#160; But, you have to continue to maintain this design until you re-write it (ever noticed that anti-pattern?).&lt;/p&gt;  &lt;p&gt;But, implementing tiers or any sort of communication between processes often ends up in the same state.&amp;#160; You have internal &amp;quot;domain&amp;quot; entities within the processes (and even within logical boundaries within those processes) that end up spawning the need for &amp;quot;DTO&amp;quot; objects that live at the seams on one or either side of the communication.&amp;#160; Further that, many times that communication is facilitated by frameworks like WCF that create their own DTOs (SOAP envelopes for example).&amp;#160; Except you&amp;#39;re mandated by the physical boundaries of processes and you&amp;#39;re forced to do things like shared-type assemblies to model the cross-cutting &amp;quot;entities&amp;quot; (if you choose that cross-cutting &amp;quot;optimization”) introducing a whole new level of effort and a massive surface area for attracting human error (you&amp;#39;ve technically introduced the need for versioning, potentially serialization, deployment issues, etc. etc.).&lt;/p&gt;  &lt;p&gt;Creating an object-oriented type to simply act as a one-way container to something that lives on the other size of some logical of physical boundary has appeared to me to be a smell for quite a while.&amp;#160; e.g. the UI layer in your original example has this concept of a &amp;quot;Track&amp;quot; DTO-like type that when used is only used in one direction at a time.&amp;#160; When moving from the UI to the domain layer it&amp;#39;s only written to.&amp;#160; If it gets a &amp;quot;track&amp;quot; back from the domain layer the UI layer only reads from it.&amp;#160; Abstracting this into an OO class seems pointless and, as Mark says, not particularly beneficial.&lt;/p&gt;  &lt;p&gt;Let’s look specifically at the in-memory representation of something like a “Track”.&amp;#160; We’ll limit our self and say that we need four Track abstractions: one for the view model, one for the domain layer abstraction, one for the data layer abstraction, and one for the object-relational mapping.&amp;#160; (I’ve assumed that the data layer may not have a track “entity” and is only responsible for pushing data around).&amp;#160; So, in effect we have four Track DTO classes in our system (and two or three Track “entities”).&amp;#160; But, if we look at the in-memory representation of instances of these objects they’re effectively identical—each one can’t really have more data than another otherwise there’s something wrong.&amp;#160; If we look at what’s actually happing with this data, we’re really writing a lot of code to copy memory around in a really inefficient way.&amp;#160; The DTO classes in part become the way to copy memory.&amp;#160; To be fair, this is a side-effect of the fact we’re manually mapping from one abstraction to another or from one abstraction to an entity (or vice-versa).&lt;/p&gt;  &lt;p&gt;This type of thing isn’t entirely unknown; it sometimes goes by the name of &lt;strong&gt;ceremony&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;For the most part, I think computer languages are hindering us in our ability to address this.&amp;#160; Languages in general tend to maintain this specific way of messaging called method-calling that limits our ability to communicate only information that can be encapsulated by the language&amp;#39;s or platform&amp;#39;s type-system.&amp;#160; But, to a certain extent we&amp;#39;re also hindered by our myopia of &amp;quot;everything must be a type in language X&amp;quot;.&amp;#160; Maybe this is another manifestation of Maslow&amp;#39;s Hammer.&lt;/p&gt;  &lt;p&gt;Imagine if you removed all the mapping code in a complex system—especially a distributed system—and were left with just the “domain” code.&amp;#160; I’ve done this with one system and I was astounded that over 75% of the code in the system had nothing to do with the systems “domain” (the “value-add”) and was “ceremony” to facilitate data mapping.&lt;/p&gt;  &lt;p&gt;I sometimes hear this isn’t so much of a problem with specific frameworks.&amp;#160; I’m often told that these frameworks do all the heavy lifting like this for us.&amp;#160; But, they really don’t.&amp;#160; The frameworks really just introduce another seam.&amp;#160; The issue of Impedance Mismatch isn’t just related to object-relational mapping.&amp;#160; I has to do with any mapping where both sides aren’t constrained by the same rules.&amp;#160; I’ve blogged about this before. but I can use some “data framework” to generate “entities” based on a data model or even based on POCO’s.&amp;#160; Some view this as solving the problem; but it doesn’t.&amp;#160; Each side operates under different rules.&amp;#160; The generated classes can only have as much impedance as what it has to communicate with, and you have to plan that that’s different than the impedance you’ll end up mapping from/to.&amp;#160; The only real solution to this is to introduce &lt;strong&gt;another DTO&lt;/strong&gt; to map between your domain and the classes generated by the framework so you are decoupled from the eventual “gotchas” where your domain has different expectations or rules than the framework you’re communicating with.&amp;#160; When people don’t do this, you see all sorts of complains like “date/time in X isn’t the same as what I need”, etc.&lt;/p&gt;  &lt;p&gt;Don’t fall into this rut.&amp;#160; Think about what you’re doing; if you’re got 4 DTOs to hold the same data; maybe there’s a better way of doing it.&amp;#160; Try to come up with something better and blog about it or at least talk about the problem out in the open like Mark.&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;iframe src="http://www.facebook.com/widgets/like.php?href=http://msmvps.com/blogs/peterritchie/archive/2012/02/10/the-rat-hole-of-object-oriented-mapping.aspx" scrolling="no" frameborder="0" style="border:none;width:325px;height:80px;"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt; &lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement(&amp;#39;script&amp;#39;); po.type = &amp;#39;text/javascript&amp;#39;; po.async = true;
    po.src = &amp;#39;https://apis.google.com/js/plusone.js&amp;#39;;
    var s = document.getElementsByTagName(&amp;#39;script&amp;#39;)[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;&lt;/div&gt;</description></item><item><title>If You’re Using “#if DEBUG”, You’re Doing it Wrong</title><link>http://msmvps.com/blogs/peterritchie/archive/2011/11/24/if-you-re-using-if-debug-you-re-doing-it-wrong.aspx</link><pubDate>Thu, 24 Nov 2011 06:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1802873</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;I was going through some legacy code the other day, refactoring it all over the place and I ran into many blocks of code wrapped in “#if DEBUG”.&amp;#160; Of course, after a bit of refactoring in a RELEASE configuration these blocks of code were quickly out of date (and by out of date, I mean no longer compiling).&amp;#160; A huge PITA.&lt;/p&gt;  &lt;p&gt;For example, take the following code:&lt;/p&gt;  &lt;pre style="line-height:normal;background:white;"&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;	&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;MyCommand&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;	{
		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt; DateAndTimeOfTransaction;
	}
 
	&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;Test&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;	{
		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;/span&gt;
&lt;span style="color:#000000;"&gt;		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessCommand(&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;MyCommand&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt; myCommand)
		{
&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;#if&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt; DEBUG
&lt;/span&gt;&lt;span&gt;&lt;span style="color:#808080;"&gt;			if (myCommand.DateAndTimeOfTransaction &amp;gt; DateTime.Now)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span style="color:#808080;"&gt;				throw new InvalidOperationException(&amp;quot;DateTime expected to be in the past&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span style="color:#0000ff;"&gt;#endif&lt;/span&gt;&lt;/span&gt;
&lt;span style="color:#000000;"&gt;			&lt;/span&gt;&lt;span&gt;&lt;span style="color:#008000;"&gt;// do more stuff with myCommand...&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;		}
		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;span style="font-size:x-small;"&gt;	}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;If, while my active configuration is RELEASE, I rename-refactor &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;DateAndTimeOfTransaction&lt;/span&gt; to &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;TransactionDateTime&lt;/span&gt; then the block of code in the #if DEBUG is now invalid—&lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;DateAndTimeOfTransaction&lt;/span&gt; will not be renamed within this block.&amp;#160; I will now get compile errors if I switch to DEBUG configuration (or worse still, if I check in an my continuous integration environment does a debug build).&lt;/p&gt;

&lt;p&gt;I would have run into the same problem had I been in a DEBUG configuration with all the “&lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;#if RELEASE&lt;/span&gt;” blocks.&amp;#160; Yes, I could create a configuration and define DEBUG &lt;strong&gt;and&lt;/strong&gt; RELEASE and do my work in there.&amp;#160; Yeah, no, not going down that twisted path… i.e. try doing it without manually adding a conditional compilation symbol.&lt;/p&gt;

&lt;p&gt;I got to thinking about a better way.&amp;#160; It dawned on me that &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;#if&lt;/span&gt; blocks are effectively comments in other configurations (assuming DEBUG or RELEASE; but true with any symbol) and that &lt;a href="http://bit.ly/vDJekK"&gt;comments are apologies&lt;/a&gt;, and it quickly became clear to me how to fix this.&lt;/p&gt;

&lt;p&gt;Enter Extract Method refactoring and Conditional Methods.&amp;#160; If you’ve been living under a rock or have simply forgotten, there exists a type in the BCL: &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;System.Diagnostics.ConditionalAttribute&lt;/span&gt;.&amp;#160; You put this attribute on methods &lt;span style="text-decoration:line-through;"&gt;that&lt;/span&gt;[whose calls] may or may not be included in the resulting binary (IL).&amp;#160; But, they are still compiled (and thus syntax checked).&amp;#160; So, if I followed the basic tenet for &lt;a href="http://bit.ly/47Ld1d"&gt;code comment smells&lt;/a&gt; and performed an Extract Method refactoring and applied the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;ConditionalAttribute&lt;/span&gt; to the resulting method, I’d end up with something like this:&lt;/p&gt;

&lt;pre style="line-height:normal;background:white;"&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;/span&gt;
&lt;span style="color:#000000;"&gt;		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessCommand(&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;MyCommand&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt; myCommand)
		{
			CheckMyCommandPreconditions(myCommand);
			&lt;/span&gt;&lt;span&gt;&lt;span style="color:#008000;"&gt;// do more stuff with myCommand...&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;		}
 
		[&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;Conditional&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;)]
		&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; CheckMyCommandPreconditions(&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;MyCommand&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt; myCommand)
		{
			&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (myCommand.DateAndTimeOfTransaction &amp;gt; &lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;"&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;.Now)
				&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;#160;&lt;/span&gt;&lt;span&gt;&lt;span style="color:#2b91af;"&gt;InvalidOperationException&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;DateTime expected to be in the past&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;span style="font-size:x-small;"&gt;);
		}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Now, if I perform a rename refactoring on &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;MyCommand.DateAndTimeOfTransaction&lt;/span&gt;, &lt;strong&gt;all&lt;/strong&gt; usages of &lt;span style="font-family:&amp;#39;Courier New&amp;#39;;"&gt;DateAndTimeOfTransaction&lt;/span&gt; get renamed and I no longer introduce compile errors.&lt;/p&gt;

&lt;h3&gt;Code Contracts&lt;/h3&gt;

&lt;p&gt;If you look closely at the name I chose for the extracted method you’ll notice that what this DEBUG code is actually doing is asserting method preconditions.&amp;#160; This type of thing is actually supported in &lt;a href="http://bit.ly/s23uAU"&gt;Code Contracts&lt;/a&gt;.&amp;#160; Code Contracts implement a concept called &lt;a href="http://bit.ly/szEmTc"&gt;Design by Contract&lt;/a&gt; (DbC).&amp;#160; One benefit to DbC is that these checks are effectively proved at compile time so there’s no reason to perform this check at runtime or &lt;a href="http://bit.ly/tYnSGC"&gt;even write unit tests to check them&lt;/a&gt; because code to violate them becomes a “compile error” with Code Contracts.&amp;#160; But, for my example I didn’t use DbC despite potentially being a better way of implementing this particular code.&lt;/p&gt;

&lt;p&gt;Anyone know what we call a refactoring that introduces unwanted side-effects like compile errors?&lt;/p&gt;

&lt;p&gt;[UPDATE: small correction to the description of conditional methods and what does/doesn&amp;#39;t get generated to IL based on comment from Motti Shaked]&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;iframe src="http://www.facebook.com/widgets/like.php?href=http://msmvps.com/blogs/peterritchie/archive/2011/11/24/if-you-re-using-if-debug-you-re-doing-it-wrong.aspx" scrolling="no" frameborder="0" style="border:none;width:130px;height:80px;"&gt;&lt;/iframe&gt;&lt;/div&gt;</description></item><item><title>Criteria for Success</title><link>http://msmvps.com/blogs/peterritchie/archive/2011/05/07/criteria-for-success.aspx</link><pubDate>Sat, 07 May 2011 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1792817</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;I was having a&amp;#160; conversation with a client recently and the topic of “11th hour” came up.&amp;#160; He seemed to think it wasn’t possible to deliver a software project without some sort of “11th hour” panic.&amp;#160; I disagreed&lt;/p&gt;  &lt;p&gt;I disagree because I’ve done it on small and large projects.&amp;#160; Don’t get me wrong; it’s very easy to lose sight of things and easily get a project into a state where “11th hour” panic is inevitable.&amp;#160; But, it’s not something that can’t be avoided.&lt;/p&gt;  &lt;p&gt;One of the problems with software projects, it seems, (and with other projects, I suppose) is losing sight of things and not having a “destination”.&amp;#160; This is what I refer to by “criteria for success”.&lt;/p&gt;  &lt;p&gt;Criteria for success is basically the criteria by which what is expected to be delivered will be evaluated.&lt;/p&gt;  &lt;p&gt;This criteria no only gives you a focused destination, it also provides a focus for the team.&amp;#160; Without some sort of criteria it’s hard to gauge whether the team is side-tracked on something that doesn’t add value to the project.&amp;#160; Criteria for success not only gives you a destination to strive for; but also means your efforts can be focused on that destination and be able to get to that destination quicker.&amp;#160; This means the project can be completed quicker and progress can be judged more accurately.&lt;/p&gt;  &lt;p&gt;Without defining criteria for success, software projects that do succeed succeed by accident.&amp;#160; I don’t know about the rest of the industry; but I’m not really comfortable with completing my work “by accident”.&lt;/p&gt;  &lt;h1&gt;Defining “Done”&lt;/h1&gt;  &lt;p&gt;&lt;em&gt;Defining “Done”&lt;/em&gt; is a very similar.&amp;#160; Defining done is another popular mantra; but, I think “criteria for success” is more clear in its goal in that it’s almost explicit that acceptance criteria is what we’re looking for.&amp;#160; “Done” can be a little wishy-washy and open for interpretation.&lt;/p&gt;  &lt;h1&gt;Defining Criteria&lt;/h1&gt;  &lt;p&gt;I don’t think it’s enough to complain that people have a problem defining success or “done”; so, I think it’s also important to help people get their own criteria for success.&amp;#160; i.e. people have a problem coming up with criteria for success because no one has defined criteria for success for defining criteria for success—so to speak.&lt;/p&gt;  &lt;p&gt;There’s various tools to help with this sort of thing; the most obvious is Agile.&amp;#160; Defining user stories and getting acceptance from stakeholders on these stories is an excellent first step.&amp;#160; Many people simply view user stories as a check-list of items.&amp;#160; This can be extremely useful; but explicitly defining criteria (acceptance criteria is the more general term) isn’t an explicit task for most people.&lt;/p&gt;  &lt;p&gt;Often we can’t expect out stakeholders to know how to define the acceptance criteria.&amp;#160; “Just do it” or “make it work” are often the definition of done—which doesn’t leave us much in the way of unambiguous. &lt;/p&gt;  &lt;p&gt;Agile kind of get’s away from “requirements” because requirements are often associated with waterfall methodologies and people expect that “requirements” mean a full set of requirements for the project before the project commences and doesn’t change for the life of the project.&amp;#160; This really isn’t what requirements are; but, we’ll continue using terms like “criteria for success” and “defining ‘done’” to avoid these associations.&amp;#160; But, theories and practices for effectively defining requirements can give us some valuable tools for defining good criteria.&amp;#160; Karl Wiegers has a couple of excellent books that has some great criteria that we can reuse for our criteria:&lt;/p&gt;  &lt;h5&gt;Correct&lt;/h5&gt;  &lt;p&gt;Is the criteria correct (easy to say)?&amp;#160; Is how we’re judging the deliverable right?&amp;#160; If we’re judging &lt;em&gt;x&lt;/em&gt; against &lt;em&gt;y&lt;/em&gt;, is &lt;em&gt;y&lt;/em&gt; what will actually be used as the criteria?&amp;#160; If it’s really &lt;em&gt;z&lt;/em&gt;, we can’t deliver something that satisfies &lt;em&gt;y&lt;/em&gt; on purpose.&lt;/p&gt;  &lt;h5&gt;Feasible&lt;/h5&gt;  &lt;p&gt;Is the criteria even attainable?&amp;#160; If we simply can’t reach that criteria, we’re going to fail…&amp;#160; This can be a bit tricky because we don’t always know whether what we’re actually working is actually possible.&amp;#160; With Agile, we like to separate what is known to be possible from what is not known to be possible with “spikes”.&amp;#160; We purposely research things we’re not sure are possible (or not entirely sure how long things may take) into spikes so we can gather the information we need to define our criteria for success correctly.&amp;#160; This mitigates the risk and gives is the ability to separate and time-box these aspects of the project because if we’re not really sure if it’s feasible or how long it’s going to take we need to prioritize it differently and explicitly.&lt;/p&gt;  &lt;h5&gt;Necessary&lt;/h5&gt;  &lt;p&gt;This is kind of obvious; but without this explicit criteria our criteria may lack focus and our work not be prioritized correctly and we end up working on tasks that are not important (i.e. not necessary).&amp;#160; It’s critical that the criteria actually define necessary qualities.&lt;/p&gt;  &lt;h5&gt;Prioritized&lt;/h5&gt;  &lt;p&gt;I’ve already alluded to this; but prioritized criteria is essential for getting a project done properly.&amp;#160; “Prioritized” is a bit ambiguous; so, it’s important to define what we mean by prioritization.&amp;#160; The most essential part is that criteria is prioritized in relation to other criteria.&amp;#160; There’s a tendency for everything to be prioritized as “important” and this has the opposite effect because it also means everything is defined as “unimportant” and the order that tasks get performed is open to interpretation (ease of implementation, interest in the task, coolness of the task, etc. none of which are stakeholder criteria).&amp;#160; If it’s not important enough to prioritize a task correctly, it’s not important enough to actually perform the task.&lt;/p&gt;  &lt;h5&gt;Unambiguous&lt;/h5&gt;  &lt;p&gt;It may seem obvious; be we need to make sure our criteria is not ambiguous.&amp;#160; It’s easy for criteria to be open for interpretation and end up being ambiguous.&amp;#160; See the next point…&lt;/p&gt;  &lt;h5&gt;Verifiable&lt;/h5&gt;  &lt;p&gt;The criteria that Weigers’ details is that the criteria needs to be verifiable.&amp;#160; It’s not enough to say “done” or “works”, we need to be able to unambiguously and empirically verify our work against specific criteria.&amp;#160; Criteria like “works” just doesn’t cut it. This is stakeholder-driven.&amp;#160; Often we’ll need to help our stakeholders think through what they’re ultimately going to use for verification—we want them to think about this before we begin the work, not after (which is very common).&lt;/p&gt;</description></item><item><title>Mapping to Your Database is a Private Affair</title><link>http://msmvps.com/blogs/peterritchie/archive/2011/03/26/mapping-to-your-database-is-a-private-affair.aspx</link><pubDate>Sat, 26 Mar 2011 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1790630</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;Your mapping to your database is generally coupled to the design of the data and the mapping provider&amp;#39;s ability to implement a relational or non-relational model.&amp;#160; This means your ORM influences the design of your mapped classes and/or the design of system that uses these mapped classes.&amp;#160; i.e. your design can be limited in certain aspects by how the mapper is implemented.&lt;/p&gt;  &lt;p&gt;Because of these idiosyncrasies, your choice of ORM becomes an implementation detail; so, you want to keep any of its details and generated classes out of your interfaces.&amp;#160; You want to keep your ORM’s classes and the classes it generates private.&lt;/p&gt;  &lt;p&gt;This may sound inconsequential, but there are some very real examples of how the limits of your choice of mapping framework can adversely affect your external interfaces.&amp;#160; Let&amp;#39;s look at some specific cases of this.&amp;#160; &lt;/p&gt;  &lt;p&gt;WCF supports data contracts and collection types.&amp;#160; But, because of the nature of&amp;#160; WCF and SOAP, &lt;a href="http://lynk.at/gd9MfW"&gt;it deals with serializing collections in a specific way&lt;/a&gt;.&amp;#160; If you have an entity class with a collection property on it, it may be perfectly acceptable to be able to add objects to that collection and expect them to make their way back into the database.&amp;#160; But, what does that mean for some entity that’s been serialized over the wire to another remote computer that doesn’t have access to your database?&amp;#160; It means the entity you publish over WCF has a different behaviour that those local to (presumably) the server.&amp;#160; But, you don’t want to modify your entity to work in a different way &lt;em&gt;just for your WCF interface&lt;/em&gt;. You’ve published an interface to the outside world, clients are using it and you realise you need to make a change to your database.&amp;#160; Opps, can’t it’s tightly coupled to your WCF interface; if you change your database and regenerate your entities you interface will change; breaking all your clients.&lt;/p&gt;  &lt;p&gt;It works the other way around too.&amp;#160; If you want to publish types over WCF, you have to annotate them, potentially, with all sorts of things to get them to serialize the way you want them to.&amp;#160; The simplest is DataContractAttribute and DataMemberAttribute; but, could get more complex with things like IExtensibleObject, things like DataContractSerializer versus XmlSerializer, etc.&amp;#160; Then there’s things like versioning…&amp;#160; Do you really want to add all these annotations to code generated by your mapper?&amp;#160; In some cases you can’t annotate them.&lt;/p&gt;  &lt;p&gt;So, classes that map to/from the data effectively should be implementation details of your system; they should be private to your data layer; but, then how do we detail transferring data from our domain to the outside world?&amp;#160; The Facade pattern and the Data Transfer Object pattern are a perfect solution to this.&amp;#160; Your WCF interface is effectively a facade—an interface (usually simplified) to a different body of code—or a type of view to your model. When you have to provide complex data structures in your interface (i.e., other than base types) use Data Transfer Objects (DTOs) to wrap them in.&amp;#160; Should the body of code that the facade wraps needs to change, simply modify the code that generates the DTOs.&amp;#160; (I almost always get this question next: “What if the change means I can no longer produce the same DTO?”.&amp;#160; Well, then you’ll have to decide if you should violate your contract and break all your clients, or find a way to make it work.)&lt;/p&gt;  &lt;p&gt;I find it amusing to see all the examples of things like using your LINQ to SQL or LINQ to Entities classes in WCF interfaces.&amp;#160; The examples are simple and often work; but once you try to do that in a complex system quickly realize you’re down a rat hole with really only one way out.&amp;#160; Do yourself a favour and completely abstract your implementation details from your internally accessible interfaces; you’ll thank yourself.&lt;/p&gt;</description></item><item><title>Using the dynamic Keyword in C# to Improve Object Orientation – A Follow-up</title><link>http://msmvps.com/blogs/peterritchie/archive/2010/05/27/using-the-dynamic-keyword-in-c-to-improve-object-orientation-a-follow-up.aspx</link><pubDate>Thu, 27 May 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1770325</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;Based on some feedback, some clarification is warranted with regard to my previous post titled “&lt;a href="http://msmvps.com/blogs/peterritchie/archive/2010/05/24/using-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx"&gt;Using the dynamic Keyword in C# to Improve Object Orientation&lt;/a&gt;”.&lt;/p&gt;  &lt;p&gt;As &lt;a href="http://www.twitter.com/JarekKowalski"&gt;Jarek Kowalski&lt;/a&gt; correctly pointed out, the example code that I provided could have used the &lt;a href="http://en.wikipedia.org/wiki/Visitor_pattern"&gt;Visitor pattern&lt;/a&gt; instead to get the same result.&amp;#160; My impetus for using the dynamic keyword the way I did was slightly different from how I described my example—which was meant to be easier to read.&lt;/p&gt;  &lt;p&gt;I think it’s worthwhile describing the Visitor Pattern.&amp;#160; The Visitor pattern is a pattern used to separate the responsibility of an algorithm from the class that the algorithm should operate upon.&amp;#160; Essentially, a Visitor class is created to contain the algorithm that would be invoked through its Visit method and the classes that the algorithm operates upon some sort of method that accepts a reference to a Visitor object (which would simply invoke the Visit method).&amp;#160; The example that Jarek provided was very similar to:&lt;/p&gt;  &lt;div style="color:black;"&gt;   &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;span style="color:#2b91af;"&gt;IShapeVisitor&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Visit(&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; square);&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Visit(&lt;span style="color:#2b91af;"&gt;Rectangle&lt;/span&gt;&lt;span style="color:#000000;"&gt; square);&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; : &lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Accept(&lt;span style="color:#2b91af;"&gt;IShapeVisitor&lt;/span&gt;&lt;span style="color:#000000;"&gt; shapeVisitor)&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 			shapeVisitor.Visit(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt;&amp;#160;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And an implementation of IShapeVisitor would be provided like this:&lt;/p&gt;

&lt;div style="color:black;"&gt;
  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;DrawingVisitor&lt;/span&gt;&lt;span style="color:#000000;"&gt;  : IShapeVisitor&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Visit(Circle circle)&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#008000;"&gt;// draw circle here&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Visit(Rectangle rectangle)&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#008000;"&gt;// draw rectangle here&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; &lt;/pre&gt;

  &lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;In our case, the bodies of Visit methods would call a Draw method contained in another class like a View implementation.&amp;#160; For example:&lt;/span&gt; &lt;/span&gt;

                &lt;div style="color:black;"&gt;
                  &lt;div style="color:black;"&gt;
                    &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;DrawingVisitor&lt;/span&gt;&lt;span style="color:#000000;"&gt;  : &lt;span style="color:#2b91af;"&gt;IShapeVisitor&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;IView&lt;/span&gt;&lt;span style="color:#000000;"&gt;  view;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  DrawingVisitor(&lt;span style="color:#2b91af;"&gt;IView&lt;/span&gt;&lt;span style="color:#000000;"&gt;  view)&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.view = view;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; &lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Visit(&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt;  circle)&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 			view.Draw(circle);&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; &lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Visit(&lt;span style="color:#2b91af;"&gt;Rectangle&lt;/span&gt;&lt;span style="color:#000000;"&gt;  rectangle)&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 			view.Draw(rectangle);&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;

                    &lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
                  &lt;/div&gt;

                  &lt;p&gt;This of course, adds another level of indirection that allows our target (a Shape implementation) to invoke a virtual method that ends up delegating to the Visitor.&amp;#160; An implementation of &lt;a href="http://en.wikipedia.org/wiki/Double_dispatch"&gt;Double Dispatch&lt;/a&gt;.&amp;#160; This allows objects whose type isn’t known until run-time to properly invoke overloaded methods of another type.&lt;/p&gt;

                  &lt;p&gt;As you can see, this is very powerful but adds a bit more complexity.&lt;/p&gt;

                  &lt;p&gt;Now, in my original scenario (before I wrote the example for the post), I was dealing with invoking particular series of static overloads of System.Convert.&amp;#160; Because I was dealing both with a class outside of my control and dealing with static overloads, the Visitor pattern could not be applied.&lt;/p&gt;

                  &lt;p&gt;&lt;/p&gt;
                &lt;/div&gt;</description></item><item><title>Using the dynamic Keyword in C# to Improve Object-Orientation</title><link>http://msmvps.com/blogs/peterritchie/archive/2010/05/24/using-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx</link><pubDate>Mon, 24 May 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1768867</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;With polymorphism, object-oriented languages allow &amp;quot;...different data types to be handled using a uniform interface&amp;quot;.&amp;nbsp; Ad-hoc polymorphism is when you declare multiple methods of the same name but differ by the type of an argument.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Draw(&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt;  circle)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Draw(&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt;  square)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These are usually referred to as &lt;i&gt;method overloads&lt;/i&gt; or &lt;i&gt;method overloading&lt;/i&gt;.&amp;nbsp; Which Draw method that gets invoked would be decided upon at compile-time based on the type of the parameter passed to it. &lt;/p&gt;
&lt;p&gt;This is great, there&amp;rsquo;s many situations where this is useful; but what about situations where you don&amp;#39;t know the exact type of the object until run-time?&amp;nbsp; Many programmers instinctly lean towards using the GetType method available on all objects in .NET and perform a manual type comparison.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  ProcessShape(&lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt;&lt;span style="color:#000000;"&gt;  type = shape.GetType();&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (type == &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt; ))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw((&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt; )shape);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (type == &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; ))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw((&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; )shape);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There&amp;#39;s other variants of this that make it a little harder to detect (and often gets around static code analysis warnings): &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  ProcessShape(&lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt;  square = shape &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (square != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; )&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw(square);&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt;  circle = shape &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (circle != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; )&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw(circle);&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This code is a problem because we lose the benefits of C#s polymorphism (this code is coupled to Circle and Shape, if either is removed, this code would have to be changed and re-tested.&amp;nbsp;&amp;nbsp; If another Shape derivative were added, the code would again have to be changed and re-tested).&amp;nbsp; There&amp;#39;s multiple circumstances where this might happen over and above the so-far specious examples above.&amp;nbsp; For example, deserializing an object from a Stream often results in not knowing the exact type of the object that has been deserialized.&amp;nbsp; This is common with type hierarchies.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  DeserializeShape(&lt;span style="color:#2b91af;"&gt;Stream&lt;/span&gt;&lt;span style="color:#000000;"&gt;  stream)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;IFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;BinaryFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt; ();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter.Deserialize(stream) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With the above code, we&amp;#39;ve re-hydrated either a Circle of a Square object into a Shape object (and ignored the appropriate checks to make sure a Shape object was loaded).&amp;nbsp; We&amp;#39;re now unable to use all the other polymorphism features of C#: method overloading won&amp;#39;t work, C# will always only use the overload with the Shape argument and generics suffers from the same problem, it will always resolve to the type parameter of type Shape.&amp;nbsp; For example, if we try to call one of our two Draw methods with our re-hydrated Shape object: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; 			Draw(shape);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;br /&gt;...we simply get a CS1502 warning because there is now Draw(Shape shape) method. 
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When programmers simply compare types, they&amp;#39;re admitting that polymorphism has been circumvented and they have to resort to introducing the &lt;i&gt;Conditional Complexity&lt;/i&gt; Code Smell or the &lt;i&gt;Switch Statement&lt;/i&gt; Code Smell (C# doesn&amp;#39;t support Type in a switch statement, so some programmers go ahead and create a integer type code--or even worse, use String in the switch statement) &lt;/p&gt;
&lt;p&gt;So, what&amp;#39;s a programmer to do to keep our code object-oriented and use polymorphism? &lt;/p&gt;
&lt;p&gt;Well, if you read the title of this post, you&amp;#39;ve probably figured it out: use the dynamic keyword.&amp;nbsp; The dynamic keyword bypasses static (compile-time) type checking and performs the type-checking dynamically (at run-time).&amp;nbsp; So, for us to use our Draw overloads with our Shape object, we simply assign it to a dynamic object and use that object instead.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape = DeserializeShape(stream);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#0000ff;"&gt;dynamic&lt;/span&gt;&lt;span style="color:#000000;"&gt;  dynShape = shape;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			Draw(dynShape);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now the runtime will check to see the actual type of dynShape object and invoke the corresponding Draw overload. &lt;/p&gt;
&lt;p&gt;In our example, we&amp;#39;ve used inheritance to derive our two shapes from a single base.&amp;nbsp; But, given our current use of Circle and Square objects, we don&amp;#39;t need them to inherit from the same base class.&amp;nbsp; We could simply view these shapes as an Object when we want to use dynamic.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;&lt;span style="color:#000000;"&gt;  DeserializeShape(&lt;span style="color:#2b91af;"&gt;Stream&lt;/span&gt;&lt;span style="color:#000000;"&gt;  stream)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;IFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;BinaryFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt; ();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter.Deserialize(stream);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#0000ff;"&gt;dynamic&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape = DeserializeShape(stream);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			Draw(shape);&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With the above, we&amp;rsquo;d have the same result without deriving both Circle and Square from Shape.&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2010%2f05%2f24%2fusing-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2010%2f05%2f24%2fusing-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Using the dynamic Keyword in C# to Improve Object-Orientation</title><link>http://msmvps.com/blogs/peterritchie/archive/2010/05/24/using-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx</link><pubDate>Mon, 24 May 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1768867</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;With polymorphism, object-oriented languages allow &amp;quot;...different data types to be handled using a uniform interface&amp;quot;.&amp;nbsp; Ad-hoc polymorphism is when you declare multiple methods of the same name but differ by the type of an argument.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Draw(&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt;  circle)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Draw(&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt;  square)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These are usually referred to as &lt;i&gt;method overloads&lt;/i&gt; or &lt;i&gt;method overloading&lt;/i&gt;.&amp;nbsp; Which Draw method that gets invoked would be decided upon at compile-time based on the type of the parameter passed to it. &lt;/p&gt;
&lt;p&gt;This is great, there are many situations where this is useful; but what about situations where you don&amp;#39;t know the exact type of the object until run-time?&amp;nbsp; Many programmers instinctly lean towards using the GetType method available on all objects in .NET and perform a manual type comparison.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  ProcessShape(&lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt;&lt;span style="color:#000000;"&gt;  type = shape.GetType();&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (type == &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt; ))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw((&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt; )shape);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (type == &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; ))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw((&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; )shape);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There are other variants of this that make it a little harder to detect (and often gets around static code analysis warnings): &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt;  ProcessShape(&lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt;  square = shape &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Square&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (square != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; )&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw(square);&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt;  circle = shape &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Circle&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (circle != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; )&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		Draw(circle);&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 		&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This code is a problem because we lose the benefits of C#s polymorphism (this code is coupled to Circle and Shape, if either is removed, this code would have to be changed and re-tested.&amp;nbsp;&amp;nbsp; If another Shape derivative were added, the code would again have to be changed and re-tested).&amp;nbsp; There are multiple circumstances where this might happen over and above the so-far specious examples above.&amp;nbsp; For example, deserializing an object from a Stream often results in not knowing the exact type of the object that has been deserialized.&amp;nbsp; This is common with type hierarchies.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  DeserializeShape(&lt;span style="color:#2b91af;"&gt;Stream&lt;/span&gt;&lt;span style="color:#000000;"&gt;  stream)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;IFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;BinaryFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt; ();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter.Deserialize(stream) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With the above code, we&amp;#39;ve re-hydrated either a Circle of a Square object into a Shape object (and ignored the appropriate checks to make sure a Shape object was loaded).&amp;nbsp; We&amp;#39;re now unable to use all the other polymorphism features of C#: method overloading won&amp;#39;t work, C# will always only use the overload with the Shape argument and generics suffers from the same problem, it will always resolve to the type parameter of type Shape.&amp;nbsp; For example, if we try to call one of our two Draw methods with our re-hydrated Shape object: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; 			Draw(shape);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;br /&gt;...we simply get a CS1502 warning because there is now Draw(Shape shape) method. 
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When programmers simply compare types, they&amp;#39;re admitting that polymorphism has been circumvented and they have to resort to introducing the &lt;i&gt;Conditional Complexity&lt;/i&gt; Code Smell or the &lt;i&gt;Switch Statement&lt;/i&gt; Code Smell (C# doesn&amp;#39;t support Type in a switch statement, so some programmers go ahead and create a integer type code--or even worse, use String in the switch statement) &lt;/p&gt;
&lt;p&gt;So, what&amp;#39;s a programmer to do to keep our code object-oriented and use polymorphism? &lt;/p&gt;
&lt;p&gt;Well, if you read the title of this post, you&amp;#39;ve probably figured it out: use the dynamic keyword.&amp;nbsp; The dynamic keyword bypasses static (compile-time) type checking and performs the type-checking dynamically (at run-time).&amp;nbsp; So, for us to use our Draw overloads with our Shape object, we simply assign it to a dynamic object and use that object instead.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#2b91af;"&gt;Shape&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape = DeserializeShape(stream);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#0000ff;"&gt;dynamic&lt;/span&gt;&lt;span style="color:#000000;"&gt;  dynShape = shape;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			Draw(dynShape);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now the runtime will check to see the actual type of dynShape object and invoke the corresponding Draw overload. &lt;/p&gt;
&lt;p&gt;In our example, we&amp;#39;ve used inheritance to derive our two shapes from a single base.&amp;nbsp; But, given our current use of Circle and Square objects, we don&amp;#39;t need them to inherit from the same base class.&amp;nbsp; We could simply view these shapes as an Object when we want to use dynamic.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="color:black;"&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;&lt;span style="color:#000000;"&gt;  DeserializeShape(&lt;span style="color:#2b91af;"&gt;Stream&lt;/span&gt;&lt;span style="color:#000000;"&gt;  stream)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#2b91af;"&gt;IFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;span style="color:#2b91af;"&gt;BinaryFormatter&lt;/span&gt;&lt;span style="color:#000000;"&gt; ();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 	&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;  formatter.Deserialize(stream);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			&lt;span style="color:#0000ff;"&gt;dynamic&lt;/span&gt;&lt;span style="color:#000000;"&gt;  shape = DeserializeShape(stream);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; 			Draw(shape);&lt;/pre&gt;
&lt;pre style="margin:0em;"&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With the above, we&amp;rsquo;d have the same result without deriving both Circle and Square from Shape.&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2010%2f05%2f24%2fusing-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2010%2f05%2f24%2fusing-the-dynamic-keyword-in-c-to-improve-object-orientation.aspx" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>The Difference between an Anti-Pattern and a Code Smell</title><link>http://msmvps.com/blogs/peterritchie/archive/2010/02/03/the-difference-between-an-anti-pattern-and-a-code-smell.aspx</link><pubDate>Wed, 03 Feb 2010 06:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1755116</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;I think the term &amp;ldquo;Anti-Pattern&amp;rdquo; is being over used.&amp;nbsp; There&amp;rsquo;s various definitions for Anti-Pattern like &amp;ldquo;obvious but wrong, solutions to recurring problems&amp;rdquo; and &amp;ldquo;common approaches to solving recurring problems that prove to be ineffective&amp;rdquo;.&amp;nbsp; All definitions have a common thread: they&amp;rsquo;re recognizable solutions (pattern) that don&amp;rsquo;t work in at least one way and should never be used.&lt;/p&gt;
&lt;p&gt;This means that anything that does work, when used correctly, isn&amp;rsquo;t an Anti-Pattern.&amp;nbsp; Transitively, that doesn&amp;rsquo;t make incorrectly used Patterns Anti-Patterns.&lt;/p&gt;
&lt;p&gt;Code Smells, on the other hand, are defined as &amp;ldquo;&amp;hellip;a hint that something might be wrong, not a certainty.&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve seen the term Anti-Pattern used in a couple of places lately that describe scenarios where patterns are used incorrectly.&amp;nbsp; Each and every pattern has a time and a place (a context).&amp;nbsp; Outside of that context, it is not an anti-pattern.&lt;/p&gt;
&lt;p&gt;The term Code Smell is much better to describe inappropriate uses of patterns.&lt;/p&gt;
&lt;p&gt;For example, Mark Seemann recently blogged that &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator is an Anti-Pattern&lt;/a&gt;.&amp;nbsp; I disagree.&amp;nbsp; I argue that there is a time and a place for Service Locator.&amp;nbsp; Incorrect use of Service Locator is definitely a code smell&amp;mdash;it&amp;rsquo;s an indication (a hint, not a certainty) that there&amp;rsquo;s something wrong with the design.&amp;nbsp; The fact that you&amp;rsquo;re using Service Locator instead of Dependency Injection is, indeed, a design issue.&amp;nbsp; But, dependency injection may not be possible.&amp;nbsp; WebForms, for example.&amp;nbsp; I can&amp;rsquo;t inject dependencies into a web form because I have no control over it&amp;rsquo;s construction.&amp;nbsp; I&amp;rsquo;m forced to either use a Service Locator or a Factory to aid in my decoupling efforts.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:be947cb2-fe03-47e2-b22b-1ea50bc1c409" class="wlWriterEditableSmartContent"&gt;del.icio.us Tags: &lt;a href="http://del.icio.us/popular/AntiPattern" rel="tag"&gt;AntiPattern&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Design%2fcoding+Guidance" rel="tag"&gt;Design/coding Guidance&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Patterns" rel="tag"&gt;Patterns&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Software+Development" rel="tag"&gt;Software Development&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Software+Development+Guidance" rel="tag"&gt;Software Development Guidance&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2010%2f02%2f03%2fthe-difference-between-an-anti-pattern-and-a-code-smell.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2010%2f02%2f03%2fthe-difference-between-an-anti-pattern-and-a-code-smell.aspx" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Evolving code over time</title><link>http://msmvps.com/blogs/peterritchie/archive/2009/03/30/evolving-code-over-time.aspx</link><pubDate>Mon, 30 Mar 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1682545</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;Given economics, time constraints, resource limitations, etc.; you can&amp;#39;t write all the functionality for a given solution for a single release.&amp;nbsp; Even if you weren&amp;#39;t limited by these constraints, you&amp;#39;re likely to get changing requirements as development progresses and everyone learns more about the software under development. &lt;/p&gt;
&lt;p&gt;It&amp;#39;s fairly easy to prioritize what is developed and what isn&amp;#39;t.&amp;nbsp; You simply develop only what you need (see YAGNI).&amp;nbsp; But, how do you manage adding new functionality without causing undue grief?&amp;nbsp; One way is to only make additive changes to the code.&amp;nbsp; For example, let&amp;#39;s say we have the method create CreateRequestPacket that creates a blob of bytes to send to a host over the wire: &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;public static byte[] CreateRequestPacket() &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; byte[] result = new byte[12]; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[0] = REQUEST_CODE; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[1] = NO_OPTIONAL_DATA_FLAG; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[2] = (byte) (result.Length - 2); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Random random = new Random(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 3; i &amp;lt; result.Length; ++i) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = (byte) (random.Next() % byte.MaxValue); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return result; &lt;br /&gt;}&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In iteration x it writes out a request making certain assumptions about what the request contains.&amp;nbsp; But, in iteration y it needs to optionally include other data.&amp;nbsp; A non additive way is to simply modify CreateRequestPacket to do what is needed: &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;public static byte[] CreateRequestPacket(bool useOptionalData) &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; byte[] result = new byte[12]; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[0] = REQUEST_CODE; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[1] = OPTIONAL_DATA_FLAG; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int index = 2; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(useOptionalData) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[index] = GetOptionalData(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ++index; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[index] = (byte) (result.Length - index); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Random random = new Random(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = index + 1; i &amp;lt; result.Length; ++i) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = (byte) (random.Next() % byte.MaxValue); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return result; &lt;br /&gt;}&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now all calls to CreateRequestPacket need to change before a build can occure and you might not be able to modify all the files that contain these calls.&amp;nbsp; So it causes undue blocking and forces you to change a number of files before you can check the file that CreateRequestPacket is contained withing. &lt;/p&gt;
&lt;p&gt;Another way of implementing this would be to implement an additive change.&amp;nbsp; That is, add a method that does what is needed and change the previous implementation to call the new method.&amp;nbsp; For example: &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;public static byte[] CreateRequestPacket(bool useOptionalData) &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; byte[] result = new byte[12]; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[0] = REQUEST_CODE; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[1] = OPTIONAL_DATA_FLAG; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int index = 2; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(useOptionalData) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[index] = GetOptionalData(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ++index; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result[index] = (byte) (result.Length - index); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Random random = new Random(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = index + 1; i &amp;lt; result.Length; ++i) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = (byte) (random.Next() % byte.MaxValue); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return result; &lt;br /&gt;} &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Courier New;"&gt;public static byte[] CreateRequestPacket() &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return CreateRequestPacket(false); &lt;br /&gt;}&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This allows you to check the file that contains CreateRequestPacket in right after passing unit testing and allows you to gradually change all calls to CreateRequestPacket as time permits.&lt;/p&gt;
&lt;div style="text-align:right;margin:0px;padding:4px 0px 4px 0px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2009%2f03%2f28%2fevolving-code-over-time.aspx&amp;amp;title=Evolving+code+over+time."&gt;&lt;img border="0" width="100" src="http://digg.com/img/badges/100x20-digg-button.png" alt="Digg This" height="20" style="border:0;" title="Digg This" /&gt;&lt;/a&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/peterritchie/archive/2009/03/28/evolving-code-over-time.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/peterritchie/archive/2009/03/28/evolving-code-over-time.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKick This" /&gt;&lt;/a&gt;&lt;/div&gt;</description></item><item><title>DevTeach 2009 Vancouver</title><link>http://msmvps.com/blogs/peterritchie/archive/2009/03/26/devteach-2009-vancouver.aspx</link><pubDate>Thu, 26 Mar 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1681658</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;The schedule for DevTeach 2009 Vancouver has been announced (&lt;a href="http://www.devteach.com/" title="http://www.devteach.com/"&gt;http://www.devteach.com/&lt;/a&gt;).&amp;nbsp; There&amp;rsquo;s lots of great software development sessions from some of the leaders in our industry.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re planning on improving yourself, this is the conference to go to.&amp;nbsp; Not only can you attend excellent sessions; but you can hob-knob with the presenters and pick their brains.&lt;/p&gt;
&lt;p&gt;If you have a friend or co-worker who&amp;rsquo;s interested, there&amp;rsquo;s a limited-time two-for-one offer for an even better price: &lt;a href="http://www.devteach.com/Register.aspx" title="http://www.devteach.com/Register.aspx"&gt;http://www.devteach.com/Register.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2009%2f03%2f26%2fdevteach-2009-vancouver.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2009%2f03%2f26%2fdevteach-2009-vancouver.aspx" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>