There are no good answers to this question :-(
since most of the answer depends on
- Effective goals (what is meant by "optimization", which is inconvenient for nested switches)
- The context in which this construct will be applied (what are the final needs implicit for the application)
TokenMacGuy was wise to ask about goals. I took the time to check out the question and its answers on a French site, and I'm still puzzled as to goals ... The last answer by Dran Dane seems to indicate less code / better readability, but let the review be sure:
- Processing speed: not a problem, nested switches are pretty effective, maybe less than 3 multiplications to get an index into the map table, but maybe not even.
- Readability: Yes, perhaps a problem. As the number of variables and levels increases, the combinatorial explosion fires, and the switch statement format tends to propagate the branch spot and related values along the length of the vertical extension. In this case, a 3-dimensional (or more) table is initialized with fct. pointers combine branch values and a function that will be called on one line.
- Writing less code: Sorry, don't help much here; at the end of the day we need to take into account a relatively large number of combinations, and the “card”, regardless of its shape, must be written somewhere. Code generators such as TokenMacGuy may come in handy, in which case it seems a little redundant. Generators have their place, but I'm not sure that it is. One of two cases: if the number of variables and the level is small enough, the generator is not worth it (it takes more time to configure it than to write the actual code in the first place), if the number of variables and levels is significant, the generated code is difficult to read, difficult to maintain. ..)
In short, my recommendation that the code is more readable (and slightly faster for writing) is the table / matrix approach described on the French site.
This solution consists of two parts:
one-time initialization of a 3-dimensional array (for 3 levels); (or, if preferred, a “more attractive” container structure), such as a tree). This is done using code like:
// This is positively more compact / readable ... FctMap[1][4][0] = fctAlphaOne; FctMap[1][4][1] = fctAlphaOne; .. FctMap[3][0][0] = fctBravoCharlie4; FctMap[3][0][1] = NULL; // impossible case FctMap[3][0][2] = fctBravoCharlie4; // note how the same fct may serve in mult. places
And a relatively simple snippet where functions should be called:
if (FctMap[cond1][cond2][cond3]) { retVal = FctMap[cond1][cond2][cond3](Arg1, Arg2); if (retVal < 0) DoSomething(); // anyway we're leveraging the common api to these fct not the switch alternative .... }
A case that may induce NOT to use the above solution if the combination space is relatively sparsely populated (many branches are not used in the switch tree) or if some functions require a different set of parameters ; For both of these cases, I would like to connect the solution proposed by Joel Goodwin first here , and which essentially combines different keys for several levels into one longer key (with a separator character if necessary), essentially smoothing the problem down to a long but single-level operator switch .
Now...
The real discussion should be about why we need such a mapping / decision tree in the first place . Unfortunately, this requires an understanding of the true nature of the underlying application. Of course, I am not saying that this indicates poor design. In some applications, a large dispatch section may make sense. However, even with the C language (which, according to French site participants, disqualified an object-oriented design), you can use an object-oriented methodology and templates. In any case, I diverge ...) It is possible that the application as a whole will be better served by alternative design patterns, where the "information tree on what to call when" was distributed in several modules and / or several objects.
Sorry to say this in rather abstract terms, this is simply the lack of application specifics ... The question remains: to challenge the idea that we need this large dispatch tree; think of alternative approaches to the application as a whole.
Alors, Bon's chance !; -)