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.8  Using 'goto'

The unconditional branch has long been reviled in structured programming texts, and many programmers avoid it like the plague. It decreases readability and increases potential for error by breaking the structure and transferring execution to anywhere else within the function. It is a statement in two parts, the goto itself and the label, which can cause synchronization problems. Nevertheless, if it is to be used, there are some elements that should be considered.

8.8.1  Inserting around labels

If you are inserting code at the same point as the label, do you insert it before or after the label? If the label marks the beginning of a section of code, then the insertion is probably after it. If it marks the end of a section, then the insertion may be before it. This can be helped by a clear indication in the label name or the comment as to whether it marks the end or the beginning of a chunk of code:

 

    goto StartFileCheck;
...
/* Check integrity of all files */
StartFileCheck:

 

8.8.2  Jump outwards

Jumping into compound statements is hazardous, as it disturbs the flow of the code, and is likely to result in problems such as jumping past some essential initialization. Bad examples include: jumping into or back and fore from the two parts of an if..else statement, jumping into switch statements and jumping into iteration statements.

By using the simple rule of never jumping into compound statements, some of the dangers of goto can be avoided.

A further constraint may be to not jump backwards. This prevents goto being used for looping, and will make the label easier to find (just look downwards).

8.8.3  Escaping deep nesting

A classic case where goto is often allowed is where it is necessary to escape from a deep level of nesting, typically for error handling (see 10.4.3).

Note that goto can be used to jump out of situations where break or continue, which can only jump out one level, could not be used.

In deciding if, when and where to use goto, balance its usefulness carefully against its hazards, and beware of emotional arguments based on unreasoned prejudice.

 

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