Is using "default" case in a switch statement a good habit? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question when I'm using a switch(in Java in this case) I normally use the default case if needed. One of my teachers told me that when he used to program in Pascal, that case d开发者_Python百科idn't exist. He said that if it didn't exist in Pascal it shouldn't be something good to use.My questions are:
- Is it wrong to use the default case?
- How does it work internally?
Thanks in advance.
I would consider it a bad habit not to use it.
- If you think the default case will never happen, throw an exception to be sure
- If you switch over an enum, it may happen that someone added another value
- If you switch over an integer, it is always possible that an unexpected value is found
- because the default case always happens when you expect it the least
- As far as I know there is something similar in Pascal
Edit:
This is Pascal, just to prove your teacher wrong
case place of
1: writeln('Champion');
2: writeln('First runner-up');
3: writeln('Second runner-up');
else writeln('Work hard next time!');
end;
Using a default case is always a good habit. I even use it when switching on an enum. If the enum has 3 values, I have 3 case statements, and one case statement that throws an AssertionError.
This is good because if the enum is extended, it is ensured that errors related to missing the new values in switch statements will be detected soon.
There is nothing wrong with the default case. In fact, I believe it should almost always be used to throw an error to indicate a faulty value in the switch.
The only thing I can think of that might of lead you professor to make such a statement, is his or her belief that you data should have been validated before reaching a case statement. i.e. If you are programming well, your cases will reflect every contingency.
Well, if that were the case, then we would not need exceptions, period. The whole idea of exceptions is to handle unanticipated conditions. If it was reasonably anticipatable, you would handle it.
So, throw an exceptions in your switch default statements by all means.
As to how they work internally? I am sure there are many possible implementations, but logically, the default case is just the final else clause in a long chain of if..then..if..then..else's.
Yes it's good practice to use the default case, because if at later time you change the condidtions or enumerations you use in your switch-statement, the application does behave "less incorrect" than it would if the new values weren't covered at all. Sorry to say, but IMO your teacher has a bit to catch up with more recent programming methodologies.
Internally it works that way, that the default branch is used in case, the condition does not match any of the other listed conditions.
I'm afraid your teacher is wrong. It's a good practice to have a default case.
Another take on the issue:
Other answers mostly state, basically, "when you switch/case on X, and X's type allows X to be 1, 2, 3, etc. (integer), or Red, Blue, Fucsia, etc. (enum), be sure to handle the etc!!". Obvious, isn't it?
On the other hand, when you believe that X, as a variable, because of program flow, will never take on the value "etc"... think otherwise, because it will, eventually. That's the general advice.
Deviating from Java/Pascal to put this in perspective: What if X's type would not allow for "etc"? (say, a "very strict" enum or a range (for integers)). That is, the compiler ensures the possible values are never "etc". And it flags unhandled cases as errors. Would be nice. :-) Functional languages have pattern-matching and algebraic types, which sort of goes this way, but I have no relevant experience there. :(
Back to Java (and similar languages), because you can implement types (classes), you can implement strict checking, e.g. by doing the "case" as a method call taking lambdas/functions/function objects...
It really depends. If there's no default, you do not need to have it.
精彩评论