Switch Case in Java, never use it?
Java switch use case - Stack Overflow
When to use switch case statements and when to use if, else if and else statements?
Do i have to use "switch" statements if i am comfortable with using "if" statements?
Can a switch statement be used inside another switch in Java?
Does the Java switch statement support String values?
Is fall-through ever useful in Java switch statements?
Videos
Should you never use a switch case in java? I'm in an intermediate programming course and the professor took points off my project because I used a switch case to get an int that corresponded to a different variable. He said switch cases are his pet peeve and to never use them.
Valid?
Assigning a value to a local variable and then returning that at the end is considered a good practice.
I have no idea when it was considered a good practice. To me, switch is usually * an indicator that a design error was made. I would rather put my effort into thinking how to avoid a switch than into wondering how to return a value from a switch.
A few examples
Long list of if statements in Java
How to avoid switch-case statements in Java
Converting many 'if else' statements to a cleaner approach
Methods having multiple exits are harder to debug and can be difficult to read.
The same goes for a method that has a lot of breaks - that's what you are going to do if you choose the "local-variable approach".
In my opinion, none of these
// 1
switch (input) {
case "A":
return "1";
case "B":
return "2";
default:
return "0";
}
// 2
String varibleToReturn = null;
switch (input) {
case "A":
varibleToReturn = "1";
break;
case "B":
varibleToReturn = "2";
break;
default:
varibleToReturn = "0";
}
return varibleToReturn;
// 3
return switch(digitInDecimal) {
case 0 -> '0';
case 1 -> '1';
case 2 -> '2';
default -> '?';
}
makes a significant difference, or a slight improvement. Yes, Java-12's switch would give more conciseness and expressiveness, but the fundamental idea remains the same.
Must I wait for Java 12 where switch can be used without temporary variables and breaks?
What does it mean? :) No, the deadline is tomorrow, you have to work with what you've got at hand now.
*I am not underestimating the usefulness of switch. It may come in handy, for instance, when you programme at low-level, or you write an optimization.
I am just saying that in the real world, with Springs, and Hibernates, in a world of patterns, switch is obsolescent.
But I found an old but high-ranked answer that says to avoid multiple return statements:
Assigning a value to a local variable and then returning that at the end is considered a good practice. Methods having multiple exits are harder to debug and can be difficult to read.
So I wonder, is that answer still relevant due to switch changes?
This is a common misconception, it originates form the phrase: "Single entry, single exit." (Page 24) All this originates from an other era, one that lead to structured programming languages and eventually to object oriented programming languages (like Java).
Don't worry about multiple return statements, there is nothing wrong with it.
I ask this question as I am learning Java and created a banking program. There are 5 options for a user to take, they select them by inputting "A","B", "C", "D" or "E" into the terminal - in this case, I feel switch case statements would be best to use.
There is a point where the user has to pick one of two options (create a account = "A" or log in with an existing account = "B"). As there are only two options, I feel using if statements would be fine here.
From your experience, when have you decided to opt for using switch case statements as opposed to if statements, and vice-versa? I would be really interested to hear examples of when you opted one over the other and your reasoning for doing so!