问题
What is the better practice of the following two switch/case statements?
Is there an easier way (less code) to do 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;
}
}
回答1:
switch (myValue)
{
case 1:
methodFor1();
break;
case 2:
methodFor2or3();
methodFor2();
break;
case 3:
methodFor2or3();
methodFor3();
break;
}
Why all the hassle just to avoid repeating methodFor2or3() once?
回答2:
One more alternative:
switch (myValue)
{
case 1:
{
methodFor1();
break;
}
case 2:
{
methodFor2or3();
methodFor2();
break;
}
case 3:
{
methodFor2or3();
methodFor3();
break;
}
}
回答3:
Since functions are first class objects in actionscript3, you could build a hash of values to functions, like so:
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!
回答4:
In my programming of switch statements, I aim for making each case have at most one line of code + a break;. This is because switch can quickly get big and complicated, and my brain isn't good at complicated.
So, in your case, I would write:
switch (myValue)
{
case 1:
{
methodFor1();
break;
}
case 2:
{
methodFor2();
break;
}
case 3:
{
methodFor3();
break;
}
}
and then make methodFor2 and methodFor3 each call methodFor2or3.
回答5:
If there is only one (fairly simple) line in common between the two cases (as the function call in your example), then I prefer to double that line just for the sake of better readability. Otherwise, it's a matter of taste. I tend to prefer if conditions inside the case statements, because that way you can't confuse the various levels.
回答6:
How about something more like:
var method:String = "methodFor" + String(value);
this[method]();
if (value == 3) methodFor2or3();
You might think of splitting out the or3 call if it's simpler code you want:
switch (value) {
case 1: methodFor1(); break;
case 2: methodFor2(); break;
case 3: methodFor3(); break;
}
if (value == 2 || value == 3) methodFor2or3();
回答7:
When you're dealing with programming flow of control issues, "less code" isn't really the attribute you want to optimize on. It's far better with structural issues to bend your code towards being simple and readable.
So for the above, the first option isn't bad, although the 'flow-through' case for 2 can easily be missed. Better is to just do the parent switch on a 1 or 2, then in the second case call another function that handles the sub-cases (formally 2 and 3), and make this driven by a different (sub-case) variable (to insure that you're not overloading the 'meaning' of the initial 'value' variable.
Philosophically, all three cases are part of the same switch, OR they are not. If they are, then they should be treated equally (and indistinguishably from each other). If they are not, then they should be driven by two separate variables and handled in two separate functions. Trying to save 'bandwidth' by combining them complicates thing unnecessarily.
Paul.
回答8:
I am answering assuming that you know the example case you gave is contrived and is better written in other ways. If you just want to know if it's best to nest switches or to use if-else within a switch, it totally depends on taste and the individual situation.
In my opinion, the fact that your block is inside a switch makes no difference. If the logic would be better served by a switch, use a switch (nested or not). If it would be better served by an if-else, use that.
来源:https://stackoverflow.com/questions/496189/what-is-the-best-easiest-way-to-use-nested-switch-case-statements