views:

84

answers:

4

It happens quite a lot that I've a two member enum :

        enum  E{
            A,
            B
        }

Let's say I want to assign a different value to i according to my enum value.

should I write this :

        int i= (e== E.A)?0:1;

or this :

        int i;
        if (e==E.A)
            i=0;
        else if (e==E.B)
            i=1;
        else throw new NotImplementedException("Unknown enum member for E : " + e);

or maybe this :

        int i;
        switch (e)
        {
            case E.A:
                i = 0;
                break;
            case E.B:
                i=1;
                break;
            default:
                throw new NotImplementedException("Unknown enum member for E : " + e);
        }

I usually go for the first option because it's way faster to write, but I always feel a bit wrong about it. What do you usually do?

I posted the code in c#, but this question is not language-related.

I don't really know how it should be tagged, don't hesitate to retag it if needed.

Edit : maybe my question is not clear enough : what I'm really wondering is : should I assume that my enum will never change and go for the fast way, or should I consider that it may change (although I've not changed it yet), and add some error handling code so that I don't spend weeks tracking where the bug is if it ever changes)

+1  A: 

if values are ever added to the enum it will be easier to add statements to a switch statement than any of the others.

Jason
well, right, but that's 12 lines of code instead of one ....
Brann
A: 

The question really is language-related, because different languages handle enums differently.

The first version is very different from the other two because of the error handling. If you don't really care what happens if it's neither A nor B, just use (in this case):

int i = (int) e;

If the int value you want out doesn't match directly, I'd either go for the switch statement or possibly even use a dictionary (which would be slower, but more compact code with C# 3.0 collection initializers, and would provide argument checking for free, if you're happy with the exception it throws).

Jon Skeet
@Jon : I know what changes is the error handling. I'm wondering whether it's ok to drop the error handling for concision sake.
Brann
That entirely depends on the situation. If the method is being called within your own code, with a value which you know is already valid, it's reasonable to not validate the arguments. If it's called by other code, you should probably validate. I'd possibly do that before anything else though.
Jon Skeet
+3  A: 

From a maintenance point of view I would look at the first option as a potential bug, and turn it into a case statement to make the behaviour more explicit. I guess it depends on the life expectancy of the code - the longer it's going to stick around, the more explicit you should make it.

marijne
A: 

The last one is a best practice. It is more readable and more maintainable. and please don't use the first one!

M. Jahedbozorgan