Always having a default:
case in switch
statements is defensive programming.
It should either take appropriate action or contain a suitable comment as to why no action is taken.
The following code snippet:
int foo = 42; switch (foo) // Non-Compliant { case 0: Console.WriteLine("foo = 0"); break; case 42: Console.WriteLine("foo = 42"); break; }
should be refactored into:
int foo = 42; switch (foo) // Compliant { case 0: Console.WriteLine("foo = 0"); break; case 42: Console.WriteLine("foo = 42"); break; default: throw new InvalidOperationException("Unexpected value foo = " + foo); }