9


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 didn'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.

Daniel Higueras
  • 2,404
  • 22
  • 34
  • 11
    Either your teacher was joking, or you need a new teacher. – Andy Thomas Mar 06 '11 at 15:57
  • 4
    Just because something doesn't exist in Pascal doesn't mean it's bad. I mean, Pascal doesn't have garbage collection, too. That doesn't make it bad. – Goran Jovic Mar 06 '11 at 16:03
  • @GoranJovic While broadly, as you put it, it is true, the case (pun!) here is slightly different: a design decision (in Pascal) to leave some detail out from a particular feature, and an otherwise easily implemented detail at that. [Anyway, Pascal does have default/else.] – Pablo H Jan 22 '19 at 12:23

7 Answers7

18

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;
Community
  • 1
  • 1
Cephalopod
  • 14,632
  • 7
  • 51
  • 70
  • 1
    +1 for the code sample. Even if there weren't a syntax for it, this could have been written using nested if-then-else statements, as well. So, that is definitely possible to write in Pascal. – Goran Jovic Mar 06 '11 at 16:02
  • @Arian - "because the default case always happens when you expect it the least" - I have had this come up as a bug when a slightly damaged build file put the previous version of the code into the built (otherwise correct) code. The previous version did not have a default case; the correct version had extended the case statement. Finding this was a major task - it built OK, but finding the error in the rebuilt system took days and a huge amount of man hours. – Chris Walton Mar 06 '11 at 16:10
  • Thanks. I hope he was joking or else I'm going to have a problem believing anything he tells me... I'll have to check everything by myself. – Daniel Higueras Mar 06 '11 at 16:17
  • @Chris: I can imagine this was hard to find... and I'm still wondering what we can learn from that. – Cephalopod Mar 06 '11 at 20:58
  • 3
    Actually, in original Pascal there was no such thing. It was added later, during the standarization phase (1983 or thereabouts). Can be seen in that Borland uses "ELSE" and Apple and ISO pascal use "OTHERWISE" – Marco van de Voort Mar 07 '11 at 00:00
  • @Arian - what we learnt from the problem was a change to our standards, and testing - every usage of a case statement HAD to have a default (else) case. This was enforced by static code testing on the check in procedures. – Chris Walton Mar 07 '11 at 04:00
5

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.

Daniel
  • 27,718
  • 20
  • 89
  • 133
  • One should generally not throw errors in java, as errors indicate that there is some big problem that cannot (and should not) be recovered from. Many people use IllegalArgumentException to signal unexpected default cases. – Cephalopod Mar 06 '11 at 20:53
  • I consider IllegalArgumentException to signal invalid function parameters. And in my case, an AssertionError is IMHO a good idea, because it signals that the assertion of having all enum covered has been wrong. – Daniel Mar 06 '11 at 21:12
  • I know exactly what you mean, I am also not happy with `IllegalArgumentException`s. But many (most?) people use `catch (Exception e)` if they want their program to continue even if something unexpected happens, and this way they will not catch AssertionErrors. Im just saying this is a problem, not that I have a good solution... – Cephalopod Mar 10 '11 at 14:50
  • 1
    ... people discarding exceptions are a reason FOR AssertionErrors IMHO ;) – Daniel Mar 10 '11 at 15:03
2

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.

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
bnieland
  • 6,047
  • 4
  • 40
  • 66
1

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.

Kosi2801
  • 22,222
  • 13
  • 38
  • 45
0

I'm afraid your teacher is wrong. It's a good practice to have a default case.

Bala R
  • 107,317
  • 23
  • 199
  • 210
0

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...

Pablo H
  • 609
  • 4
  • 22
-2

It really depends. If there's no default, you do not need to have it.

Quincy
  • 4,393
  • 3
  • 26
  • 40
  • IMO case statement is no more than a set of if-then-else statement. Do we always need to set a else case? I believe not. – Quincy Mar 06 '11 at 16:23
  • 1
    is correct in that a stand alone "if-then" sans the else is a useful and common construct, but it is mainly used when there is a exception from the main line of the logic. For example, if (isFirstTimeLoading) populateDropdowns(); Switch cases are rarely used for such, but they could be. I think the point is, "Don't assume that you know all the cases. If you can identify an exception by using a default case, go for it.". – bnieland Mar 06 '11 at 16:38
  • You are right that you don't *need* to use it. But, you *may* use it. – Goran Jovic Mar 06 '11 at 16:48
  • There are a few cases where I would agree to that. For instance, if you need special handling for some values in a large range and the default case applies to those elements as well and is thus written after the switch. However, according to my experience those cases are rare. – Cephalopod Mar 06 '11 at 20:48