The switch-statement tag has no usage guidance.
8
votes
6answers
3k views
How to reduce a switch in a switch statement?
So I'm making a method to create a salutation line based on two people from a database.
There are four parameters: the two names (name1 and name2) and the two genders (gender and gender2).
For every ...
4
votes
2answers
238 views
Enums in java switch-statements and completeness
I feel like I should be able to find an answer to this, but it turns out to be harder to search than expected... so:
In C#, when we do something like:
enum MyEnumClass { A, B };
static String ...
1
vote
2answers
178 views
Caveats of using String.hashCode() on a switch on java < 1.7
There are several cases when I want to switch over a String input. I decided for implementing something like:
public Object doStuff(String param) {
switch (param.hashCode()) {
case 1234546:
...
1
vote
3answers
575 views
Using “Return” over “Break” or a combination
When using the Switch statement, is using return instead of break or a combination of the two considered bad form?
while (true)
{
var operation = Randomness.Next(0, 3);
switch (operation)
...
4
votes
4answers
13k views
Checking “instanceof” rather than value using a switch statement
Is there some syntax (other than a series of if statements) that allows for the use of a switch statement in Java to check if an object is an instanceof a class? I.e., something like this:
switch ...
3
votes
4answers
566 views
Are `switch` statements generally used wrong? [closed]
I see most developers using switch statements with breaks in each and every case. Is this usage of switch statements inappropriate since a simple set of if-else statements would suffice?
Is it OK for ...
1
vote
2answers
475 views
Searching algorithm used in switch statement
What is the searching algorithm used in switch statement in C language?
If the cases are not in order still it searches proper case which means it is not a binary search algorithm, can anybody ...
35
votes
14answers
12k views
Why use an OO approach instead of a giant “switch” statement?
I am working in a .Net, C# shop and I have a coworker that keeps insisting that we should use giant Switch statements in our code with lots of "Cases" rather than more object oriented approaches. His ...
5
votes
2answers
338 views
How should I refactor switch statements like this (Switching on type) to be more OO?
I'm seeing some code like this in our code base, and want to refactor it:
(Typescript psuedocode follows):
class EntityManager{
private findEntityForServerObject(entityType:string, ...
16
votes
8answers
3k views
Why does Clang/LLVM warn me about using default in a switch statement where all enumerated cases are covered?
Consider the following enum and switch statement:
typedef enum {
MaskValueUno,
MaskValueDos
} testingMask;
void myFunction(testingMask theMask) {
switch theMask {
case ...
17
votes
10answers
18k views
Should I use switch statements or long if…else chains?
Often when I hear about the switch statement, its put off as a way to replace long if...else chains. But it seems that when I use the switch statement I'm writing more code that I would be just ...
0
votes
1answer
144 views
Switch / select case etc. compared to If … ElseIf … Else [duplicate]
I don't understand why the switch or equivalent is so popular in languages. To me, it seems like it had a place back in the days when the alternative was lots of nested if statements in the else part ...
1
vote
3answers
345 views
How to shorten the case statement from hades? [duplicate]
I'm refactoring code and have reached a horribly gigantic switch statement. Every single API method available to end users is represented as an enum and we have a switch statement iterating over the ...
1
vote
3answers
4k views
If statements vs switch cases? in a JavaScript game and if to use a function [duplicate]
I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of start to start the game or about to learn about the ...
18
votes
8answers
5k views
What is the benefit of switching on Strings in Java 7?
When I was starting to programme in Java, the fact that switch statements didn't take strings frustrated me. Then on using Enums, I realised the benefits that you get with them rather than passing ...
13
votes
4answers
787 views
Switch vs Polymorphism when dealing with model and view
I can't figure out a better solution to my problem. I have a view controller that presents a list of elements. Those elements are models that can be an instance of B, C, D, etc and inherit from A. So ...
13
votes
5answers
5k views
Multiple arguments in function call vs single array
I have a function that takes in a set of parameters, then applies to them as conditions to an SQL query. However, while I favored a single argument array containing the conditions themselves:
...
26
votes
3answers
15k views
Break on default case in switch
I am a bit puzzled on whenever or not to include break after the last case, often default.
switch (type) {
case 'product':
// Do behavior
break;
default:
// Do ...
17
votes
6answers
1k views
Map of functions vs switch statement
I'm working on a project that processes requests, and there are two components to the request: the command and the parameters. The handler for each command is very simple (< 10 lines, often < ...
14
votes
2answers
1k views
Why don't languages use explicit fall-through on switch statements?
I was reading Why do we have to use break in switch?, and it led me to wonder why implicit fall-through is allowed in some languages (such as PHP and JavaScript), while there is no support (AFAIK) for ...
21
votes
6answers
8k views
Refactoring Switch Statements and is there any real use for Switch Statements at all?
I was reading this article and was wondering, do we get rid of all switch statements by replacing them with a Dictionary or a Factory so that there are no switch statements at all in my projects.
...
4
votes
2answers
2k views
Is it possible to avoid enormously big switch in that case? [duplicate]
I'm writing a simple chess-related code with intention to write it clearly (performance doesn't matter at all). And this method I have doesn't look clean to me at all:
public static Piece ...
33
votes
8answers
18k views
Is it necessary to add the default case while using switch cases?
During a recent code review I was asked to put default cases in all the files wherever switch block is used, even if there is nothing to do in default. That means I have to put the default case and ...
-1
votes
2answers
640 views
Why is there never any controversy regarding the switch statement? [closed]
We all know that the gotostatement should only be used on very rare occasions if at all. It has been discouraged to use the goto statement countless places countless times. But why it there never ...
8
votes
5answers
2k views
If-Else V.S. Switch end of flow
I was wondering the if if-else statements, is like a switch statement that does have a break statement.
if( boolean_expression_1 )
statement_1
else if( boolean_expression_2 )
statement_2
else ...
7
votes
4answers
15k views
How is a switch statement better than a series of if statements? [duplicate]
Possible Duplicate:
Should I use switch statements or long if…else chains?
I'm working on a small program that will conduct an Insertion Sort. A number will be inputted through the ...
4
votes
3answers
452 views
Is case after case in a switch efficient?
Just a random question regarding switch case efficiency in case after case; is the following code (assume pseudo code):
function bool isValid(String myString){
switch(myString){
case "stringA":
...
5
votes
2answers
386 views
Good ways to jump to a particular state in a yielding stateful function?
I'm working on some embedded code using C. Various pieces of functionality need non-blocking stateful functions, which are mostly implemented using a switch on various states. For example, a modem ...
10
votes
6answers
3k views
Appropriate uses of fall-through switch statements
When is it appropriate to use a fall-through (classic) switch statement? Is such usage recommended and encouraged or should it be avoided at all costs?
6
votes
2answers
473 views
Patterns to avoid long switch block in UI?
Sometimes you have many entities which have common parts, but also should be addressed uniquely in UI. For example, in a CMS, you have many content types (like news, images, articles, pages, etc.) ...
2
votes
2answers
624 views
How is switch/case handled as to avoid comparisons to the case values?
I've read in multiple answers that switch/case avoids "unnecessary" comparisons, but I never learned this in college, and I'm a little stumped on how the program would figure out which case to jump to ...