Hello Tamas,
first of all: thanks for considering chaning this rule :-)
However, I'm not sure that ignoring enums altogether is the right way to got. Let's have a look at some examples:
1. sometimes, one enum value stand out of the others, for instance:
if (person.PetPreference == PetType.None)
{
// person doesn't like pets at all
}
If the rule simply ignores switch statements on enums, you could just as well write
switch (person.PetPreference)
{
case PetType.None:
// person doesn't like pets at all
break;
}
But I would want the rule to suggest the if-statement here.
2. Let's say someone adds a default statement to this switch:
switch (person.PetPreference)
{
case PetType.None:
// person doesn't like pets at all
break;
default:
// person likes pets
break;
}
One could argue that an if/else would still better convey the intention of the code. Check for one specific value and act accordingly, treat all other values the same.
if (person.PetPreference == PetType.None)
{
// person doesn't like pets at all
}
else
{
// person likes pets
}
So I think that checking for 2 actual "case" statements (not counting "default") would be the better choice.
Same can be argued for integer based values
3.
switch (person.NumberOfLotteryWins)
{
case 0:
// normal person
break;
default:
// lucky person
break;
}
Would probably make more sense to be written as
if (person.NumberOfLotteryWins == 0)
{
// normal person
}
else
{
// lucky person
}
4. but with 2 actual case statements a switch makes more sense
switch (person.NumberOfLotteryWins)
{
case 0:
// normal person
break;
case 1:
// lucky person
break;
default:
// unnaturally lucky person
break;
}
Writing this in an "if/else if" construct would look weird. Basically I guess if/else is fine, but as soon as you would have to use "if/else if" a switch is better.
That's why I suggested to check for "2 actual case statements" (not counting "default").
Regards,
Markus