0

I have an UGLY logic tree that I'm looking at ways replace with some code generated from a table. It branches based on several thing:

  • the length of a List<AbstractType>
  • the actual types in the list
  • a flags enum

My basic idea is to build some kind of decision tree from by input table. For the list length and flags that's easy (a switch), but what about the types bit?

This question suggests that polymorphism is a good idea but that would mix concerns and strew code to the four winds in my case (and doesn't lend its self to generation anyway). The other suggested solution (IDictionary<Type, DelegateType>) might work but seem a little ugly.

Does anyone have any suggestions.

Community
  • 1
  • 1
BCS
  • 75,627
  • 68
  • 187
  • 294
  • If you could post how it looks now, it would help. – Alexey Romanov Aug 15 '09 at 18:49
  • Think of a logic tree half way between what you see on The Daily WTF and something you just try and avoid working with and that's about right. I'm looking for somewhat general solutions, so if you want more than that, you're answering a different question. – BCS Aug 15 '09 at 19:19

1 Answers1

1

One way would be to concatenate the type names (separated e.g. by colon), and then switch by string, e.g.

 switch(colon_separated_typenames(list)) {
   case "int:int": //foo
   case "double:String:double": //bar
 }
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • Nice and clean +1, but string building and (is suspect) switches are a little on the slow side. With a little more effort up front I think I can get away without any allocations. – BCS Aug 15 '09 at 19:21