Is it bad practice to have a switch case in a switch case? If so, what are alternatives? I don't really want to use if
/else if
if I don't need.
Instead of doing some like:
if((this == 1) && (that == 1)){ //something }
else if((this == 1) && (that == 2)){ //something }
else if((this == 2) && (that == 3)){ //something }
I was thinking along the lines of:
switch(this){
case 1:
switch(that){
case 1:
// something
break;
....
}
break;
....
}
It just looks really wrong to me. Not wrong in syntax but wrong in terms of proper practice.
Nested switch structures are difficult to understand because you can easily confuse the cases of an inner switch as belonging to an outer statement. Therefore nested switch statements should be avoided.
By nesting switch statements, you're obfuscating your code. That might be useful, but generally a really bad coding practice.
They work great during the creation of your initial set of cases, but once you have to extend them, they get unhandy and bloat your code. Furthermore, when you want to map complex scenarios, the switch-case is a mess.
Last but not least, because a switch statement requires us to modify a lot of classes, it violates the Open-Closed Principle from the SOLID principles. To conclude, switch statement are bad because they are error-prone and they are not maintainable.
It's bad practise to have massive functions that do lots of different things. If you have a switch case in a switch case, that suggests your function is probably too big and you should consider splitting it up into smaller easier-to-understand chunks.
But there are no hard-and-fast rules; it all depends on the exact scenario.
Avoid following approach
switch(id)
{
case 1:
{
switch (subid)
{
case 4:
// nested code
}
}
case 2:
// some code
}
The preferable approach and improve your code by moving nested section into a method
switch(id)
{
case 1:
SubSwtich(subid);
break;
case 2:
// some code
}
function SubSwtich(int subid)
{
switch (subid)
{
case 4:
// nested code
}
}
i would consider it a bad pratice. in most cases this is unreadable.
you can extract the "sub" switch-cases to methods.
If it makes your code harder to read, then it's bad practice.
Whether that's the case in your particular example is up to you to decide, but in general I'd say it probably would be the case.
You asked for alternatives...
Extract some of your code into a sub-function. Eg:
case 'foo' :
myVar = 'blah';
break;
case 'bar' :
myVar = secondLevelFunction();
break;
Here, the secondLevelFunction()
contains the additional switch()
statement where each case
returns a value for myVar
.
Use array mapping. Eg:
var mapper = {'foo':'blah', 'bar':'bibble', etc};
//now, instead of a big switch(input) { .... } block that sets myVar
//for each option, you can just set it directly in a single line, like so:
var myVar = mapper[input];
In addition, if you're looking for concrete measures of code quality, you should learn about Cyclomatic Complexity. This is a measure of how complex a function is. The measurement is taken by looking at how many "decision points" the function has. Each case
, if
, loop, etc is a "decision point". The more you have, the more complex your function.
Cyclomatic Complexity is stronly linked to code quality and good coding practices, so if your function has a high CC score (which it probably will do if it has multiple nested switch
blocks), then it is a sign of poor code quality. Both the alternative solutions I described above can help with this. I'll leave it to you to read up more on CC.
Obviously the alternative solutions would need to be adapted to your needs, but hopefully they give you some ideas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With