<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Strings in switch statements</title>
	<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/</link>
	<description>A tech blog by Luigi R. Viggiano</description>
	<pubDate>Fri, 12 Mar 2010 00:39:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.3</generator>

	<item>
		<title>by: antsbull</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-231736</link>
		<pubDate>Mon, 28 Jul 2008 03:18:46 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-231736</guid>
					<description>What everyone here seems to have missed is that if you need big if..else blocks or switch statements in your code, then your code smells in the first place!  That is a sure indicator of a poor design.  

Polymorphic behaviour is the way to go in these situations (if possible).  if..else..if..else* smells a lot more than switch statements, and performs like a dog at the same time - its a good indicator that you need to re-design your program.

On the point about switch statements, where performance is necessary, then they are the way to go - e.g. game or graphic engines, high-perf real time java, element identification when processing massive XML documents using a SAX parser etc.</description>
		<content:encoded><![CDATA[<p>What everyone here seems to have missed is that if you need big if..else blocks or switch statements in your code, then your code smells in the first place!  That is a sure indicator of a poor design.  </p>
<p>Polymorphic behaviour is the way to go in these situations (if possible).  if..else..if..else* smells a lot more than switch statements, and performs like a dog at the same time - its a good indicator that you need to re-design your program.</p>
<p>On the point about switch statements, where performance is necessary, then they are the way to go - e.g. game or graphic engines, high-perf real time java, element identification when processing massive XML documents using a SAX parser etc.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: peter lawrey</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-213635</link>
		<pubDate>Tue, 01 Jul 2008 05:41:18 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-213635</guid>
					<description>If you can choose the string constants used you can use strings which map to enums which make efficient lookup tables and a readable switch statement.
This is also a handy way of keeping track of the strings used, with compiler validation and keeping them broadly in sync between sender and receiver. (However hyphen would need to be mapped to something like under score)</description>
		<content:encoded><![CDATA[<p>If you can choose the string constants used you can use strings which map to enums which make efficient lookup tables and a readable switch statement.<br />
This is also a handy way of keeping track of the strings used, with compiler validation and keeping them broadly in sync between sender and receiver. (However hyphen would need to be mapped to something like under score)
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ishaaq</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-213084</link>
		<pubDate>Mon, 30 Jun 2008 11:00:28 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-213084</guid>
					<description>Ok, I am going to make a bold claim - only because sometimes you have to make an extreme statement to make a point: a number of &quot;design patterns&quot; are useless in a functional language.

The switch case construct and the foreach construct that another of the posters above disliked so much are used extensively by functional language proponents for a variety of tasks that make generally accepted design patterns redundant and seem more cumbersome once you get used to its strange looking syntax.

Remember that design patterns are not something sacred - they are simply common coding patterns to solve common problems - quite often these patterns are developed from within the context of the limitations of a language - it is a common misconception that design patterns are language agnostic - this is not true. Thankfully, this is not an original thought on my part - I have references to back me up, off the cuff &lt;a href=&quot;http://www.defmacro.org/ramblings/fp.html#part_7&quot; rel=&quot;nofollow&quot;&gt;here's one.&lt;/a&gt;

As for the switch case itself - to see how it and its more advanced functional brother: pattern matching are used extensively and quite usefully you'll need to look at functional language implementations - the topic is too complex for me to explain in a short reply here, suffice to say that once you get used to functional programming you'll find yourself frustrated with non-functional programming languages. Since you're obviously coming from a Java background I'd suggest you start with Scala - it takes a happy medium between OO and functional paradigms and even runs in the JVM and is interoperable with Java.

My earlier comment with Paul Graham's fictitious &quot;Blub&quot; programming language was not in jest - I used to be where you are right now - not understanding why Java needed any changes and now I am on the other side and I understand why. There is a very fundamental reason why Java needs to change or die - it is the imminent software crisis that we are almost in in the midst of facing - multi-core programming. I admit it is a long shot, from switch-case statements to multi-core programming - but there is a connection - functional languages.</description>
		<content:encoded><![CDATA[<p>Ok, I am going to make a bold claim - only because sometimes you have to make an extreme statement to make a point: a number of &#8220;design patterns&#8221; are useless in a functional language.</p>
<p>The switch case construct and the foreach construct that another of the posters above disliked so much are used extensively by functional language proponents for a variety of tasks that make generally accepted design patterns redundant and seem more cumbersome once you get used to its strange looking syntax.</p>
<p>Remember that design patterns are not something sacred - they are simply common coding patterns to solve common problems - quite often these patterns are developed from within the context of the limitations of a language - it is a common misconception that design patterns are language agnostic - this is not true. Thankfully, this is not an original thought on my part - I have references to back me up, off the cuff <a href="http://www.defmacro.org/ramblings/fp.html#part_7" rel="nofollow">here&#8217;s one.</a></p>
<p>As for the switch case itself - to see how it and its more advanced functional brother: pattern matching are used extensively and quite usefully you&#8217;ll need to look at functional language implementations - the topic is too complex for me to explain in a short reply here, suffice to say that once you get used to functional programming you&#8217;ll find yourself frustrated with non-functional programming languages. Since you&#8217;re obviously coming from a Java background I&#8217;d suggest you start with Scala - it takes a happy medium between OO and functional paradigms and even runs in the JVM and is interoperable with Java.</p>
<p>My earlier comment with Paul Graham&#8217;s fictitious &#8220;Blub&#8221; programming language was not in jest - I used to be where you are right now - not understanding why Java needed any changes and now I am on the other side and I understand why. There is a very fundamental reason why Java needs to change or die - it is the imminent software crisis that we are almost in in the midst of facing - multi-core programming. I admit it is a long shot, from switch-case statements to multi-core programming - but there is a connection - functional languages.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luigi</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-213030</link>
		<pubDate>Mon, 30 Jun 2008 09:01:43 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-213030</guid>
					<description>I think patterns should be used carefully, but notice that many of them can be used to replace smelling switch or if-else-if-else statements to implement a common behavior: 

chain of responsibility. for instance jdbc: you don't switch on the proper jdbc driver when you specify an url to the driver manager.

command pattern. for instance struts: you have a list of configured commands attached to a discriminator url. This avoid hard coding command triggers to switching on command implementations.

strategy: you have a list of algorithms that are injected in the executors, instead of having a switch in the executor to select the appropriate algorithm

observer: you have a set of classes that handle specific messages, instead of having a single class that switch on the messages to notify the appropriate listeners

If switch is perfect, it is quite strange that people invented so many patterns to avoid that (in different contexts, and for solving different problems). :)

I agree that sometime if-else-if-else are simpler than overcomplicated solutions. But most of the time doing a good design avoids switches and repeated ifs. My opinion is that is best to go for the easiest code.</description>
		<content:encoded><![CDATA[<p>I think patterns should be used carefully, but notice that many of them can be used to replace smelling switch or if-else-if-else statements to implement a common behavior: </p>
<p>chain of responsibility. for instance jdbc: you don&#8217;t switch on the proper jdbc driver when you specify an url to the driver manager.</p>
<p>command pattern. for instance struts: you have a list of configured commands attached to a discriminator url. This avoid hard coding command triggers to switching on command implementations.</p>
<p>strategy: you have a list of algorithms that are injected in the executors, instead of having a switch in the executor to select the appropriate algorithm</p>
<p>observer: you have a set of classes that handle specific messages, instead of having a single class that switch on the messages to notify the appropriate listeners</p>
<p>If switch is perfect, it is quite strange that people invented so many patterns to avoid that (in different contexts, and for solving different problems). <img src='http://en.newinstance.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I agree that sometime if-else-if-else are simpler than overcomplicated solutions. But most of the time doing a good design avoids switches and repeated ifs. My opinion is that is best to go for the easiest code.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ishaaq</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212738</link>
		<pubDate>Mon, 30 Jun 2008 00:16:57 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212738</guid>
					<description>Hmm, maybe you're right - you are becoming a dinosaur.

&quot;Blub falls right in the middle of the abstractness continuum... As long as our hypothetical Blub programmer is looking down the power continuum, he knows he's looking down. Languages less powerful than Blub are obviously less powerful, because they're missing some feature he's used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn't realize he's looking up. What he sees are merely weird languages... Blub is good enough for him, because he thinks in Blub.&quot; -- Paul Graham, Beating the Averages</description>
		<content:encoded><![CDATA[<p>Hmm, maybe you&#8217;re right - you are becoming a dinosaur.</p>
<p>&#8220;Blub falls right in the middle of the abstractness continuum&#8230; As long as our hypothetical Blub programmer is looking down the power continuum, he knows he&#8217;s looking down. Languages less powerful than Blub are obviously less powerful, because they&#8217;re missing some feature he&#8217;s used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn&#8217;t realize he&#8217;s looking up. What he sees are merely weird languages&#8230; Blub is good enough for him, because he thinks in Blub.&#8221; &#8212; Paul Graham, Beating the Averages
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tony</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212371</link>
		<pubDate>Sun, 29 Jun 2008 12:19:13 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212371</guid>
					<description>Switch is only for J2ME...is bad form in J2SE?  Are you kidding?  There's absolutely nothing wrong with using switch blocks in any code, especially if using enums, which were made to work within a switch block.  I'd say switching to another language, rather than first attempting to optimize your own code is bad form.</description>
		<content:encoded><![CDATA[<p>Switch is only for J2ME&#8230;is bad form in J2SE?  Are you kidding?  There&#8217;s absolutely nothing wrong with using switch blocks in any code, especially if using enums, which were made to work within a switch block.  I&#8217;d say switching to another language, rather than first attempting to optimize your own code is bad form.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Vladimir Tsukur</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212263</link>
		<pubDate>Sun, 29 Jun 2008 09:54:21 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212263</guid>
					<description>I agree with Luigi, and NEVER use &lt;code&gt;switch&lt;/code&gt; because there is always a possibility to replace such code smell with &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;Map&lt;/code&gt; based implementation (last option is much better).</description>
		<content:encoded><![CDATA[<p>I agree with Luigi, and NEVER use <code>switch</code> because there is always a possibility to replace such code smell with <code>if</code> or <code>Map</code> based implementation (last option is much better).
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Daniel</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212148</link>
		<pubDate>Sun, 29 Jun 2008 04:59:49 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212148</guid>
					<description>I think the current fashion of pushing all kinds of features in Java is a mistake. Only because language X managed to cram more features in the language is not going to make language X more successful. Unfortunately damage has been done already. Because features were pushed in and they were half-baked. Like the &quot;foreach&quot; statement which can be used only for read but not for change. Line the &quot;erased&quot; generics and so on. Personally I don't care if I have to type a bit more if things are clear. Yes Java is a more verbose language than some others. But for the kind of success Java seeks this is not important. The typing is such a small part of the development.</description>
		<content:encoded><![CDATA[<p>I think the current fashion of pushing all kinds of features in Java is a mistake. Only because language X managed to cram more features in the language is not going to make language X more successful. Unfortunately damage has been done already. Because features were pushed in and they were half-baked. Like the &#8220;foreach&#8221; statement which can be used only for read but not for change. Line the &#8220;erased&#8221; generics and so on. Personally I don&#8217;t care if I have to type a bit more if things are clear. Yes Java is a more verbose language than some others. But for the kind of success Java seeks this is not important. The typing is such a small part of the development.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: wentman</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212116</link>
		<pubDate>Sun, 29 Jun 2008 00:32:22 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212116</guid>
					<description>The purpose of the switch statement is more for j2me.  Switching uses a jump table, so only 2 comparisons is needed for any switch, whereas a if else if else if is many comparisons and therefore much slower.  For j2se and j2ee, it is bad form unless you really need the performance boost, but in that case(yes the pun is intended), you should move to c  .</description>
		<content:encoded><![CDATA[<p>The purpose of the switch statement is more for j2me.  Switching uses a jump table, so only 2 comparisons is needed for any switch, whereas a if else if else if is many comparisons and therefore much slower.  For j2se and j2ee, it is bad form unless you really need the performance boost, but in that case(yes the pun is intended), you should move to c  .
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luigi</title>
		<link>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212003</link>
		<pubDate>Sat, 28 Jun 2008 19:58:24 +0000</pubDate>
		<guid>http://en.newinstance.it/2008/06/28/strings-in-switch-statements/#comment-212003</guid>
					<description>why switch is not so good: http://www.c2.com/cgi/wiki?SwitchStatementsSmell</description>
		<content:encoded><![CDATA[<p>why switch is not so good: <a href='http://www.c2.com/cgi/wiki?SwitchStatementsSmell' rel='nofollow'>http://www.c2.com/cgi/wiki?SwitchStatementsSmell</a>
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
