This is an old revision of the document!
Table of Contents
Use of Whitespace in Programs
Whitespace is a character or set of characters that produce “space” in a text file. Generally whitespace is generated by three keys, the spacebar, the tab key, and the enter key.
The appropriate use of whitespace characters greatly enhances the readability of code.
Short Answer
- Do not use tabs
- One statement per line
- One line per statement in most cases.
- Use blank lines to separate sections of code.
- Consistently use a single style for brace placement.
- Place spaces between operators in statements.
- Do not exceed 80 characters for any line.
- Follow the indentation guidelines.
Discussion
Do not use tabs
Tabs are problematic as different editors will interpret tabs differently. This will cause programs to appear to be incorrectly formatted when the code is opened in different editors.
Most editors have an autoindent feature where the editor will attempt to duplicate the indentation used on the previous line. Some editors will use a tab in place of spaces. This feature should be disabled.
Consult the documentation of your favorite editor to disable automatic insertion of tab.
Disabling Tabs in Selected Editors
- vi
- In vi, add set expandtab to your .vi, .exrc, vimrc or other
One statement per line
Generally you should only put one statement per line.
Acceptable use example
float x = 7; int i; for(i = 0; i < 10; i++) { x = 3 * i + 4; cout << " x = " << x << endl; }
Unacceptable use example
float x=7; int i; for(i = 0; i < 10; i++) { x = 3 * i + 4; cout << " x = " << x << endl; }
One line per statement in most cases.
When possible, a statement should occupy a single line.
An unacceptable example
x = r *cos(theta * M_PI/180) *sin(phi * M_PI/180)