Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a switch statement ok for 30 or so conditions?

Tags:

c#

parsing

I am in the final stages of creating an MP4 tag parser in .Net. For those who have experience with tagging music you would be aware that there are an average of 30 or so tags. If tested out different types of loops and it seems that a switch statement with Const values seems to be the way to go with regard to catching the tags in binary.

The switch allows me to search the binary without the need to know which order the tags are stored or if there are some not present but I wonder if anyone would be against using a switch statement for so many conditionals.

Any insight is much appreciated.

EDIT: One think I should add now that where discussing this is that the function is recursive, should I pull out this conditional and pass the data of to a method I can kill?

like image 971
deanvmc Avatar asked Apr 14 '10 19:04

deanvmc


1 Answers

It'll probably work fine with the switch, but I think your function will become very long.

One way you could solve this is to create a handler class for each tag type and then register each handler with the corresponding tag in a dictionary. When you need to parse a tag, you can look up in the dictionary which handler should be used.

like image 50
Mark Byers Avatar answered Oct 17 '22 08:10

Mark Byers