anti-if campaign
Posted
by Andrew Siemer
on Stack Overflow
See other posts from Stack Overflow
or by Andrew Siemer
Published on 2009-07-22T19:23:09Z
Indexed on
2010/03/17
11:41 UTC
Read the original article
Hit count: 242
design-patterns
|best-practices
I recently ran against a very interesting site that expresses a very interesting idea - the anti-if campaign. You can see this here at www.antiifcampaign.com. I have to agree that complex nested IF statements are an absolute pain in the rear. I am currently on a project that up until very recently had some crazy nested IFs that scrolled to the right for quite a ways. We cured our issues in two ways - we used Windows Workflow Foundation to address routing (or workflow) concerns. And we are in the process of implementing all of our business rules utilizing ILOG Rules for .NET (recently purchased by IBM!!). This for the most part has cured our nested IF pains...but I find myself wondering how many people cure their pains in the manner that the good folks at the AntiIfCampaign suggest (see an example here) by creating numerous amounts of abstract classes to represent a given scenario that was originally covered by the nested IF. I wonder if another way to address the removal of this complexity might also be in using an IoC container such as StructureMap to move in and out of different bits of functionality. Either way...
Question: Given a scenario where I have a nested complex IF or SWITCH statement that is used to evaluate a given type of thing (say evaluating an Enum) to determine how I want to handle the processing of that thing by enum type - what are some ways to do the same form of processing without using the IF or SWITCH hierarchical structure?
public enum WidgetTypes
{
Type1,
Type2,
Type3,
Type4
}
...
WidgetTypes _myType = WidgetTypes.Type1;
...
switch(_myType)
{
case WidgetTypes.Type1:
//do something
break;
case WidgetTypes.Type2:
//do something
break;
//etc...
}
© Stack Overflow or respective owner