<?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: How to avoid switch-case syndrome</title>
	<atom:link href="http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 02 Feb 2012 18:03:26 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: Floby</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-17927</link>
		<dc:creator>Floby</dc:creator>
		<pubDate>Sun, 09 Aug 2009 22:21:09 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-17927</guid>
		<description>@kzz : You&#039;ll need to change this code, indeed. It simply doesn&#039;t work, the value between the &#039;case&#039; and the &#039;:&#039; must be a constant value which is then compared (==) with the subject of the switch statement (switch(subject)). You may want to use a &quot;else-if&quot; structure.
&lt;pre lang=&quot;javascript&quot;&gt;
if(i &lt; 0) {
  //something
}
else if(i == 0) {
  //something else
}
else {  //use as default
  window.dump(&#039;i is strictly superior to 0, since i = &#039; + i);
}

&lt;/pre&gt;
I often use this in the same conditions as a switch but when I know that the first conditions are more likely to happen.</description>
		<content:encoded><![CDATA[<p>@kzz : You&#8217;ll need to change this code, indeed. It simply doesn&#8217;t work, the value between the &#8216;case&#8217; and the &#8216;:&#8217; must be a constant value which is then compared (==) with the subject of the switch statement (switch(subject)). You may want to use a &#8220;else-if&#8221; structure.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw1">if</span><span class="br0">&#40;</span>i <span class="sy0">&amp;</span>lt<span class="sy0">;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="co1">//something</span>
<span class="br0">&#125;</span>
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>i <span class="sy0">==</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="co1">//something else</span>
<span class="br0">&#125;</span>
<span class="kw1">else</span> <span class="br0">&#123;</span>  <span class="co1">//use as default</span>
  window.<span class="me1">dump</span><span class="br0">&#40;</span><span class="st0">'i is strictly superior to 0, since i = '</span> <span class="sy0">+</span> i<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>I often use this in the same conditions as a switch but when I know that the first conditions are more likely to happen.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Kirk</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-17205</link>
		<dc:creator>Martin Kirk</dc:creator>
		<pubDate>Mon, 27 Jul 2009 09:40:40 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-17205</guid>
		<description>... and because the lookup is used 2 times to find the element in the object.</description>
		<content:encoded><![CDATA[<p>&#8230; and because the lookup is used 2 times to find the element in the object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Kirk</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-17204</link>
		<dc:creator>Martin Kirk</dc:creator>
		<pubDate>Mon, 27 Jul 2009 09:35:25 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-17204</guid>
		<description>excuse me, but this MacGyver hack&#039;on&#039;crack code isn&#039;t very good...

there is very good reasons when to use switch-case:
- when performance is an issue
- when each token i distinct (or nearly)
- when the context matters
- when other people read your code (as stated by Antonio)

i&#039;ve used switch-case when formulating my code-parser in this way (which is the fastest and best way to do so)

&lt;pre lang=&quot;javascript&quot;&gt;
var tokens = [
	&#039;distinct1&#039;,&#039;distinct2&#039;,&#039;distinct3&#039;,
	&#039;mixed1&#039;,&#039;mixed2&#039;,
	&#039;various&#039;,&#039;undefined&#039;,&#039;crap&#039;
]

var context = &quot;&quot;;

for(var i=0,token; (token=tokens[i]); i++) //unsafe code !!
{
	switch(token)
	{
		case &#039;distinct1&#039;: context = do1(); break;
		case &#039;distinct2&#039;: context = do2(); break;
		case &#039;distinct3&#039;: context = do3(); break;
		case &#039;mixed1&#039;:
		case &#039;mixed2&#039;: context = doMix(); break;
		default:
			if(token == &#039;various&#039;){
				context = doVar();
			}else if(token == &#039;undefined&#039;){
				context = doUndef();
			}else if(token == &#039;crap&#039;){
				context = doCrap();
			}else{
				//somethingElse()
			}
			break;
	}
}
&lt;/pre&gt;

which performs REALLY good, and carries the context throughout the switch selector... 

ofcause you could upgrade the lookup to take the context on call:

&lt;pre lang=&quot;javascript&quot;&gt;

function doX(context){ return context; }
function doY(context){ return context; }
function doN(context){ return context; }

var cases = {
    1: doX,
    2: doY,
    3: doN
};

if (cases[something]) {
    context = cases[something](context);
}
&lt;/pre&gt;

but wouldn&#039;t perform as good as the switch-case because of the copy of context in each call..</description>
		<content:encoded><![CDATA[<p>excuse me, but this MacGyver hack&#8217;on&#8217;crack code isn&#8217;t very good&#8230;</p>
<p>there is very good reasons when to use switch-case:<br />
- when performance is an issue<br />
- when each token i distinct (or nearly)<br />
- when the context matters<br />
- when other people read your code (as stated by Antonio)</p>
<p>i&#8217;ve used switch-case when formulating my code-parser in this way (which is the fastest and best way to do so)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> tokens <span class="sy0">=</span> <span class="br0">&#91;</span>
	<span class="st0">'distinct1'</span><span class="sy0">,</span><span class="st0">'distinct2'</span><span class="sy0">,</span><span class="st0">'distinct3'</span><span class="sy0">,</span>
	<span class="st0">'mixed1'</span><span class="sy0">,</span><span class="st0">'mixed2'</span><span class="sy0">,</span>
	<span class="st0">'various'</span><span class="sy0">,</span><span class="st0">'undefined'</span><span class="sy0">,</span><span class="st0">'crap'</span>
<span class="br0">&#93;</span>
&nbsp;
<span class="kw2">var</span> context <span class="sy0">=</span> <span class="st0">&quot;&quot;</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span>token<span class="sy0">;</span> <span class="br0">&#40;</span>token<span class="sy0">=</span>tokens<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">&#41;</span> <span class="co1">//unsafe code !!</span>
<span class="br0">&#123;</span>
	<span class="kw1">switch</span><span class="br0">&#40;</span>token<span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">case</span> <span class="st0">'distinct1'</span><span class="sy0">:</span> context <span class="sy0">=</span> do1<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="kw1">break</span><span class="sy0">;</span>
		<span class="kw1">case</span> <span class="st0">'distinct2'</span><span class="sy0">:</span> context <span class="sy0">=</span> do2<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="kw1">break</span><span class="sy0">;</span>
		<span class="kw1">case</span> <span class="st0">'distinct3'</span><span class="sy0">:</span> context <span class="sy0">=</span> do3<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="kw1">break</span><span class="sy0">;</span>
		<span class="kw1">case</span> <span class="st0">'mixed1'</span><span class="sy0">:</span>
		<span class="kw1">case</span> <span class="st0">'mixed2'</span><span class="sy0">:</span> context <span class="sy0">=</span> doMix<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="kw1">break</span><span class="sy0">;</span>
		<span class="kw2">default</span><span class="sy0">:</span>
			<span class="kw1">if</span><span class="br0">&#40;</span>token <span class="sy0">==</span> <span class="st0">'various'</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
				context <span class="sy0">=</span> doVar<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
			<span class="br0">&#125;</span><span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>token <span class="sy0">==</span> <span class="st0">'undefined'</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
				context <span class="sy0">=</span> doUndef<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
			<span class="br0">&#125;</span><span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>token <span class="sy0">==</span> <span class="st0">'crap'</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
				context <span class="sy0">=</span> doCrap<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
			<span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span>
				<span class="co1">//somethingElse()</span>
			<span class="br0">&#125;</span>
			<span class="kw1">break</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>which performs REALLY good, and carries the context throughout the switch selector&#8230; </p>
<p>ofcause you could upgrade the lookup to take the context on call:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span class="kw2">function</span> doX<span class="br0">&#40;</span>context<span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="kw1">return</span> context<span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="kw2">function</span> doY<span class="br0">&#40;</span>context<span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="kw1">return</span> context<span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="kw2">function</span> doN<span class="br0">&#40;</span>context<span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="kw1">return</span> context<span class="sy0">;</span> <span class="br0">&#125;</span>
&nbsp;
<span class="kw2">var</span> cases <span class="sy0">=</span> <span class="br0">&#123;</span>
    <span class="nu0">1</span><span class="sy0">:</span> doX<span class="sy0">,</span>
    <span class="nu0">2</span><span class="sy0">:</span> doY<span class="sy0">,</span>
    <span class="nu0">3</span><span class="sy0">:</span> doN
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">if</span> <span class="br0">&#40;</span>cases<span class="br0">&#91;</span>something<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    context <span class="sy0">=</span> cases<span class="br0">&#91;</span>something<span class="br0">&#93;</span><span class="br0">&#40;</span>context<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>but wouldn&#8217;t perform as good as the switch-case because of the copy of context in each call..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antonio Max</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-16854</link>
		<dc:creator>Antonio Max</dc:creator>
		<pubDate>Tue, 21 Jul 2009 14:26:47 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-16854</guid>
		<description>Yeah! Lets Re-invent the wheel!
The switch is much more elegant than if/else. Also, it haves a default statement, witch provides more security on web apps.
There&#039;s a reason for it to be there.
That&#039;s a nice approach the one you wrote, but its only useful on some very specific situations.
BTW, I think standards are a nice way to share code among other developers and other coders who might have to mess up with your code and will not need to &#039;translate&#039; your logic.</description>
		<content:encoded><![CDATA[<p>Yeah! Lets Re-invent the wheel!<br />
The switch is much more elegant than if/else. Also, it haves a default statement, witch provides more security on web apps.<br />
There&#8217;s a reason for it to be there.<br />
That&#8217;s a nice approach the one you wrote, but its only useful on some very specific situations.<br />
BTW, I think standards are a nice way to share code among other developers and other coders who might have to mess up with your code and will not need to &#8216;translate&#8217; your logic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-16832</link>
		<dc:creator>David</dc:creator>
		<pubDate>Tue, 21 Jul 2009 08:06:24 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-16832</guid>
		<description>@Dusan - Very interesting article, thanx for the link! With a quick hack to allow single or array alternatives, the &quot;cond&quot; function that DBJ defines could be made to accomodate Kzz&#039; example above:

&lt;pre lang=&quot;javascript&quot; line=&quot;&quot; escaped=&quot;&quot;&gt;
function cond( v ) {
  var i;
  for (i=1 ; i &lt; arguments.length-1; i+=2 ) {
  	var vals = typeof arguments[i] == &quot;object&quot; ? arguments[i] : [arguments[i]];
	for (var j = 0; j&lt;vals.length; j++)
	  if (v===vals[j]) 
	    return arguments[i + 1];
  }
  return arguments[i];
}
&lt;/pre&gt;

Fallthrough could now be emulated giving an array for some values:

&lt;pre lang=&quot;javascript&quot;&gt;
cond(x,2,&quot;green&quot;,[3,4,5],&quot;yellow&quot;,&quot;defaultclr&quot;);
&lt;/pre&gt;

And kzz conditional bit could be done like so:

&lt;pre lang=&quot;javascript&quot;&gt;
cond(true,x&lt;2,&quot;green&quot;,x&lt;7,&quot;red&quot;,&quot;defaultclr&quot;);
&lt;/pre&gt;

Finally, James&#039; original function lookup example (although missing the defined check):

&lt;pre lang=&quot;javascript&quot;&gt;
cond(something,1,doX,2,doY,3,doN)();
&lt;/pre&gt;

Quite cute, I think! Although, as DBJ writes in his article, the actual value is obviously debatable.

...argh, sorry about the messed-up &gt; and &lt; ! James, help, how do I prevent that?</description>
		<content:encoded><![CDATA[<p>@Dusan &#8211; Very interesting article, thanx for the link! With a quick hack to allow single or array alternatives, the &#8220;cond&#8221; function that DBJ defines could be made to accomodate Kzz&#8217; example above:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> cond<span class="br0">&#40;</span> v <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw2">var</span> i<span class="sy0">;</span>
  <span class="kw1">for</span> <span class="br0">&#40;</span>i<span class="sy0">=</span><span class="nu0">1</span> <span class="sy0">;</span> i <span class="sy0">&amp;</span>lt<span class="sy0">;</span> arguments.<span class="me1">length</span><span class="sy0">-</span><span class="nu0">1</span><span class="sy0">;</span> i<span class="sy0">+=</span><span class="nu0">2</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  	<span class="kw2">var</span> vals <span class="sy0">=</span> <span class="kw1">typeof</span> arguments<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">==</span> <span class="st0">&quot;object&quot;</span> <span class="sy0">?</span> arguments<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">:</span> <span class="br0">&#91;</span>arguments<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> j <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> j<span class="sy0">&amp;</span>lt<span class="sy0">;</span>vals.<span class="me1">length</span><span class="sy0">;</span> j<span class="sy0">++</span><span class="br0">&#41;</span>
	  <span class="kw1">if</span> <span class="br0">&#40;</span>v<span class="sy0">===</span>vals<span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="br0">&#41;</span> 
	    <span class="kw1">return</span> arguments<span class="br0">&#91;</span>i <span class="sy0">+</span> <span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
  <span class="kw1">return</span> arguments<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Fallthrough could now be emulated giving an array for some values:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">cond<span class="br0">&#40;</span>x<span class="sy0">,</span><span class="nu0">2</span><span class="sy0">,</span><span class="st0">&quot;green&quot;</span><span class="sy0">,</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="sy0">,</span><span class="nu0">4</span><span class="sy0">,</span><span class="nu0">5</span><span class="br0">&#93;</span><span class="sy0">,</span><span class="st0">&quot;yellow&quot;</span><span class="sy0">,</span><span class="st0">&quot;defaultclr&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>And kzz conditional bit could be done like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">cond<span class="br0">&#40;</span><span class="kw2">true</span><span class="sy0">,</span>x<span class="sy0">&amp;</span>lt<span class="sy0">;</span><span class="nu0">2</span><span class="sy0">,</span><span class="st0">&quot;green&quot;</span><span class="sy0">,</span>x<span class="sy0">&amp;</span>lt<span class="sy0">;</span><span class="nu0">7</span><span class="sy0">,</span><span class="st0">&quot;red&quot;</span><span class="sy0">,</span><span class="st0">&quot;defaultclr&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Finally, James&#8217; original function lookup example (although missing the defined check):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">cond<span class="br0">&#40;</span>something<span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span>doX<span class="sy0">,</span><span class="nu0">2</span><span class="sy0">,</span>doY<span class="sy0">,</span><span class="nu0">3</span><span class="sy0">,</span>doN<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Quite cute, I think! Although, as DBJ writes in his article, the actual value is obviously debatable.</p>
<p>&#8230;argh, sorry about the messed-up &gt; and &lt; ! James, help, how do I prevent that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dusan</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-16589</link>
		<dc:creator>Dusan</dc:creator>
		<pubDate>Thu, 16 Jul 2009 23:35:08 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-16589</guid>
		<description>http://dbj.org/dbj/?p=119

I think you might find this an interesting angle on the same theme ...

--DBJ</description>
		<content:encoded><![CDATA[<p><a href="http://dbj.org/dbj/?p=119">http://dbj.org/dbj/?p=119</a></p>
<p>I think you might find this an interesting angle on the same theme &#8230;</p>
<p>&#8211;DBJ</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kzz</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-16463</link>
		<dc:creator>kzz</dc:creator>
		<pubDate>Wed, 15 Jul 2009 04:06:14 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-16463</guid>
		<description>Great!
But,how to transfer this to your method?

&lt;pre lang=&quot;javascript&quot;&gt;
switch(i){
  case i &lt; 0:
    alert(&#039; 0;
    alert(&#039;&gt;0&#039;);break;
  default:
    alert(&#039;undefine&#039;);break;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Great!<br />
But,how to transfer this to your method?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw1">switch</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#123;</span>
  <span class="kw1">case</span> i <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="sy0">:</span>
    <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">' 0;
    alert('</span><span class="sy0">&gt;</span><span class="nu0">0</span><span class="st0">');break;
  default:
    alert('</span>undefine<span class="st0">');break;
}</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Haim Michael</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-15875</link>
		<dc:creator>Haim Michael</dc:creator>
		<pubDate>Tue, 07 Jul 2009 09:27:12 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-15875</guid>
		<description>I strongly recommend on learning the alternatives using object oriented code... not sure all of them will apply for JavaScript... though they will defently be relevant for PHP, Java &amp; C#. Best, Haim.</description>
		<content:encoded><![CDATA[<p>I strongly recommend on learning the alternatives using object oriented code&#8230; not sure all of them will apply for JavaScript&#8230; though they will defently be relevant for PHP, Java &amp; C#. Best, Haim.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kangax</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-15834</link>
		<dc:creator>kangax</dc:creator>
		<pubDate>Mon, 06 Jul 2009 16:34:24 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-15834</guid>
		<description>It&#039;s a good idea to mention that Javascript objects are quite lousy at emulating proper hash tables, due to their prototypical nature and inability to modify their internal prototype chains (non-standard implementations aside).

A popular example is probably &quot;toString&quot;/&quot;valueOf&quot; keys which, when accessed on a plain object, resolve to `Object.prototype.toString/valueOf` respectively. Keys, matching other `Object.prototype.*` properties (either built-in or user-defined) have similar outcome. 

Explicitly modifying keys before property access usually solves this &quot;problem&quot; nicely, albeit in expense of additional overhead. Other solutions, such as `hasOwnProperty`, are either not widely supported (e.g. Safari 2 lacks it) or are not reliable (e.g. `hasOwnProperty(&#039;__proto__&#039;)` is `true` on plain objects in Gecko-based browsers).</description>
		<content:encoded><![CDATA[<p>It&#8217;s a good idea to mention that Javascript objects are quite lousy at emulating proper hash tables, due to their prototypical nature and inability to modify their internal prototype chains (non-standard implementations aside).</p>
<p>A popular example is probably &#8220;toString&#8221;/&#8221;valueOf&#8221; keys which, when accessed on a plain object, resolve to `Object.prototype.toString/valueOf` respectively. Keys, matching other `Object.prototype.*` properties (either built-in or user-defined) have similar outcome. </p>
<p>Explicitly modifying keys before property access usually solves this &#8220;problem&#8221; nicely, albeit in expense of additional overhead. Other solutions, such as `hasOwnProperty`, are either not widely supported (e.g. Safari 2 lacks it) or are not reliable (e.g. `hasOwnProperty(&#8216;__proto__&#8217;)` is `true` on plain objects in Gecko-based browsers).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/comment-page-1/#comment-15724</link>
		<dc:creator>James</dc:creator>
		<pubDate>Sun, 05 Jul 2009 15:30:11 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=989#comment-15724</guid>
		<description>@A, thanks for taking the time to write your comment. I understand that my proposal, and generally the idea of lookup tables may not easily compare to the speed of native switch statements but I still think that using objects to store conditions is cleaner and can be the better solution in certain situations.

Plus, web programming cannot really be prepared to any other type of programming (like &quot;Real time airplane safety&quot;). The basic principles may still hold true but there is a much higher demand for code customization on the web. Storing conditions extends this principle; it allows you and other contributors to change/debug conditions without having to go digging through the source looking through line after line of cases. 

Speed/performance, especially when operating at such a high abstraction layer, is largely moot. I understand that we still need to take these things into consideration but today, right now, I consider readability and general code maintainability to be a much higher priority. The question of whether the syntax is cleaner is subjective and therefore really isn&#039;t worth mentioning.

@unscriptable, another way:

&lt;pre lang=&quot;javascript&quot;&gt;
var focusMethod = {
    1: function(){ input1.focus(); },
    2: function(){ input2.focus(); },
    3: function(){ input3.focus(); }
};
&lt;/pre&gt;

Multiple return paths may annoy you, but this is even worse IMO:

&lt;pre lang=&quot;javascript&quot;&gt;
function something() {
    
    var retValue;
    
    if (true) {
        retValue = 123;
    } else {
        if (true) {
            retValue = 567;
        } else {
            retValue = 234;
        }
    }
    
    return retValue;
}
&lt;/pre&gt;

I know, I know, it&#039;s subjective, but I just really hate this... It looks like the author wanted to follow the &quot;single entry, single exit&quot; rule regardless of what was practical. What&#039;s so bad about multiple return paths anyway? - sure, over-usage is ugly, but if used wisely it&#039;s not that bad IMO.</description>
		<content:encoded><![CDATA[<p>@A, thanks for taking the time to write your comment. I understand that my proposal, and generally the idea of lookup tables may not easily compare to the speed of native switch statements but I still think that using objects to store conditions is cleaner and can be the better solution in certain situations.</p>
<p>Plus, web programming cannot really be prepared to any other type of programming (like &#8220;Real time airplane safety&#8221;). The basic principles may still hold true but there is a much higher demand for code customization on the web. Storing conditions extends this principle; it allows you and other contributors to change/debug conditions without having to go digging through the source looking through line after line of cases. </p>
<p>Speed/performance, especially when operating at such a high abstraction layer, is largely moot. I understand that we still need to take these things into consideration but today, right now, I consider readability and general code maintainability to be a much higher priority. The question of whether the syntax is cleaner is subjective and therefore really isn&#8217;t worth mentioning.</p>
<p>@unscriptable, another way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> focusMethod <span class="sy0">=</span> <span class="br0">&#123;</span>
    <span class="nu0">1</span><span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span> input1.<span class="kw3">focus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">,</span>
    <span class="nu0">2</span><span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span> input2.<span class="kw3">focus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">,</span>
    <span class="nu0">3</span><span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span> input3.<span class="kw3">focus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div>

<p>Multiple return paths may annoy you, but this is even worse IMO:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> something<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">var</span> retValue<span class="sy0">;</span>
&nbsp;
    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        retValue <span class="sy0">=</span> <span class="nu0">123</span><span class="sy0">;</span>
    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            retValue <span class="sy0">=</span> <span class="nu0">567</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            retValue <span class="sy0">=</span> <span class="nu0">234</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw1">return</span> retValue<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>I know, I know, it&#8217;s subjective, but I just really hate this&#8230; It looks like the author wanted to follow the &#8220;single entry, single exit&#8221; rule regardless of what was practical. What&#8217;s so bad about multiple return paths anyway? &#8211; sure, over-usage is ugly, but if used wisely it&#8217;s not that bad IMO.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

