Should I leave an unattainable gap when I make an exception?

Is it really stupid for me to leave unreachable break statements in a case that just throws an exception? The protective part of me wants to leave it there, in case of a change in logic. The other part of me does not want other developers to see compiler warnings in 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 several answers saying that removing the throw later will result in a compiler error. However, simply deleting (or commenting) the cast without interruption after it would add up cases that may be unintended behavior. I'm not saying that this is a likely scenario, but ... well, is this defensive programming to deal with likely scenarios?

+6
source share
9 answers

I will delete them. A few reasons:

  • You do not need them at the moment.
  • Watching a lot of warnings always makes me nervous, because you can lose the real warnings in the noise coming from this warning (provided that you warn of an error).
+6
source

I would not "hide" it in the switch . I would throw ArgumentExceptions as soon as possible. This avoids side effects as well as being more transparent.

Someone might add code in front of the switch at some point that uses someInt , although it is 3.

For instance:

 public void SomeMethod(int someInt) { if (someInt == 3) throw new ArgumentException("someInt must not be 3", "someInt"); switch (someInt) { // ... } } 
+7
source

By neglecting some compiler warnings, you reinforce bad behavior by ignoring any compiler warning. In my opinion, this is a greater risk than any advantage that you get from leaving break statements.

EDIT: I deleted my starting point about the compiler, forcing you to re-enable break statements if the throw statements were deleted. As payo noted, in some cases there will be no compiler. He simply connects the case to the one below him.

+2
source

Personally, I would never leave unreachable code in production code. This is good for testing, but do not leave it as such.

Would you never do that?

 public void MyMethodThatThrows() { throw new Exception(); return; // unneeded } 

so why keep break ?

+2
source

I would delete it.

It gets rid of warnings, and even if the logic needs to change and the exception is removed, you would get a compiler error stating that you need to add a break.

+1
source

The protective part of me wants to leave her there, in case there is a logical change.

What logic can be changed here? What happens when an exception is thrown is well documented. And I think you can be reasonably sure that a later version of C # will not lead to breaking the changes "all the programs written so far are no longer working."

Even if there was no warning about the compiler, redundant code is not very good, if there is no possibility that the code can come into play, preventing errors during routine maintenance, which in this case cannot reasonably be said to exist.

0
source

The protective part of me wants to leave it there, in case of a change in logic.

Well, if you remove throw and forget to add break , the compiler will let you know.

I see no reason for a break. This is redundant and you will still raise a warning, so I will remove it. There is no good reason to leave useless code cluttering your logic.

0
source

I would like my project sources to be so cool and no problem that I would have to think about such minor cases of defensive programming :)

I mean, there are so many mistakes in software development - COM and Interop, Security / Permissions and Auth, Certificates, which ... gosh, I can’t say how much code I would need to write to protect yourself from shooting in the leg.

Just remove this gap because it is superfluous, it is confusing for fellows who know that "case" should end with break or return and should become obvious for fellows who do not know it.

0
source

The C # MSDN link refers to: "A transition operation, such as a break, is required after each case block, including the last block, whether it is a case argument or a default expression."

So, doesn’t the “throw” constitute a “transition hint”? I think so. Thus, a “gap” is not required, and the problem of “inaccessibility” disappears. :)

0
source

Source: https://habr.com/ru/post/911988/


All Articles