Is this a violation of the single responsiblity principle?
Posted
by L. Moser
on Stack Overflow
See other posts from Stack Overflow
or by L. Moser
Published on 2009-09-12T23:24:41Z
Indexed on
2010/05/23
2:20 UTC
Read the original article
Hit count: 412
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?
© Stack Overflow or respective owner