To switch or not to switch...

Nigel Jones makes a compelling argument against the use of switch statements. A compiler has roughly two options for implementing a switch statement: 1) the equivalent of a number of if - else if - else statements or 2) a jump table. For a small number of cases option 1) may be more efficient. If you have a large number of cases option 2) is likely to be more efficient. Some compilers only support one option, others apply heuristics to choose between the alternatives. The bottom line of Nigel's argument is the actual code size of a switch statement is rather unpredictable. This can be especially confusing if you remove a case and find the code size increases due to the compiler now favoring option 1) over option 2). However, the code size for a switch statement is always comparable or less than an equivalent series of if - else if - else statements. For me, that is good enough to generously apply the switch statements in my code. Furthermore, I find the syntax of the statement aesthetically pleasing and thus easy to read. Forgetting an 'else' in a sequence of if - else if - else statements can be a nasty bug to find. I find the equivalent in a switch statement, a missing break, much harder to overlook. But hold on a second, what if you implement a jump table yourself? As the following snippet shows, a jump table is also very easy to read.

  1typedef enum {
  2    action_turnOn      = 0,
  3    action_turnOff     ,
  4    action_dimUp       ,
  5    action_dimDown     ,
  6    action_stopDimming
  7} action_t;
  9static void TurnOn(void);
 10static void TurnOff(void);
 11static void DimUp(void);
 12static void DimDown(void);
 13static void StopDimming(void);
 18void ExecuteAction(action_t action) {
 19    switch( action ) {
 20        case action_turnOn      : TurnOn()     ; break;
 21        case action_turnOff     : TurnOff()    ; break;
 22        case action_dimUp       : DimUp()      ; break;
 23        case action_dimDown     : DimDown()    ; break;
 24        case action_stopDimming : StopDimming(); break;
 25        default:
 26            break;
 27    }
 33void ExecuteAction(action_t action) {
 34    if ( action == action_turnOn ) {
 35        TurnOn();
 36    } else if ( action == action_turnOff ) {
 37        TurnOff();
 38    } else if ( action == action_dimUp ) {
 39        DimUp();
 40    } else if ( action == action_dimDown ) {
 41        DimDown();
 42    } else if ( action == action_stopDimming ) {
 43        StopDimming();
 44    }
 50void ExecuteAction(action_t action) {
 51    void (*jumpTable[])(void) = {
 52        TurnOn         ,
 53        TurnOff        ,
 54        TurnDimUp      ,
 55        TurnDimDown    ,
 56        TurnStopDimming
 57    };
 59    jumpTable[action]();

Unfortunately the compiler has more freedom in implementing a jump table than you have at the source level. You will have to create functions and reference them by pointers, which prevents the compiler from inlining them. The functions themselves may cause some administrative overhead. If the functions are inlined, which can be the case in a switch statement, it is easier for the compiler to exploit common parts and do a better job at optimizing for small code size. Not all jumps are equal, a local jump within a function may be less costly than a function call. The result is that a jump table is probably more costly in terms of code size than a switch statement or an if - else is - else sequence. That said, jump tables CAN be a good alternative. Switch statements only work with constant numbers. With some modifications jump tables can work with other data types as well. Measure what works best for you and your platform. At least you now have the choice between a sequence of if - else if - else statements, a switch statement or a jump table.