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)