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

M

Mark S.

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;
}
}
 
S

Samuel R. Neff

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top