Repeating code in switch statements

I have switch expressions like this:

switch(x){ case a: executeSth(); executeA(); break; case b: executeSth(); executeB(); break; ... } 

therefore executeSth (); should be executed, with the exception of the default case, but after that some specific code is called (executeA () or executeB (), etc.). (So ​​just putting it in front of the switch fails).

Is there an effective way to reduce the amount of "executeSth ();" without sacrificing performance?

I could only imagine that it is broken into two switches (one of which executes executeSth () and one that executes specific code), but it will sacrifice performance. Maybe you have better ideas?

I am basically impressed with the code for c / C ++ or php. My goal is to minimize code size, and in the case of c / C ++, the size of the resulting executable file.

Edit: Yes, the order of the functions matters. Edit2: I have no choice between php or C ++, I need both of them to be as good as possible.

+4
source share
6 answers

Nested switch is an option ...

In this case, two switches are used, but the second does not start in the default case, so it has a slightly better performance profile than the two built-in switches.

 switch($x) { case a: case b: case c: executeSth(); switch($x) { case a: executeA(); break; case b: executeB(); break; case c: executeC(); break; } break; default: ... } 

Alternatively, a variable function can do the job ...

This is a variant of PHP that can work, although many people do not like variable functions. This is probably the best option if you want to completely remove nesting and repetition.

 switch($x) { case a: $function = "executeA"; break; case b: $function = "executeB"; break; case c: $function = "executeC"; break; default: ... } if(isset($function)) { executeSth(); $function(); } 

I also made a small live test bed here , if someone wants to test their PHP solutions before publishing them ( case 10 should executeSth() and executeA() , case 20 should executeSth() and executeB() , default should executeDefault() ).

In C ++, you can use a function pointer to achieve the same as above

I had a complete brain fart when I wrote this, fortunately idipous reminded me that we can do this with a simple function pointer.

 // Declare function pointer int (*functionCall)() = NULL; // In switch statement, assign something to it functionCall = &execute; // After the switch statement, call it int result = (*functionCall)(); 

Note. I went out, so I did not check the syntax for them. The syntax I used is C syntax and may require some minor changes to work in C ++.

+7
source

What could you do (although this might not be the most readable solution), and that if you use PHP 5.3 and higher, you can create a method like the one below:

 function mymethod($funcToCall){ executeSth(); $funcToCall(); } 

and have swtich as shown below:

 switch(x){ case a: mymethod('executeA'); break; case b: mymethod('executeB'); break; ... } 
+2
source

If in fact there are not so many things, I would leave it as it is.

One solution is, of course, to move the executeSth call to the executeA and executeB - this, of course, makes sense only if there are several places with similar code - if there are no more of them in one place, you moved two lines of code from one place to another.

Another solution might be to pass the executeA or executeB to executeSth as an argument. But it will just complicate the reading.

In general, I would say that a "smaller code" is not necessarily a "better code". The key is to make the code as clear as possible (although, of course, achieve reasonable performance and code size).

In C ++, I would also expect that if executeSth is small, it will be bound to the case code. Thus, there is no difference between overhead between one or two function calls.

+1
source

Why don't you set the flag in your case by default, and then, after the switch statement, perform the general function if the flag is not set (which means that this is not the case by default)? It sounds like a smart decision.

 $flag = false; switch(x){ case a: executeA(); break; case b: executeB(); break; default: ... // other stuff $flag = true; break; } if ( !$flag ) executeSth(); 

EDIT

I misinterpreted the question. For the reverse order, you can put the possible cases into a variable, and then use something like in_array in PHP or strstr in C ++ (not sure if there is a more efficient function):

 if ( !in_array(x, cases) { executeSth(); } switch(x){ case a: executeA(); break; case b: executeB(); break; ... } 
0
source

In an expression of different sizes: it does not matter. Readability> codeize.

Perhaps you can write something like this for your executeSth ():

 if(!notDefault){ executeSth(); } 
-one
source

(C ++) Let the preprocessor duplicate the code for you? ( Dᴏɴᴛ Uꜱᴇ Tʜɪꜱ ɪɴ Mᴀɪɴᴛᴀɪɴᴇᴅ Cᴏᴅᴇʙᴀꜱᴇ. This may impair readability. I think a solution with two switches is okay.)

 #define CASE(n) case n: printf("non-default\n"); _unused_label_##n switch (x) { CASE(1): printf("case 1\n"); break; CASE(2): printf("case 2\n"); break; default: printf("case else\n"); break; } #undef CASE 
-one
source

Source: https://habr.com/ru/post/1500167/


All Articles