<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Polymorphism Part 2: Refactoring to Polymorphic Behavior</title>
	<atom:link href="http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/</link>
	<description></description>
	<lastBuildDate>Mon, 03 Jun 2013 06:05:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: John Teague</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-276</link>
		<dc:creator>John Teague</dc:creator>
		<pubDate>Wed, 08 May 2013 18:43:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-276</guid>
		<description>That&#039;s an interesting opinion.  I respect but obviously don&#039;t agree, especially when the code is more difficult than what I can put in a blog post as Artem said.  So good luck with that.</description>
		<content:encoded><![CDATA[<p>That&#8217;s an interesting opinion.  I respect but obviously don&#8217;t agree, especially when the code is more difficult than what I can put in a blog post as Artem said.  So good luck with that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artëm Smirnov</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-272</link>
		<dc:creator>Artëm Smirnov</dc:creator>
		<pubDate>Sun, 05 May 2013 08:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-272</guid>
		<description>I guess it depends on the complexity of the original switch statement. Sure in this example it&#039;s better to leave it as it is. However, when it grows up, I&#039;d prefer it scattered among several classes rather than several screens in one file.</description>
		<content:encoded><![CDATA[<p>I guess it depends on the complexity of the original switch statement. Sure in this example it&#8217;s better to leave it as it is. However, when it grows up, I&#8217;d prefer it scattered among several classes rather than several screens in one file.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MBR</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-262</link>
		<dc:creator>MBR</dc:creator>
		<pubDate>Mon, 25 Mar 2013 20:34:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-262</guid>
		<description>Let me offer a different view -- that the switch statement isn&#039;t &quot;code smell&quot;, but the &quot;refactoring&quot; often is. Consider what one accomplishes with this sort of re-factoring:

(1) You still have a switch statement of sorts - however, instead of it being completely under your control, you have now relagated responsibilty to the compiler&#039;s single-inheritence v-table dispatch implementation, which you can not control the behavior of -- you are now conflating the semantics of the programming language you are using with the the semantics of your domain.

(2) You have possibly added very serious costs and impedance-mismatches further down the chain, such as changing how serialization and object-mapping function, changing the signature of proxys/stubs to expose as web-services, etc.

(3) You have taken a localized desription of rules that are easily viewed all in one place and scattered them among several classes, and perhaps several source files. I would argue that this certainly does not make the code &quot;easier to test and read&quot;. One set of rules in one place is easier to read, and a single funciton that maps an integer to a string is easier to test than a test requiring several sub-classes of mock objects to be created.

(4) You have taken a step backwards from solving the problem generally - you have taken a &quot;table based&quot; view of your problem, which could easily have been externalized as a workflow in a scripting language or a set of rules in a database, and pushed your logic further into pre-compiled code and a distinct set of classes.  Manipulating data is much easier than managing code (meta-programming techniques such as reflection, dynamic code generation, etc.), and offers much more flexibility.

It could be that in a particular case (especially as the consumer of a large web/GUI/etc. framework that uses OOP formalisms) that this type of refactoring is reducing friction overall , but it certainly isn&#039;t a &quot;gimme&quot; that it&#039;s always the right thing to do. (Using the phrase &quot;code smell&quot; is</description>
		<content:encoded><![CDATA[<p>Let me offer a different view &#8212; that the switch statement isn&#8217;t &#8220;code smell&#8221;, but the &#8220;refactoring&#8221; often is. Consider what one accomplishes with this sort of re-factoring:</p>
<p>(1) You still have a switch statement of sorts &#8211; however, instead of it being completely under your control, you have now relagated responsibilty to the compiler&#8217;s single-inheritence v-table dispatch implementation, which you can not control the behavior of &#8212; you are now conflating the semantics of the programming language you are using with the the semantics of your domain.</p>
<p>(2) You have possibly added very serious costs and impedance-mismatches further down the chain, such as changing how serialization and object-mapping function, changing the signature of proxys/stubs to expose as web-services, etc.</p>
<p>(3) You have taken a localized desription of rules that are easily viewed all in one place and scattered them among several classes, and perhaps several source files. I would argue that this certainly does not make the code &#8220;easier to test and read&#8221;. One set of rules in one place is easier to read, and a single funciton that maps an integer to a string is easier to test than a test requiring several sub-classes of mock objects to be created.</p>
<p>(4) You have taken a step backwards from solving the problem generally &#8211; you have taken a &#8220;table based&#8221; view of your problem, which could easily have been externalized as a workflow in a scripting language or a set of rules in a database, and pushed your logic further into pre-compiled code and a distinct set of classes.  Manipulating data is much easier than managing code (meta-programming techniques such as reflection, dynamic code generation, etc.), and offers much more flexibility.</p>
<p>It could be that in a particular case (especially as the consumer of a large web/GUI/etc. framework that uses OOP formalisms) that this type of refactoring is reducing friction overall , but it certainly isn&#8217;t a &#8220;gimme&#8221; that it&#8217;s always the right thing to do. (Using the phrase &#8220;code smell&#8221; is</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Teague</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-258</link>
		<dc:creator>John Teague</dc:creator>
		<pubDate>Thu, 07 Mar 2013 13:30:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-258</guid>
		<description>Yeah, good point.  There are a couple of ways to do this.  In this case, since they are simple objects and all self contained, you could use the Enumeration pattern I linked to in the article.  It gives you a way to the list of possible objects as an array of strings that you could use to bind to a user interface.  Then once selected it gives you a way to fetch the object from the string.  We use this a lot to list our enumerated classes and rehydrate them.

If however, the objects are more complicated to build and you need more information at initialization time, then use the factory pattern.  Inside the factory there will be a switch statement of some kind, but now you&#039;ve isolated that switch to one, *and only one*, location in your application. So if you do need to change it, it only happens in one place.</description>
		<content:encoded><![CDATA[<p>Yeah, good point.  There are a couple of ways to do this.  In this case, since they are simple objects and all self contained, you could use the Enumeration pattern I linked to in the article.  It gives you a way to the list of possible objects as an array of strings that you could use to bind to a user interface.  Then once selected it gives you a way to fetch the object from the string.  We use this a lot to list our enumerated classes and rehydrate them.</p>
<p>If however, the objects are more complicated to build and you need more information at initialization time, then use the factory pattern.  Inside the factory there will be a switch statement of some kind, but now you&#8217;ve isolated that switch to one, *and only one*, location in your application. So if you do need to change it, it only happens in one place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-257</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Thu, 07 Mar 2013 09:27:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-257</guid>
		<description>Great little article to remind us of the importance of keeping an eye out for this kind of smell. One thing i always want to see when examples like this are shown is how you setup the Account, how do you assign it its calculator. I always feel like you end up with a switch/if to do that.</description>
		<content:encoded><![CDATA[<p>Great little article to remind us of the importance of keeping an eye out for this kind of smell. One thing i always want to see when examples like this are shown is how you setup the Account, how do you assign it its calculator. I always feel like you end up with a switch/if to do that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aleksandar Dragojević</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-255</link>
		<dc:creator>Aleksandar Dragojević</dc:creator>
		<pubDate>Wed, 06 Mar 2013 12:56:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-255</guid>
		<description>Nice article, good job, I personally prefer coding to the interface since it makes integrating DI a breeze later on, among other things. :)

</description>
		<content:encoded><![CDATA[<p>Nice article, good job, I personally prefer coding to the interface since it makes integrating DI a breeze later on, among other things. <img src='http://lostechies.com/johnteague/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The Morning Brew - Chris Alcock &#187; The Morning Brew #1309</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-254</link>
		<dc:creator>The Morning Brew - Chris Alcock &#187; The Morning Brew #1309</dc:creator>
		<pubDate>Wed, 06 Mar 2013 09:30:19 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-254</guid>
		<description>[...] Polymorphism Part 2: Refactoring to Polymorphic Behavior - John Teague continues his series looking at polymorphism, with a look at refactoring procedural code to polymorphic behaviour, converting code which uses switch statements to a more object oriented approach. [...]</description>
		<content:encoded><![CDATA[<p>[...] Polymorphism Part 2: Refactoring to Polymorphic Behavior &#8211; John Teague continues his series looking at polymorphism, with a look at refactoring procedural code to polymorphic behaviour, converting code which uses switch statements to a more object oriented approach. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Teague</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-252</link>
		<dc:creator>John Teague</dc:creator>
		<pubDate>Tue, 05 Mar 2013 18:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-252</guid>
		<description>Yeah, it&#039;s an important point.  I&#039;ll edit it to emphasize this.

Thanks!</description>
		<content:encoded><![CDATA[<p>Yeah, it&#8217;s an important point.  I&#8217;ll edit it to emphasize this.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Teague</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-251</link>
		<dc:creator>John Teague</dc:creator>
		<pubDate>Mon, 04 Mar 2013 21:15:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-251</guid>
		<description>yeah, I added it.</description>
		<content:encoded><![CDATA[<p>yeah, I added it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcin</title>
		<link>http://lostechies.com/johnteague/2013/03/03/polymorphism-part-2-refactoring-to-polymorphic-behavior/#comment-250</link>
		<dc:creator>Marcin</dc:creator>
		<pubDate>Mon, 04 Mar 2013 20:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/johnteague/?p=184#comment-250</guid>
		<description>You forgot to implement CalculateServiceFee() in PerUserServiceFee class. Right? ;) </description>
		<content:encoded><![CDATA[<p>You forgot to implement CalculateServiceFee() in PerUserServiceFee class. Right? <img src='http://lostechies.com/johnteague/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
]]></content:encoded>
	</item>
</channel>
</rss>
