User Tools

Site Tools


guides:programstyle:whitespace

This is an old revision of the document!


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)

An exception to this rule occurs when a line becomes long and needs to be wrapped. This frequently occurs in

  • Functions with many parameters
  • Output lines

See the Indentation section for examples.

A second exception occurs when declaring multiple variables of the same type. Again, see Indentation section for examples.

Use blank lines to separate sections of code.

Much like new paragraphs in English composition, blank line should be used to separate different thoughts.

The following is an unacceptable use of blank lines

int max=0, 
    min=0, 
    num,
    sum,
    i, 
    count;
bool first = true;
cin >> count;
for(i = 0; i < count; i++) {
    cin >> num;
    if(first) {
       min = num;
       max=num;
       first = false;
    }
    sum += num;
    if (num > max) {
       max = num;
    }
    if (num < min) {
        num = min;
    }
}

The following is a more acceptable use of blank lines.

int max=0, 
    min=0, 
    num,
    sum,
    i, 
    count;
 
bool first = true;
 
cin >> count;
for(i = 0; i < count; i++) {
    cin >> num;
 
    if(first) {
       min = num;
       max=num;
       first = false;
    }
 
    sum += num;
 
    if (num > max) {
       max = num;
    }
 
    if (num < min) {
        num = min;
    }
}

Consistently use a single style for brace placement.

Place spaces between operators in statements.

Do not exceed 80 characters for any line.

guides/programstyle/whitespace.1596560283.txt.gz · Last modified: 2024/07/25 15:01 (external edit)