Is it silly of me to leave unreachable break statements in a case that just throws an Exception anyway? The defensive part of me wants to leave it there in the event that the logic changes. Another part of me doesn't want other developers seeing compiler warnings on my code ("Unreachable code detected").
switch (someInt)
{
case 1:
// Do something
break;
case 2:
// Do something else
break;
case 3:
// Oh, we don't use threes here!
throw new Exception("Business rules say don't use 3 anymore");
break; // Unreachable...until the fickle business rules change...
default:
throw new Exception("Some default exception");
break; // Unreachable...until...well, you get the idea.
}
What to do?
UPDATE
I see a few responses saying that removing the throw at a later date would cause a compiler error. However, simply removing (or commenting) the throw without a break following it would stack the cases, which may be unintended behavior. I'm not saying it's a likely scenario, but...well, is defensive programming about combating only likely scenarios?
1(a) is compiled, line 12 raises an unreachable statement error because the break statement exits the for loop and the successive statement cannot be executed. To address this issue, the control flow needs to be restructured and the unreachable statement removed, or moved outside the enclosing block, as shown in Fig.
An unreachable catch clause may indicate a logical mistake in the exception handling code or may simply be unnecessary. Although certain unreachable catch clauses cause a compiler error, there are also unreachable catch clauses that do not cause a compiler error.
The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.
If an exception occurs inside the switch, then it's fine. The one reasonably standard place you might throw inside a switch statement is in the default case. If reaching that case indicates invalid input, then it's reasonable to throw an exception.
I'd remove them. Several reasons:
I wouldn't "hide" it in the switch
. I would throw ArgumentExceptions
as soon as possible. That avoids side-effects and is also more transparent.
Somebody might add code before the switch at some point which uses someInt
although it is 3.
For example:
public void SomeMethod(int someInt)
{
if (someInt == 3)
throw new ArgumentException("someInt must not be 3", "someInt");
switch (someInt)
{
// ...
}
}
Personally, I would never leave unreachable code in production code. It's fine for testing, but don't leave it as such.
You would never do this would you?
public void MyMethodThatThrows()
{
throw new Exception();
return; // unneeded
}
so why keep the break
?
By ignoring some compiler warnings you are reinforcing the bad behavior of ignoring any compiler warning. In my opinion, that's a greater risk than any advantage you gain from leaving the break
statements in.
EDIT: I removed my original point about the compiler forcing you to put the break
statements back in if the throw
statements were to be removed. As payo pointed out, in some cases, the compiler wouldn't. It would just combine the case with the one below it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With