I have the following method and interface:
public object ProcessRules(List<IRule> rules)
{
foreach(IRule rule in rules)
{
if(EvaluateExpression(rule.Exp) == true) return rule.Result;
}
//Some error handling here for not hitting any rules
}
public interface IRule
{
Expression Exp;
Object Result;
int Precedence;
}
Because rules have a precedence, they should actually never be processed out of order. This leads me with (I think) three solutions:
- Sort rules before passing them into the evaluator.
- Change the parameter type to something that enforces a sort order.
- Sort within the evaluator.
I like option 3 because it always ensures that it is sorted and I like option 1 because it seems more cohesive. And option 2 seems like a good compromise.
Is a scenario like this context specific/subjective, or is there really a best practice to be applied here?