Questions tagged [switch-statement]

58 questions
99
votes
3 answers

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 default behavior break; // Is it considered…
Robin Castlin
  • 1,199
  • 1
  • 8
  • 11
63
votes
17 answers

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 argument consistently goes back to the fact that…
James P. Wright
  • 2,135
  • 4
  • 18
  • 25
47
votes
12 answers

Avoiding the goto voodoo?

I have a switch structure that has several cases to handle. The switch operates over an enum which poses the issue of duplicate code through combined values: // All possible combinations of One - Eight. public enum ExampleEnum { One, Two,…
Hazel へいぜる
  • 1,165
  • 1
  • 8
  • 19
44
votes
8 answers

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 write nothing in it. Is this the right thing to…
Ankit
  • 949
  • 4
  • 11
  • 15
40
votes
8 answers

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 MaskValueUno: {}// deal with it case MaskValueDos:…
Swizzlr
  • 503
  • 1
  • 5
  • 8
38
votes
10 answers

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 writing if...else. You also have other issues like…
TheLQ
  • 13,478
  • 7
  • 55
  • 87
28
votes
7 answers

How does this switch statement do "multiple things"?

This doubt is about Switch Statements from Chapter 3: Functions of the book named Clean Code Here we have a function: public Money calculatePay(Employee e) throws InvalidEmployeeType { switch (e.type) { case COMMISSIONED: return…
The Chinky Sight
  • 529
  • 4
  • 12
28
votes
7 answers

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. Something did not quite add up. The question is, do…
Kanini
  • 2,248
  • 5
  • 24
  • 28
25
votes
5 answers

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: function searchQuery($params = array()) { …
xiankai
  • 373
  • 1
  • 3
  • 6
25
votes
6 answers

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). There are at least 20 commands, and likely will have…
beatgammit
  • 1,907
  • 3
  • 18
  • 25
20
votes
2 answers

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 explicit fall-through. It's not like a new…
zzzzBov
  • 5,794
  • 1
  • 27
  • 28
20
votes
4 answers

How is a switch statement better than a series of if statements?

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 keyboard and stored in a variable I called "num." I've decided to…
kullalok
  • 1,075
  • 3
  • 9
  • 10
19
votes
8 answers

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 around raw values — type safety (which brings easier…
anotherdave
  • 467
  • 3
  • 12
15
votes
7 answers

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?
shabunc
  • 2,424
  • 2
  • 20
  • 27
12
votes
4 answers

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 in that view controller, each item should go to a…
1
2 3 4