<?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: A Less Ugly Switch Statement For C#</title>
	<atom:link href="http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/</link>
	<description>Better Than Yesterday</description>
	<lastBuildDate>Fri, 24 May 2013 06:39: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: :o)</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-3101</link>
		<dc:creator>:o)</dc:creator>
		<pubDate>Fri, 08 Mar 2013 12:14:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-3101</guid>
		<description>Wow. You&#039;ve removed a few curly braces but introduced unnessecary complexity in the form of lambdas and degraded performance. The most worry thing though is you&#039;ve obfuscated a basic control flow operation understood by everyone which will lead to head scratching for any developer that takes on the code after you.


Worth it just to get rid of a few braces and breaks?</description>
		<content:encoded><![CDATA[<p>Wow. You&#8217;ve removed a few curly braces but introduced unnessecary complexity in the form of lambdas and degraded performance. The most worry thing though is you&#8217;ve obfuscated a basic control flow operation understood by everyone which will lead to head scratching for any developer that takes on the code after you.</p>
<p>Worth it just to get rid of a few braces and breaks?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: thinkbeforecoding</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1230</link>
		<dc:creator>thinkbeforecoding</dc:creator>
		<pubDate>Mon, 25 Oct 2010 10:04:49 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1230</guid>
		<description>One of the things you can do with such construct is to create a functional switch, ie a switch whose result is a value instead of a switch between statement. 
The if statement as the ?: expression, but the switch statement hasn&#039;t.

It&#039;s very usefull when building trees with Linq to Xml...</description>
		<content:encoded><![CDATA[<p>One of the things you can do with such construct is to create a functional switch, ie a switch whose result is a value instead of a switch between statement.<br />
The if statement as the ?: expression, but the switch statement hasn&#8217;t.</p>
<p>It&#8217;s very usefull when building trees with Linq to Xml&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Clarke</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1229</link>
		<dc:creator>David Clarke</dc:creator>
		<pubDate>Thu, 14 Oct 2010 19:54:26 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1229</guid>
		<description>Nice post about one of my pet peeves with C#. When the opportunity presented to fix the C switch statement, what happened instead? It got more broke! C# switch has no fall through which I don&#039;t object to; the small number of scenarios where fall through might be useful aren&#039;t a good reason to keep semantics that are responsible for a vast number of defects. So given there is no fall through, why the hell do we need to use a &quot;break&quot; to exit the current condition. Surely it would have been better to use the standard block semantics which you hint at in your post by wrapping braces round the conditional statements. Without braces - execute single statement and exit switch, with braces - execute block and exit. Crying. Spilt. Milk.

When you suggest the performance of your fluent code is an unnoticeable amount slower than a switch statement, you may be correct where the code is executed infrequently. But if the switch is inside a loop that slight difference can turn into a substantial delay. As always, use with caution.</description>
		<content:encoded><![CDATA[<p>Nice post about one of my pet peeves with C#. When the opportunity presented to fix the C switch statement, what happened instead? It got more broke! C# switch has no fall through which I don&#8217;t object to; the small number of scenarios where fall through might be useful aren&#8217;t a good reason to keep semantics that are responsible for a vast number of defects. So given there is no fall through, why the hell do we need to use a &#8220;break&#8221; to exit the current condition. Surely it would have been better to use the standard block semantics which you hint at in your post by wrapping braces round the conditional statements. Without braces &#8211; execute single statement and exit switch, with braces &#8211; execute block and exit. Crying. Spilt. Milk.</p>
<p>When you suggest the performance of your fluent code is an unnoticeable amount slower than a switch statement, you may be correct where the code is executed infrequently. But if the switch is inside a loop that slight difference can turn into a substantial delay. As always, use with caution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris McGrath</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1228</link>
		<dc:creator>Chris McGrath</dc:creator>
		<pubDate>Mon, 11 Oct 2010 23:40:24 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1228</guid>
		<description>Ok, It has it&#039;s place (for things that aren&#039;t switchable) but you loose so much power (when you want to run the same code for 2 cases), it is slower and if you did the switch statement correctly, then your solution is uglier.
As someone already pointed out, if you remove the braces, put it all on 1 line you have a lighter weight version of what you created.</description>
		<content:encoded><![CDATA[<p>Ok, It has it&#8217;s place (for things that aren&#8217;t switchable) but you loose so much power (when you want to run the same code for 2 cases), it is slower and if you did the switch statement correctly, then your solution is uglier.<br />
As someone already pointed out, if you remove the braces, put it all on 1 line you have a lighter weight version of what you created.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Kemp</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1227</link>
		<dc:creator>Chris Kemp</dc:creator>
		<pubDate>Sat, 09 Oct 2010 14:55:11 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1227</guid>
		<description>the important point I missed from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#039;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good  post.</description>
		<content:encoded><![CDATA[<p>the important point I missed from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#8217;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good  post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Kemp</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1226</link>
		<dc:creator>Chris Kemp</dc:creator>
		<pubDate>Sat, 09 Oct 2010 14:51:33 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1226</guid>
		<description>the important point I mused from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#039;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good post.the important point I mused from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#039;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good post.the important point I mused from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#039;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good post.</description>
		<content:encoded><![CDATA[<p>the important point I mused from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#8217;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good post.the important point I mused from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#8217;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good post.the important point I mused from my earlier post is that your solution still violates the open closed principe because to support a new scanned asset state you have to amend your new switch statement, so unfortunately you&#8217;ve failed to the core goal of better adherence to solid principles. still, thanks for making me ask Why switch violates open closed, and thinking about it properly. Good post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Woods</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1225</link>
		<dc:creator>Simon Woods</dc:creator>
		<pubDate>Sat, 09 Oct 2010 08:19:33 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1225</guid>
		<description>Don&#039;t know if you&#039;ve had a look at the series starting with

http://lorgonblog.wordpress.com/2007/12/02/c-3-0-lambda-and-the-first-post-of-a-series-about-monadic-parser-combinators/

I think you&#039;ll find quite a few control structures implemented in this sort of way (except as extension methods). I  think Bart de Smet did  something similar with If and For. 

http://bartdesmet.net/blogs/bart/archive/2009/07/11/bart-s-control-library-not-what-you-think-it-is-part-0.aspx
</description>
		<content:encoded><![CDATA[<p>Don&#8217;t know if you&#8217;ve had a look at the series starting with</p>
<p><a href="http://lorgonblog.wordpress.com/2007/12/02/c-3-0-lambda-and-the-first-post-of-a-series-about-monadic-parser-combinators/" rel="nofollow">http://lorgonblog.wordpress.com/2007/12/02/c-3-0-lambda-and-the-first-post-of-a-series-about-monadic-parser-combinators/</a></p>
<p>I think you&#8217;ll find quite a few control structures implemented in this sort of way (except as extension methods). I  think Bart de Smet did  something similar with If and For. </p>
<p><a href="http://bartdesmet.net/blogs/bart/archive/2009/07/11/bart-s-control-library-not-what-you-think-it-is-part-0.aspx" rel="nofollow">http://bartdesmet.net/blogs/bart/archive/2009/07/11/bart-s-control-library-not-what-you-think-it-is-part-0.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Chandler</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1224</link>
		<dc:creator>Michael Chandler</dc:creator>
		<pubDate>Sat, 09 Oct 2010 06:32:12 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1224</guid>
		<description>I don&#039;t like it. I don&#039;t see it offering anything extra (such as extensibility), just extra complexity over the top of a well known concept.

I&#039;d prefer to see language/framework support for the Chain-of-responsibility pattern (http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern).

For example, maybe something in the BCL like:

interface IHandler&lt;TContext&gt;
{
   bool IsHandlerFor(TContext context);
   void Apply(TContext context);
}

[this is basically identical to ICommand, perhaps we could just reuse that...?]

You can then either explicitly create a class for each handler, or create a &#039;reusable&#039; class, something like:

class Handler&lt;TContext&gt; : IHandler&lt;TContext&gt; {
   private Func&lt;TContext, bool&gt; _isHandlerFor;
   private Action&lt;TContext&gt; _applier;
   public Handler(Func&lt;TContext, bool&gt; isHandlerFor, Action&lt;TContext&gt; applier) {
      _isHandlerFor = isHandleFor; _applier = applier;
   }
   bool IsHandlerFor(TContext context) { return _isHandlerFor(context); }
   void Apply(TContext context) { _applier(context); }
}

You could then do something like:
var handlers = new[] { 
   new Handler&lt;ScannedAssetState&gt;(context =&gt; context.Status == ScannedAssetState.ConfirmAssetRemove, context =&gt; ConfirmAssetRemove(...)),
   new Handler&lt;ScannedAssetState&gt;(context =&gt; context.Status == ScannedAssetState.Error, context =&gt; View.ShowError(...)),
}

ApplyFirstMatchingHandler(newContainer, handlers);

This also gives you the flexibility of allowing the IoC container to automatically setup the handlers for you, if you so wish. If you did do that, then as new handlers are created (say for a new state), none of the existing code needs to change. It also means that the logic for handling that particular state is located only within the relevant handler, leading to higher cohesion.

Thoughts?</description>
		<content:encoded><![CDATA[<p>I don&#8217;t like it. I don&#8217;t see it offering anything extra (such as extensibility), just extra complexity over the top of a well known concept.</p>
<p>I&#8217;d prefer to see language/framework support for the Chain-of-responsibility pattern (<a href="http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern</a>).</p>
<p>For example, maybe something in the BCL like:</p>
<p>interface IHandler<tcontext><br />
{<br />
   bool IsHandlerFor(TContext context);<br />
   void Apply(TContext context);<br />
}</p>
<p>[this is basically identical to ICommand, perhaps we could just reuse that...?]</p>
<p>You can then either explicitly create a class for each handler, or create a &#8216;reusable&#8217; class, something like:</p>
<p>class Handler</tcontext><tcontext> : IHandler</tcontext><tcontext> {<br />
   private Func</tcontext><tcontext , bool> _isHandlerFor;<br />
   private Action</tcontext><tcontext> _applier;<br />
   public Handler(Func</tcontext><tcontext , bool> isHandlerFor, Action</tcontext><tcontext> applier) {<br />
      _isHandlerFor = isHandleFor; _applier = applier;<br />
   }<br />
   bool IsHandlerFor(TContext context) { return _isHandlerFor(context); }<br />
   void Apply(TContext context) { _applier(context); }<br />
}</p>
<p>You could then do something like:<br />
var handlers = new[] {<br />
   new Handler<scannedassetstate>(context => context.Status == ScannedAssetState.ConfirmAssetRemove, context => ConfirmAssetRemove(&#8230;)),<br />
   new Handler</scannedassetstate><scannedassetstate>(context => context.Status == ScannedAssetState.Error, context => View.ShowError(&#8230;)),<br />
}</p>
<p>ApplyFirstMatchingHandler(newContainer, handlers);</p>
<p>This also gives you the flexibility of allowing the IoC container to automatically setup the handlers for you, if you so wish. If you did do that, then as new handlers are created (say for a new state), none of the existing code needs to change. It also means that the logic for handling that particular state is located only within the relevant handler, leading to higher cohesion.</p>
<p>Thoughts?</scannedassetstate></tcontext></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mikhail Opletayev</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1223</link>
		<dc:creator>Mikhail Opletayev</dc:creator>
		<pubDate>Fri, 08 Oct 2010 20:57:58 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1223</guid>
		<description>I don&#039;t know about less ugly but surely a LOT slower. You allocate several objects and use a bunch of indirect jumps over a compiler-optimized jump table. I would argue it&#039;s a pretty poor practice.

Also, there is no need to use { } brackets in cases:

switch( myObj.status ) {
    case StatusEnum.Case1:
        doSomething();
        break;
    case StatusEnum.Case2:
        doSomethingElse();
        break;
    default:
        doElse();
        break;
}</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know about less ugly but surely a LOT slower. You allocate several objects and use a bunch of indirect jumps over a compiler-optimized jump table. I would argue it&#8217;s a pretty poor practice.</p>
<p>Also, there is no need to use { } brackets in cases:</p>
<p>switch( myObj.status ) {<br />
    case StatusEnum.Case1:<br />
        doSomething();<br />
        break;<br />
    case StatusEnum.Case2:<br />
        doSomethingElse();<br />
        break;<br />
    default:<br />
        doElse();<br />
        break;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DaRage</title>
		<link>http://lostechies.com/derickbailey/2010/10/07/a-less-ugly-switch-statement-for-c/#comment-1222</link>
		<dc:creator>DaRage</dc:creator>
		<pubDate>Fri, 08 Oct 2010 15:18:07 +0000</pubDate>
		<guid isPermaLink="false">/blogs/derickbailey/archive/2010/10/07/a-less-ugly-switch-statement-for-c.aspx#comment-1222</guid>
		<description>@Diego

I don&#039;t think this is uglier because first it uses generics while the switch statement is limited to numbers, strings. Second there is no curly bracket soup (and that&#039;s what i like most) so it reads better. Last switch semantics are very often useful so and easy to read and understand so it&#039;s good to extend it to other types.

I would call this the Object-Oriented switch while the old one is the structured switch which i consider a very stinky code smell.
</description>
		<content:encoded><![CDATA[<p>@Diego</p>
<p>I don&#8217;t think this is uglier because first it uses generics while the switch statement is limited to numbers, strings. Second there is no curly bracket soup (and that&#8217;s what i like most) so it reads better. Last switch semantics are very often useful so and easy to read and understand so it&#8217;s good to extend it to other types.</p>
<p>I would call this the Object-Oriented switch while the old one is the structured switch which i consider a very stinky code smell.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
