10
In some situations it is necessary to practice the switch case
for optimization and improvement of code. I have an application developed for Android using Java, which in case is used of both situations. Below I have an example using return
in which it is used with Tabs
:
switch (position){
case 0:
Tab tab1 = new Tab1();
return tab1;
case 1:
Tab tab2 = new Tab2();
return tab2;
case 2:
Tab tab3 = new Tab3();
return tab3;
}
In this case below we have to use the break
in which the desired month is defined:
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
In the SO gringo, there’s an explanation, but I don’t understand exactly what diverges one from the other. What is the real difference between return
and the break
in a switch case
?
The break only breaks the switch flow, that is, it interrupts the continuity from the block. Return breaks the flow of the method to which it is part.
– user28595
Recalling that the
switch
is one of the most horrific language constructions in Java. It has been inherited from C and C++, and has been a horrific thing since it was born in C. Avoid using it, only use it if you really.– Victor Stafusa
@Victorstafusa good, but maybe, I say maybe, is better than using a "ifelse" sausage. By chance there is some other alternative?!
– viana
@Acklay, yes there is. In the first case you can do (with Java 8)
Supplier<Tab1> a = Tab1::new; Supplier<Tab2> b = Tab2::new; Supplier<Tab3> c = Tab3::new; return Arrays.asList(a, b, c).get(position).get();
. In your second case you can useString[] meses = {"Invalid month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String monthString = meses[month < 1 || month > 12 ? 0 : month];
– Victor Stafusa
@Victorstafusa cool. I’ll do a deep search on that side. But this option not to use
switch
is a performance issue or just a matter of opinion?– viana
@Acklay is a matter of readability and maintainability of code. The
switch
is sometimes compared to thegoto
. A code usinggoto
works and performs well, but the code quickly tends to become a disorganized, gambiarrado spaghetti - the same goes for theswitch
, only in a degree not so bad, but still bad. What happens is that almost always there is some alternative better using something else. In the case ofswitch
, polymorphism is the most frequent output, but there are others (such as the ones I used above, based on arrays).– Victor Stafusa
@Victorstafusa + 1 by comment. It was quite useful and clarified enough. Abs.
– viana
Switch is one of the cleanest ways to do things like state machines and any other conditional jump situations with constant values, and in languages like C is much more performatic than various other structures because of the jump tables. Programming well has a lot to do with knowing the specialized tools and knowing how to use, and not just with using tools that are cute, colorful and with rubberized cables. As for the implementation, I would not know how to say in Java to the point of saying whether it is good or not. In C I would say that is a powerful ally.
– Bacco
In other words, the
switch
in C and some other languages is not a syntax sugar, It is a specialized and powerful tool for what has been designed. In no way can it be compared withif
,elseif
and similar, except for the "impression" that it passes from functionality. The reason it exists is different, the internal functioning is completely different. To understand the reason of the break, you have to understand that there is only one block, which is theswitch
whole, and not justcase
. In PHP, for example, this is all lost. So yes it’s really just "one more way to doif
".– Bacco
@Bacco The same can be said of
goto
- There are situations where it is the best tool. However, most of the time, those who use it end up doing lambança even. I agree that state machines and jumps based on numerical values, theswitch
has the best performance, but even so it is rather likely to be used incorrectly, especially in cases where polymorphism would be the alternative. In Java, theswitch
exists at bytecode level and hotspot will probably compile this with JIT in something similar to C in cases where performance makes a difference.– Victor Stafusa
@Victorstafusa yes, in C o
switch
is a supergoto
in the sense of functionality, but less "dangerous" because the destination is contained in the block of theswitch
. I think it’s important that people understand what he is, because his bad reputation is usually due to misuse (which is due to the lack of clarity when teaching).– Bacco
@Bacco Well, maybe I didn’t get the tone of my words right at the beginning. The idea isn’t to simply abolish the
switch
and never use it, but evaluate that normally it is not the best solution. On the other hand it also means that from time to time, in some more specific cases, it is rather the best answer. My alert is more because there are a lot of people who fall in love when you meet him and start punchingswitch
the left and right to do everything because it is quite versatile and flexible - as well as thegoto
is also.– Victor Stafusa
@Victorstafusa yes, put this way I agree. For me the important point is that (at least in C) people understand that it is a unique tool, and with a specialized behavior (and a block only, so the eventual use of break where applicable). Not a simple alternative to other structures. And as such, should not be abused, really.
– Bacco