syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 8 : Language Usage

PART 4 : USAGE

CHAPTER 8 : Language Usage
8.1 General principles of language usage
8.2 Using expressions
8.3 Using 'if'
8.4 Using 'while'
8.5 Using 'for'
8.6 Using 'do'
8.7 Using 'switch'
8.8 Using 'goto'
8.9 Using 'continue' and 'break'
8.10 Using 'return'
8.11 Using functions
8.12 Using '#define'
8.13 Conditional compilation
8.14 Other preprocessor commands
8.15 Summary

<--Prev page | Next page -->

 

8.7  Using 'switch'

The switch statement is more limited than the Pascal case, in that it can only switch on integers. Thus when it is required to switch on such as string values, it is better to use the if...else if... construct (see 8.3.4) than to artificially convert to an integer just to use the switch.

Because the switch statement is effectively a glorified goto, execution starts from the selected case, and will happily fall into the next case and right out of the bottom of the switch statement, enabling such imaginative uses as:

 

switch ( ErrorNo )
{
case ERR1: if ( FileLen > F_LEN_MAX )
case ERR2:     fclose( FileNo );
case ERR3: printf( "Error %d\n", ErrorNo );
}

 

This is very succinct, saving on vertical space. It is also very liable to error, especially if modified. It also does not cater for possible other cases.

8.7.1  Use of 'break'

A simple standard is to insist that each case is independent of all other cases. This can be done if each case is terminated with a break. Thus the above example becomes:

 

switch ( ErrorNo )
{
case ERR1:
    if ( FileLen > F_LEN_MAX )
        fclose( FileNo );
    printf( "Error %d\n", ErrorNo );
    break;
case ERR2:
    fclose( FileNo );
    printf( "Error %d\n", ErrorNo );
    break;
case ERR3:
    printf( "Error %d\n", ErrorNo );
    break;
}

 

Each case is now a separate 'chunk' which can be read and edited independently, although at the cost of duplicated code. If you wish to allow fall-through between coded case's, then this should be made very clear with a comment where the break should be:

 

case ERR2:
    fclose( FileNo );
        printf( "Error %d\n", ErrorNo );
        /************* FALL-THROUGH ****************/
case ERR3:

-----------------------------------------------------

It is reasonable to allow fallthrough on multiple cases which execute the identical code:

 

...
case ERR3:
case ERR4:
case ERR5: printf( "Error %d\n", ErrorNo );
                break;

 

8.7.2  Use of 'default'

Defensive programming dictates that all possible case's should be catered for, even ones that are not expected. Thus the default statement should always be used. Putting it last makes it easier to find; it also helps readability:

 

switch ( ErrorNo )
{
case ERR1:
...
default:
    printf( "Unknown error %d\n", ErrorNo );
    break;
}

 

Thus, if an error in a previous statement results in an invalid ErrorNo, then this will be caught in the default statement. Note that a break statement is used, even though it is clearly redundant here. This allows a simple "break on all case's" rule to be used. It will also preserve the code if a case is added later, after the default.

 

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-
Massive Content -- Maximum Speed