Dofactory.com
Dofactory.com
 Back to list
Views:   10.3K
Replies:  1
Archived

Do you feel guilty when using a C# switch statement?

Now that I am familiar with Design Patterns I am beginning to feel guilty whenever I use the C# switch statement.  It is almost like switches are a 'code smell' and that a better solution is possible -- typically that would be a State or Factory Design Pattern.

Am I the only one having this guilt feeling or is it just me?
Also, am I justified to see the C# switch as a code-smell or is there still a place for this construct?
John Telstra, Mar 29, 2011
Reply 1
I believe it's good thing that if you use/see a switch statement, consider if there are better alternatives using Strategy, State, or Factory. As I believe that using those patterns will make your code more robust and easier to maintain and change.

However, not all situations require patterns. As with switch statements, sometimes it would be easier to use the switch statements instead of implementing a full-blown pattern. I guess it depends on the situation and more specifically if you anticipate that the switch statement would be changed in the future (added conditionals, etc). Bare in mind that most often, when you use patterns, your code will become more complex and you should consider if it is worth it.

It is always a trade off :)
Good Luck
Robert Blixt, Mar 29, 2011
any other brief description about clone() and copy() string methods.
Dec 07, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.