What is the best / easiest way to use nested switch / case statements?

What is the best practice of the following two switch / case statements?

Is there an easier way (less code) for this?

switch (myValue) { case 1: { methodFor1(); break; } case 2: case 3: { methodFor2or3(); if (myValue == 2) methodFor2(); if (myValue == 3) methodFor3(); break; } } ...or... switch (myValue) { case 1: { methodFor1(); break; } case 2: case 3: { methodFor2or3(); switch (myValue) { case 2: { methodFor2(); break; } case 3: { methodFor3(); break; } } break; } } 
0
source share
8 answers
 switch (myValue) { case 1: methodFor1(); break; case 2: methodFor2or3(); methodFor2(); break; case 3: methodFor2or3(); methodFor3(); break; } 

Why all the hassle to avoid repeating methodFor2or3() once?

+8
source

Another alternative:

 switch (myValue) { case 1: { methodFor1(); break; } case 2: { methodFor2or3(); methodFor2(); break; } case 3: { methodFor2or3(); methodFor3(); break; } } 
+3
source

Since functions are first-class objects in actionscript3, you can build a hash of values ​​for functions, for example:

 var myDict:Dictionary = new Dictionary(); myDict[1] = methodFor1; myDict[2] = methodFor2; function handleStuff(myVal:Number):void{ var myFunction:Function = myDict[myVal]; myFunction(); } 

hope this helps!

+2
source

In my programming of switch I try to ensure that each case has at most one line of code + a break; . This is because a switch can quickly become large and complex, and my brain is not very complicated.

So, in your case, I would write:

 switch (myValue) { case 1: { methodFor1(); break; } case 2: { methodFor2(); break; } case 3: { methodFor3(); break; } } 

and then do methodFor2 and methodFor3 each call to methodFor2or3 .

+1
source

If there is only one (rather simple) line between the two cases (as a function call in your example), I prefer to double this line only for better readability. Otherwise, it is a matter of taste. I usually prefer if conditions inside case because you cannot confuse different levels.

0
source

How about something more:

 var method:String = "methodFor" + String(value); this[method](); if (value == 3) methodFor2or3(); 

You might consider splitting the or3 call if you need simpler code:

 switch (value) { case 1: methodFor1(); break; case 2: methodFor2(); break; case 3: methodFor3(); break; } if (value == 2 || value == 3) methodFor2or3(); 
0
source

When you are dealing with programming a stream of control problems, less code is not really the attribute you want to optimize. This is much better with structural issues to keep your code simple and readable.

So, for the above, the first option is not bad, although the “through” case for 2 can easily be missed. It is best to make the parent switch 1 or 2, then in the second case, call another function that processes the sub-series (formally 2 and 3), and make it manageable using another (sub-file) variable (make sure that you do not overload the "value" source variable "value".

Philosophically, all three cases are part of the same switch, otherwise they are not. If they are, then they should be treated equally (and indistinguishably from each other). If this is not the case, then they must be controlled by two separate variables and processed in two separate functions. An attempt to save bandwidth by combining them complicates the situation unnecessarily.

Pavel.

0
source

I answer, believing that you know the example that you gave is invented and better written in other ways. If you just want to know whether it is better to install the sockets or use if-else inside the switch, it completely depends on the taste and individual situation.

In my opinion, the fact that your unit is inside the switch does not matter. If the logic is better served by the switch, use the switch (nested or not). If this is better served by if-else, use this.

0
source

All Articles