Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [switch-statement]

The tag has no usage guidance.

4
votes
2answers
333 views

Why do I need a game loop with states, instead of init the next state in current state directly?

For example, it is common to see games with game loop and states: stateChanged(){ switch(state){ STATE.PLAYER_SELECT_CHARACTER: this.currentController=new PlayerSelectCharacterController()...
1
vote
1answer
146 views

Remove duplicated switch statements caused by not knowing type at compile time of opencv matrix?

I have a situation that occurs 5 times in my code, and continues to grow. Basically I have the same switch statement structure every time I need to perform an operation that involves my matrix and ...
1
vote
4answers
235 views

What should be the last entry in a switch/case statement?

When writing a switch statement that only ever has to deal with a known set of values (imagine an implicit enumeration), I find myself wondering what should be the last entry in the construct. I'm ...
0
votes
1answer
155 views

Refactoring bad code switch statment without distort everything

Once in a while I’m stumbling on switch statements during a Code Review session. I would like to find a more elegant way of this code .... $istannceOfClass = $repository->loadFoo(); switch($...
0
votes
1answer
188 views

In Qt or C++, is there a good means for creating a switch with dynamically generated cases, or should I just use a for loop (or something else)?

Presume this situation: Max Number of 256 key slots. Key slots are defined by a struct, and a variable for each (256) has to exist. User defines which keys slots are active at initiation (in my ...
1
vote
1answer
111 views

Is it standard to log each case of a switch statement?

Some code I wrote was sent back to me during peer review, telling me to add logging to the start of each case in a switch statement in a servlet (Java, if that matters), so if something goes wrong, we ...
4
votes
2answers
326 views

OOP: Switching on polymorphic types and maintaining state throughout workflow

High level: I think my design pattern is flawed. Despite implementing Polymorphism, I find myself relying on large switch statements based on derived Type within my WPF application. As I'm adding more ...
11
votes
7answers
969 views

Implementation of object state in an OO language?

I've been given some Java code to look at, which simulates a car race, of which includes an implementation of a basic state machine. This is not a classical computer science state machine, but merely ...
1
vote
3answers
2k views

Most efficient method for large switch statements

Let's say you have many paths that an application can take at a certain point based on the value of a specific input (for example a simple int). Is there a certain method that is most efficient for ...
4
votes
1answer
174 views

Is there something to be said for putting the “default” case of a switch statement first?

Like: switch (value) { default: something_common() case 1: niche_case() ... } etcetera. if-else statements require an "if" first and an "else" last, but switch statements are more flexible ...
0
votes
1answer
464 views

How to structure huge conditional statement?

I'm creating some kind of a "personal assistant" application which is basically a web service that receives a message and then does something according to it. For example I send "what time is it?" ...
7
votes
2answers
2k views

is switch(this) antipattern or bad practice in Java for Enums?

At work I bumped into a problem to see how enums are growing and storing business logics. As they grew, the constructors grew a lot. At one point I found out instead of putting in let's say the tenth ...
-4
votes
4answers
393 views

Why aren't named switch statements a language feature? [closed]

I could imagine the below code being somewhat useful. Is there a reason this pattern hasn't made it into programming languages? To be clear the string was just what I chose as an example you could ...
9
votes
6answers
4k 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
4k 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 Test(...
1
vote
2answers
916 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: ...
4
votes
3answers
16k 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) ...
3
votes
4answers
3k 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
2k 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 ...
7
votes
2answers
2k 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, serverObject:any)...
0
votes
1answer
543 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
551 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 ...
3
votes
3answers
20k 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 ...
19
votes
8answers
8k 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 ...
12
votes
4answers
2k 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 ...
22
votes
5answers
19k 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: ...
69
votes
3answers
67k 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 ...
20
votes
6answers
5k 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 < 5)....
6
votes
5answers
41k 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 (...
4
votes
2answers
4k 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 ...
39
votes
8answers
38k 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 ...
29
votes
8answers
9k 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 ...
8
votes
5answers
3k 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 ...
17
votes
2answers
2k 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 ...
15
votes
4answers
36k 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 ...
5
votes
3answers
518 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": ...
27
votes
6answers
16k 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. ...
5
votes
2answers
654 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 ...
6
votes
3answers
19k views

How to use a switch statement with enum efficiently?

I would like to know how I can use a switch statement with enum values for the following scenarios: I am making a small program for a flight reservation system. The program is meant to enter certain ...
14
votes
7answers
10k 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
528 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.) ...
4
votes
2answers
2k 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 ...
56
votes
17answers
24k 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 ...
34
votes
10answers
36k 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 ...