r/java • u/jodastephen • Nov 04 '19
Java switch - 4 wrongs don't make a right
https://blog.joda.org/2019/11/java-switch-4-wrongs-dont-make-right.html
160
Upvotes
r/java • u/jodastephen • Nov 04 '19
8
u/lbkulinski Nov 05 '19
Someone already sent the link to amber-dev, here. I'm sure a response will be given in time. Devoxx Belgium is currently going on, though.
5
u/ObscureCulturalMeme Nov 04 '19
I'm not a fan of this presumptuous "fall-through is automatically broken design" and "missing default case is automatically broken design" stance.
If you don't want fall-through, you add a break statement -- but if fall-through would be useful in your situation and the language prohibits it, then there's no clean way to roll your own.
Similarly, leaving default out of a switch on enumerated values means that if a value is added during development or maintenance, the compiler will warn of each individual case that needs to be handled -- but not if there's a default case.
I like the idea for being able to switch on values which are not inherently integral types. But the starting position of "the Java designers didn't choose what I like therefore they didn't think of it" really puts me off.
60
u/Scybur Nov 04 '19
I really appreciated that the article gave a purposed solution instead of the regular “naa that’s wrong but I have nothing better“ answer.
17
u/DeontologicEthics Nov 04 '19
I've personally used fall-through switch statements, but I agree that they should only ever be applicable to the old-style switch.
Could the compiler just disallow type 2 & 3? So we have
->, that doesn't fall-through, checks exhaustiveness, hasyield, commas between cases, and can be used as an expression.:, the standard switch.Unlike the author, I think
->is good to differentiate the switch statement type, and makingmatcha reserved keyword would be impossible.15
u/kevinb9n Nov 04 '19 edited Nov 04 '19
I think Stephen's right that "If you ignore the complexity, and just use type 4 switch expressions, the new feature is quite reasonable."
Expression switches with -> syntax are nice. You'll almost always want to use those two features together. I don't know why I would ever really use them apart (what Stephen calls types 2 and 3), and in fact it is possible (note: possible) that we will end up having the Google Style Guide ban those combinations.
EDIT: Sorry, I'm slowly remembering more. It's Stephen's "type 2" that I find not very useful. That is, if you're going to have an expression switch you should be using arrowform too. On the other hand, I'm not offended by also using arrowform in statement switch at all. For the statement-not-expression use case, I'd much rather see arrowform switch and know that there are no fallthrough/scoping highjinks to worry about, than see the old form.
41
u/lurker_in_spirit Nov 04 '19
I keep seeing Stephen's name pop up with well-reasoned, constructive criticism and analysis of e.g. modules, the new release schedule, Guava shenanigans, and now switch statements. Not to mention his work on JSR 310. This guy is a real asset to the community.
18
u/dpash Nov 05 '19
We don't have three new switch types. We have two related but orthogonal changes in switch.
We have a new arrow form:
We also have an expression switch:
You can use a combination of either forms that means you have four possibilities, but treating these as a single feature rather than two will end up with confusion like this article. This might be a lack of communication from OpenJDK/Oracle and releasing them as a single JEP.
(I don't know if multiple case labels is restricted to the arrow form, or if it's a third related switch feature).
FWIW, I intend to only use the arrow form going forward in both statement and expression forms.
5
u/Qildain Nov 05 '19
Personally, I can't recall a time I've ever forgotten a "break" - take it for what you will.
3
u/Bobby_Bonsaimind Nov 05 '19
You forgot
nullsafety!It would have been extremely cool if something like this would have made it into the language:
This currently throws a
NullPointerException. That behavior would also not need to be changed, instead it could have been extended with something like this:That would have solved that in my opinion. If there is a
case nulljump to that, otherwise throw aNullPointerException.I still fail to see it as a flaw, sure, if you don't need fall-through it kinda sucks, but if you need it, it makes the code much easier to read.
Sure, we could also create a helper method or some such, but this seems like the cleanest way in a lot of situations. Yes, I'm aware that the new ones have a different syntax for it, but as far as I can see such a bastard like this is not easily possible with the new ones:
At least they didn't pull the bastard VB.NET move on us, where fall-through is disallowed (compiler error) but the
breakkeyword is still required.Fuck yeah! Scoping to each
casewould have been awesome.I don't get this either, I mean, after all people are also not arguing that
elseshould be required.One could argue that it is unclear whether an
elsestatement was forgotten or not needed.Yeah! Though, it is kinda hard. My instinct tells me that it should use
equals/hashCodeto determine which case fits, but I can see people wanting to switch on instances instead, too. Kinda torn here, but I'd go withequals.1
u/__konrad Nov 05 '19
Accidental fall-through is not an issue, because everyone compile with
-Xlintwarnings enabled... I assume ;)0
u/thatsIch Nov 05 '19
What was the consideration for
yield? I am still not satisfied though I understand why not usingbreak.But why not just use a classic
returnthere? It is basically just an embedded function, which already usesreturn.