There is a lot of code in my current project using switch with too many cases.
Switch(c) { case CASENUMBERONE: //50-100 lines of code in each code ... case CASENUMBERTWENTY: .. } //thousands of lines of macro definition in another file #define CASENUMBERONE 1 ... #define CASENUMBERTWENTY 20 ...
This does not seem to be a reasonable programming style. How to avoid this?
It depends.
You can write a function for each case. You can embed them if you are concerned about performance, but in real life, in fact, you can get a performance boost due to the presence of separate functions due to better consistency of the command cache.
, . , /, . - , - . , NULL.
.
@Zain, / ( , ).
, , , .
handler[c](): -)
handler[c]()
, , .
, ( switch).
switch
hth.,
, . , , , , . "" , . , , , , . .
, .
1500 = (50-100 ) * 20
. .
/ .
/, , . ( )
( )
, . , , , ; , , .
, - , , ; .
++ :/
, , ,
, . boost:: assign:: map_list_of.
find . , .
- :
, case foo , . , , . , , .
, , ptr- > foo (c) ptr- > foo(). , .
Source: https://habr.com/ru/post/1771243/More articles:Downloading a PDF file from the resource directory for iPhone crashes - iphoneSSE3 instructions in F # - sseHow to parse microseconds with Time :: Piece strptime? - perlconditional statements in php heredocs syntax? - phpjavaReflection returns a list of classes that implement a specific interface - javaHow to change the schema in an OO database such as DB4O? - javaFair Task Queue for Java EE - javaUnix shell code portable enough to work on all shells - unixPlaying a video track when entering a background? - iphoneMySQL присоединяется к записи, которая может не существовать - sqlAll Articles