views:

85

answers:

5

Hi,
I am writing a parser, which call some functions dependent on some value.

I can implement this logic with simple switch like this

switch(some_val)   
{   
    case 0:   
        func0();   
        break;   
    case 1:   
        func1();   
        break;   
}     

or with delegates and dictionary like this

delegate void some_delegate();   
Dictionary<int, some_delegate> some_dictionary = new Dictionary<int, some_delegate>();   
some_dictionary[0] = func0;   
some_dictionary[1] = func1;   

some_dictionary[some_value].Invoke();    

Are this two methods equal and which is preferred?
Thanks.

+2  A: 

Both do the same (well you should check if the key appears in the dictonary).

It is just a matter of readability. What looks the best for your and more important, what do people reading your code would prefer.

(I think the dictionary)

PoweRoy
Thanks for yuor answer.I now that both do the same, in other case the question would not have any mean :)
Samvel Siradeghyan
+3  A: 

In terms of access, they're identical: both just check if that specific value has a corresponding result. However, a Dictionary will throw an out-of-bounds exception if you try to access a non-existent key.

The choice should primarily be on re-usability. If you only need to make this branching logic at one point, then using a switch-case is probably makes more sense than storing a variable. If you need to access it repeatedly in separate points, then use the Dictionary to save yourself from just re-pasting the switch-statement repeatedly.

ccomet
+3  A: 

I strongly prefer the dictionary choice, because with an initializer, it can be a lot more compact and readable:

var actions = new Dictionary<int, Action>
{
  {1, () => Console.WriteLine("One!")},
  {2, () => Console.WriteLine("Two!")}
}

Also, you have some more flexibility; you can add conditions and actions programatically, which is often handy, depending on what you're doing.

Isaac Cambron
What about speed?
Samvel Siradeghyan
It'll be slower than the switch. Be careful there, though; premature optimization is the root of all evil. Code for good design, readability, and all that jazz, and only work on the speed part if it's too slow for your purposes. The speed difference might be completely negligible, but the readability differences are real and obvious.
Isaac Cambron
Thanks, time is not too critical for me in this case, so I will use Dectionary with delegate
Samvel Siradeghyan
A: 

switch statement seems to be more readable for a parser - you don't need dynamic capabilities provided by dictionary/delegate approach.

Al Bundy
A: 

If the typical programmer on your team is anything like the ones I often deal with, you should go for the simplest option i.e. the switch. The delegates seem to me like a 'clever' solution that is not needed.

Johann Strydom
I agree with ccomet. If the switch only occurs once in your code, then its the best option, but if it gets duplicated on 2 or more methods, then the dictionary is the cleaner approach.Incompetent or disinterested programmers are a whole other problem. I don't believe it is beneficial to write "lowest common denominator" code. If the team is not interested in learning, then get a new job. ;)
Jacques Bosch