Skip to content

Instantly share code, notes, and snippets.

@finalfantasia
Last active August 29, 2015 14:12
Show Gist options
  • Save finalfantasia/3a01e377a850cdb88d21 to your computer and use it in GitHub Desktop.
Save finalfantasia/3a01e377a850cdb88d21 to your computer and use it in GitHub Desktop.
Spaces

White space contributes as much to the effect produced by software text as silence to the effect of a musical piece.

The general rule, for simplicity and ease of remembering, is to follow as closely as possible the practice of a standard written language. By default, we will assume this language to be English, although it may be appropriate to adapt the conventions to the slightly different rules of other languages.

Here are some of the consequences. You will use a space:

  • Before an opening parenthesis, but not after: f (x) (not f(x), the C style, or f( x)).
  • After a closing parenthesis unless the next character is a punctuation sign such as a period or semicolon; but not before. Hence: proc1 (x); x := f1 (x) + f2 (y).
  • After a comma but not before: g (x, y, z).
  • After the two dash signs that start a comment: -- A comment.

Similarly, the default rule for semicolons is to use a space after but not before:

p1; p2 (x); p3 (y, z)

Here, however, some people prefer, even for English-based software text, the French style of including a space both before and after, which makes the semicolon stand out and emphasize the symmetry between the components before and after it:

p1 ; p2 (x) ; p3 (y, z)

Choose either style, but then use it consistently. (This book uses the English style.) English and French styles have the same difference for colons as for semicolons; since, however, the software notation only uses colons for declarations, in which the two parts—the entity being declared and its type—do not play a symmetric role, it seems preferable to stick to the English style, as in your_entity: YOUR_TYPE.

Spaces should appear before and after arithmetic operators, as in a + b. (For space reasons, this book has omitted the spaces in a few cases, all of the form n+1.)

For periods, the notation departs from the conventions of ordinary written languages since it uses periods for a special construct, as originally introduced by Simula. As you know, a·r means: apply feature r to the object attached to a. In this case, there is a space neither before nor after the period. To avoid any confusion, this book makes the period bigger, as illustrated: · rather than just .

There is another use of the period: as decimal point in real numbers, such as 3.14. Here, to avoid any confusion, the period is not made any bigger.

—Bertrand Meyer (1997), Object-Oriented Software Construction (2nd ed., pp. 895-896). Upper Saddle River, NJ: Prentice Hall PTR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment