Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Decisions tree/table vs. IF/Switch vs. ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark S. - 28 Sep 2007 08:43 GMT
Hello, I've put my question at the bottom of the code below...

using System;
using System.Collections.Generic;
using System.Text;

public class Criterion
{
   public string operatorLogical;
   public string argument;
   public string operatorEquality;
   public bool isCaseSensitive;
   public List<String> values = new List<string>();
}

public class Moi
{
   public string companyName;
   public string firstName;
   // in the real app there will be +/- 30 attributes
}

class Decisions
{
   static void Main(string[] args)
   {
       // all the criterion will be held by in a criteria list
       List<Criterion> criteria = new List<Criterion>();

       // make some Criterion
       Criterion foo = new Criterion();
       foo.operatorEquality = "AND";
       foo.argument = "companyName";
       foo.operatorEquality = "eq";
       foo.isCaseSensitive = false;
       foo.values.Add("Acme");
       foo.values.Add("Inc");
       criteria.Add(foo);

       Criterion foo2 = new Criterion();
       foo2.operatorEquality = "OR";
       foo2.argument = "firstName";
       foo2.operatorEquality = "contains";
       foo2.isCaseSensitive = true;
       foo2.values.Add("Jon");
       foo2.values.Add("Rob");
       criteria.Add(foo2);
       // end making Criterion

       // make compare to this
       Moi moi = new Moi();
       moi.companyName = "Acme";
       moi.firstName = "Robert";
       // end make compare to this

       // is there a match?
       if (decision(ref moi, ref criteria))
       {
           Console.WriteLine("Matched!");
       }
       else
       {
           Console.WriteLine("NO match.");
       }

       // console
       Console.WriteLine("");
       Console.WriteLine("Press any key to exit.");
       Console.ReadLine();
   }

   static bool decision(ref Moi moi, ref List<Criterion> criteria)
   {
       /*
        * Here's where I need advice. Is it best to use a series of
        * if/try statements or a decision table/tree or an entirely
different approach
        *
        * if I where to write this in English I would write something
like...
        * IF moi.companyName Equals "Acme OR Inc" (ignore case)
        * OR moi.firstName Contains "Jon OR Rob" (case sensitive)
        *
        * TIA, especially to any suggestions that are Occam's Razor
friendly.
       */
       return true;
   }
}
Samuel R. Neff - 28 Sep 2007 12:50 GMT
I would suggest the logic either be in Criterion subclasses which
define the different comparisons or a separate object Criterion uses
such CriterionEvaluator.  That way each comparison is isolated and the
main decision loop simply loops through criterion and calls a
consistent method.

I would certainly do everything possible to avoid a big if/switch
statement.

HTH,

Sam

------------------------------------------------------------
We're hiring!  B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC.  Work with a variety of technologies
in a relaxed team environment.  See ads on Dice.com.

>Hello, I've put my question at the bottom of the code below...
>
>using System;
>using System.Collections.Generic;
>using System.Text;

...
>class Decisions
>{
[quoted text clipped - 12 lines]
>        foo.values.Add("Inc");
>        criteria.Add(foo);

...
>    }
>
[quoted text clipped - 16 lines]
>    }
>}
sloan - 28 Sep 2007 16:24 GMT
As an add-on thought.  I would look at the "Factory Design Patten"

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!126.entry

> Hello, I've put my question at the bottom of the code below...
>
[quoted text clipped - 85 lines]
>    }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.