<?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: Using Polymorphism instead of Conditionals</title>
	<atom:link href="http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/feed/" rel="self" type="application/rss+xml" />
	<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/</link>
	<description>Thoughts and factoids from the Real Shyam. You know, unlike those fake ones!!</description>
	<lastBuildDate>Wed, 21 Apr 2010 17:05:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: ktn</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-215</link>
		<dc:creator>ktn</dc:creator>
		<pubDate>Mon, 09 Nov 2009 02:27:20 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-215</guid>
		<description>Do you really find tech interviews consisting of algorithm-related questions?  (like what, detail quicksort vs. bubblesort? various flavors of b-trees?)
God, i hope this is an anomaly (i honestly can&#039;t remember when I&#039;ve actually implemented a new general algorithm); I can&#039;t really think of a less insightful question.

There are certainly times when closures are useful, but @Loup&#039;s suggestion is certainly not one of them; there&#039;s another important oo-concept, viz encapsulation, after all</description>
		<content:encoded><![CDATA[<p>Do you really find tech interviews consisting of algorithm-related questions?  (like what, detail quicksort vs. bubblesort? various flavors of b-trees?)<br />
God, i hope this is an anomaly (i honestly can&#8217;t remember when I&#8217;ve actually implemented a new general algorithm); I can&#8217;t really think of a less insightful question.</p>
<p>There are certainly times when closures are useful, but @Loup&#8217;s suggestion is certainly not one of them; there&#8217;s another important oo-concept, viz encapsulation, after all</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Lindal&#8217;s Blog &#187; Encapsulation &#38; Polymorphism</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-196</link>
		<dc:creator>John Lindal&#8217;s Blog &#187; Encapsulation &#38; Polymorphism</dc:creator>
		<pubDate>Sat, 17 Oct 2009 19:03:32 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-196</guid>
		<description>[...] with questions about pointers and functional programming, digging into a person&#8217;s understanding of encapsulation and polymorphism makes for a good technical [...]</description>
		<content:encoded><![CDATA[<p>[...] with questions about pointers and functional programming, digging into a person&#8217;s understanding of encapsulation and polymorphism makes for a good technical [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gowtham</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-178</link>
		<dc:creator>Gowtham</dc:creator>
		<pubDate>Fri, 25 Sep 2009 05:34:28 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-178</guid>
		<description>Also please note that the Design patterns books point at using Polymorphism just for very simple cases of if, else and switch statements as a switch case code smell, so I won&#039;t do this unless I see atleast 2+ repetitions of same conditional. Doing it just for a single conditional would make it speculative generality.</description>
		<content:encoded><![CDATA[<p>Also please note that the Design patterns books point at using Polymorphism just for very simple cases of if, else and switch statements as a switch case code smell, so I won&#8217;t do this unless I see atleast 2+ repetitions of same conditional. Doing it just for a single conditional would make it speculative generality.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shyam</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-175</link>
		<dc:creator>Shyam</dc:creator>
		<pubDate>Wed, 23 Sep 2009 18:06:05 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-175</guid>
		<description>I&#039;m going to try to reply to all the comments in this one.

@argh: greybeard addressed most of my points, but for an example other where it should have been used : http://code.google.com/p/js-test-driver/source/browse/trunk/JsTestDriver/src/com/google/jstestdriver/ui/CapturedBrowsersPanel.java. The contains method on line 80 here totally argues that it needs a FirefoxSlaveBrowser, IESlaveBrowser and it should ahve a getIcon rather than all this. The if conditional would totally go away, and I think it is more readable.

@anonymous: When I ask such a question, I lay down a few requirements first, which I should have mentioned in the blog post. One is, I do tell them that initially we have a few operators, but there might be a need to add more later. And I do ask how it would be extensible in the sense that even if the source code is not directly available, you should be able to add an operation (this might be a later follow up after an initial iteration, of course).

@Eric: For conditional statements, I have more interesting questions, which check his control structure flow. Something which involves both ifs and fors. for 3.) is an interesting idea, any suggestions or examples for that? I am not sure exactly what you are expecting there. Something else I love to ask is what makes classes and methods untestable, or give an example of a horridly untestable class including static initialization blocks and ask how they would test it.

@Loup, I love it, except in Java, functions are not a first class citizen. I love closures, and I wish Java had them. But until then, we are stuck with this approach.</description>
		<content:encoded><![CDATA[<p>I&#8217;m going to try to reply to all the comments in this one.</p>
<p>@argh: greybeard addressed most of my points, but for an example other where it should have been used : <a href="http://code.google.com/p/js-test-driver/source/browse/trunk/JsTestDriver/src/com/google/jstestdriver/ui/CapturedBrowsersPanel.java" rel="nofollow">http://code.google.com/p/js-test-driver/source/browse/trunk/JsTestDriver/src/com/google/jstestdriver/ui/CapturedBrowsersPanel.java</a>. The contains method on line 80 here totally argues that it needs a FirefoxSlaveBrowser, IESlaveBrowser and it should ahve a getIcon rather than all this. The if conditional would totally go away, and I think it is more readable.</p>
<p>@anonymous: When I ask such a question, I lay down a few requirements first, which I should have mentioned in the blog post. One is, I do tell them that initially we have a few operators, but there might be a need to add more later. And I do ask how it would be extensible in the sense that even if the source code is not directly available, you should be able to add an operation (this might be a later follow up after an initial iteration, of course).</p>
<p>@Eric: For conditional statements, I have more interesting questions, which check his control structure flow. Something which involves both ifs and fors. for 3.) is an interesting idea, any suggestions or examples for that? I am not sure exactly what you are expecting there. Something else I love to ask is what makes classes and methods untestable, or give an example of a horridly untestable class including static initialization blocks and ask how they would test it.</p>
<p>@Loup, I love it, except in Java, functions are not a first class citizen. I love closures, and I wish Java had them. But until then, we are stuck with this approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Lindal</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-174</link>
		<dc:creator>John Lindal</dc:creator>
		<pubDate>Wed, 23 Sep 2009 18:03:11 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-174</guid>
		<description>@Loup -- I think you just implemented Eric&#039;s #3.  It works wonderfully in languages like Lisp or JavaScript where functions are first class objects.

To comment on the post itself:


I&#039;m definitely adding this to my interviews, using Eric&#039;s three methods!
I actually wrote a much more complicated version of this when I implemented &lt;a href=&quot;http://newplanetsoftware.com/leibnitz/&quot; rel=&quot;nofollow&quot;&gt;Leibnitz&lt;/a&gt;, my clone of Apple&#039;s Graphing Calculator.  Every item in the parse tree can not only evaluate itself, but also render itself.  Addition and multiplication are actually N-ary, so a single node can sum or multiply an arbitrary number of values.  This helps with rendering parentheses cleanly.
</description>
		<content:encoded><![CDATA[<p>@Loup &#8212; I think you just implemented Eric&#8217;s #3.  It works wonderfully in languages like Lisp or JavaScript where functions are first class objects.</p>
<p>To comment on the post itself:</p>
<p>I&#8217;m definitely adding this to my interviews, using Eric&#8217;s three methods!<br />
I actually wrote a much more complicated version of this when I implemented <a href="http://newplanetsoftware.com/leibnitz/" rel="nofollow">Leibnitz</a>, my clone of Apple&#8217;s Graphing Calculator.  Every item in the parse tree can not only evaluate itself, but also render itself.  Addition and multiplication are actually N-ary, so a single node can sum or multiply an arbitrary number of values.  This helps with rendering parentheses cleanly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loup Vaillant</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-159</link>
		<dc:creator>Loup Vaillant</dc:creator>
		<pubDate>Wed, 23 Sep 2009 14:09:37 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-159</guid>
		<description>Shyam, this example doesn&#039;t call for class polymorphism. It calls for genericity. Your goal is to instantiate several nodes, each with different `eval()` and `to_string()` functions. The simplest way is to treat these functions as ordinary member variables. To let them be parametrers of the `Node()` constructor.

With this scheme, a `Node` is nothing more than a pair of functions : `eval()` and `to_string()`. You don&#039;t even have to use inheritance. Sure, you still have to define `eval()` and `to_string()` for each case, so my solution looks hardly better than yours.

Except when talking about the &quot;step further&quot; you mentioned. I can take this step easily, after the fact, without modifying anything: I just have to write functions which generate `eval()` or `to_string()` for subsets of all possible cases. Such a subset could be the binary operations. I just need to write a `Node gen_eval(Binary_op)` function or somthing.

Parameter passing (here in a constructor) is a simple concept. Even when the &quot;thing&quot; being passed is a function, this is simpler than class polymorphism. The only reason the more complicated, less flexible approach was the right one here is because my approach is more verbose in mainstream OO languages.

The simpler concept is more verbose. Kind of silly, don&#039;t you think?</description>
		<content:encoded><![CDATA[<p>Shyam, this example doesn&#8217;t call for class polymorphism. It calls for genericity. Your goal is to instantiate several nodes, each with different `eval()` and `to_string()` functions. The simplest way is to treat these functions as ordinary member variables. To let them be parametrers of the `Node()` constructor.</p>
<p>With this scheme, a `Node` is nothing more than a pair of functions : `eval()` and `to_string()`. You don&#8217;t even have to use inheritance. Sure, you still have to define `eval()` and `to_string()` for each case, so my solution looks hardly better than yours.</p>
<p>Except when talking about the &#8220;step further&#8221; you mentioned. I can take this step easily, after the fact, without modifying anything: I just have to write functions which generate `eval()` or `to_string()` for subsets of all possible cases. Such a subset could be the binary operations. I just need to write a `Node gen_eval(Binary_op)` function or somthing.</p>
<p>Parameter passing (here in a constructor) is a simple concept. Even when the &#8220;thing&#8221; being passed is a function, this is simpler than class polymorphism. The only reason the more complicated, less flexible approach was the right one here is because my approach is more verbose in mainstream OO languages.</p>
<p>The simpler concept is more verbose. Kind of silly, don&#8217;t you think?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Normand</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-157</link>
		<dc:creator>Eric Normand</dc:creator>
		<pubDate>Wed, 23 Sep 2009 04:29:41 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-157</guid>
		<description>Why not ask him to program it in the three different ways?

1. Polymorphism
2. Conditional statement
3. Data-driven

That would test his versatility, because there are times for each of them.</description>
		<content:encoded><![CDATA[<p>Why not ask him to program it in the three different ways?</p>
<p>1. Polymorphism<br />
2. Conditional statement<br />
3. Data-driven</p>
<p>That would test his versatility, because there are times for each of them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: greybeard</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-156</link>
		<dc:creator>greybeard</dc:creator>
		<pubDate>Wed, 23 Sep 2009 03:15:46 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-156</guid>
		<description>@ argh

The main reason for using polymorphism instead of conditionals/switch-cases is to be able to vary the logic based on discreet selectors.  In this case, it&#039;s operands in an expression tree, but in others it may be how to notify entities when an exception is encountered.  Or what the proper way is to compute a cash disbursement based on potential receivers&#039; account status.  These are just a couple of areas where this style can be applied.

Of course, taken to the extreme, trivial logic ends up segregated from the main flow and readability can be negatively impacted.  Like most things in the programming craft, there are tradeoffs to consider and all that lot.

This example is very well covered in the book &quot;Refactoring&quot; by Fowler and Beck (IIRC).  There are numerous techniques covered that can really help a developer decide when to use these kinds of approaches.

HTH.</description>
		<content:encoded><![CDATA[<p>@ argh</p>
<p>The main reason for using polymorphism instead of conditionals/switch-cases is to be able to vary the logic based on discreet selectors.  In this case, it&#8217;s operands in an expression tree, but in others it may be how to notify entities when an exception is encountered.  Or what the proper way is to compute a cash disbursement based on potential receivers&#8217; account status.  These are just a couple of areas where this style can be applied.</p>
<p>Of course, taken to the extreme, trivial logic ends up segregated from the main flow and readability can be negatively impacted.  Like most things in the programming craft, there are tradeoffs to consider and all that lot.</p>
<p>This example is very well covered in the book &#8220;Refactoring&#8221; by Fowler and Beck (IIRC).  There are numerous techniques covered that can really help a developer decide when to use these kinds of approaches.</p>
<p>HTH.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Using Polymorphism instead of Conditionals « The Shyam!</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-155</link>
		<dc:creator>Using Polymorphism instead of Conditionals « The Shyam!</dc:creator>
		<pubDate>Wed, 23 Sep 2009 00:29:40 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-155</guid>
		<description>[...] rest is here:  Using Polymorphism instead of Conditionals « The Shyam!   SHARETHIS.addEntry({ title: &quot;Using Polymorphism instead of Conditionals « The Shyam!&quot;, url: [...]</description>
		<content:encoded><![CDATA[<p>[...] rest is here:  Using Polymorphism instead of Conditionals « The Shyam!   SHARETHIS.addEntry({ title: &quot;Using Polymorphism instead of Conditionals « The Shyam!&quot;, url: [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Interesting Reads for 2009.09.22 &#124; simplicity</title>
		<link>http://theshyam.com/2009/09/using-polymorphism-instead-of-conditionals/comment-page-1/#comment-154</link>
		<dc:creator>Interesting Reads for 2009.09.22 &#124; simplicity</dc:creator>
		<pubDate>Wed, 23 Sep 2009 00:03:27 +0000</pubDate>
		<guid isPermaLink="false">http://theshyam.com/?p=223#comment-154</guid>
		<description>[...] Using Polymorphism instead of conditionals when programming [...]</description>
		<content:encoded><![CDATA[<p>[...] Using Polymorphism instead of conditionals when programming [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
