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.
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.
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; }
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
See the Indentation section for examples.
A second exception occurs when declaring multiple variables of the same type. Again, see Indentation section for examples.
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 = 0, 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 = 0, 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; } }
Braces are used to mark the body of control structures, type definitions and other related blocks of code. A consistent use of bracket placement provides readers with a visual guide to the structure of a program. There have been many styles ( see External Link|Wikipedia Article on Indentation Style) developed.
Several styles are approved in the local style guide.
In the Allman style, braces associated with the start of a control statement begin on the next line, indented at the same level.
int main() { int i; bool printed = false; for(i = 1; i < 100; i++) { printed = false; if (i % 3 == 0) { cout << "fizz"; printed = true; } if (i % 5 == 0) { cout << "buzz"; printed = true; } if (not printed) { cout << i; } cout << endl; } return 0; }
In this style, the opening brace is placed at the end of the beginning of a control structure.
int main() { int i; bool printed = false; for(i = 1; i < 100; i++) { printed = false; if (i % 3 == 0) { cout << "fizz"; printed = true; } if (i % 5 == 0) { cout << "buzz"; printed = true; } if (not printed) { cout << i; } cout << endl; } return 0; }
While either style is acceptable, programmers should select a single style and employ it constantly.
Proper spacing in statements, especially mathematical calculations make it easier to read these statements.
Unacceptable example:
for(i=0;i<20;i++) { ... } x = r*cos(theta)*sin(phi); r1 = (-b+sqrt(4*a*c))/(2*a);
Acceptable example:
for(i = 0; i < 20; i++) { ... } x = r * cos(theta) * sin(phi); r1 = (-b + sqrt(4*a*c)) / (2*a);