syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 6 : Layout

PART 3 : LAYOUT

CHAPTER 6 : Code Layout
6.1 Basic principles of code layout
6.2 Use of Spaces
6.3 Use of blank lines
6.4 Use vertical alignment
6.5 Indentation level
6.6 Line wrapping
6.7 Braces
6.8 Use of parentheses
6.9 Nested single statement
6.10 Empty statements
6.11 'else..if'
6.12 'switch' statements
6.13 'do..while'
6.14 Labels
6.15 Data declarations
6.16 Function declaration
6.17 Preprocessor commands
6.18 Summary

<--Prev page | Next page -->

 

6.12  'switch' statements

There are a few variations on the layout for a switch statement, beyond the brace style discussed above:

 

switch ( WidgetStyle )
{
    case WS_GOTHIC:     ChangeCutter( R_GOTHIC );
                        break;
    case WS_ROMAN:      ChangeCutter( R_ROMAN );
                        break;
    default:            ChangeCutter( R_MODERN );
                        break;
}

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

This 'toothbrush' style tends to tramp rapidly across to the right in the indenting of both the case and the code for each case starting on the same line as the case (particularly if the cases are long). These can be addressed with a 'comb' layout:

 

switch ( WidgetStyle )
{
case WS_GOTHIC:
    ChangeCutter( R_GOTHIC );
    break;

case WS_ROMAN:
    ChangeCutter( R_ROMAN );
    break;

default:
    ChangeCutter( R_MODERN );
    break;
}

 

Each case is now more in the style of an if statement, with the code for each case indented on the line following the decision. Starting the case in the same column as the switch is like matching an else with an if. It also saves horizontal space.

 

<--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