Off-side rule
From Free net encyclopedia
Template:Otheruses2 A computer programming language is said to adhere to the off-side rule if in it the scope of declarations (a block) is expressed by their indentation, i.e., blocks are formed (indicated) via indentation. The term and the idea are attributed to Peter Landin, and the term can be seen as a pun on the rule in football.
Contents |
Definition
Any non-whitespace token to the left of the first such token on the previous line is taken to be the start of a new declaration.— Landin, P. J., The Next 700 Programming Languages
Communications of the ACM, 9:157–165, March 1966.
Code examples
The following is an example of indentation blocks in Python (note also the obligatory colons - they are merely syntactical noise to make it easier to read) [1]:
def is_even(a): if a / 2.0 == a / 2: return True else: return False
Alternatives
The way popularized by C programming language is to ignore whitespace and mark blocks between curly braces {
and }
. An advantage of this is that program code can be automatically reformatted and neatly indented without fear of the block structure changing. A disadvantage is that a human reader of the code pays attention to indentation and might miss the formal meaning that is communicated with braces.
A variation of the curly braces is the Pascal convention of starting blocks with keyword begin and ending them with end. LISP doesn't differentiate statements from expressions, and parentheses are enough in it.
Another way to mark blocks is to implicitly start one after each control keyword and end it with the keyword written backwards. Thus in POSIX shell, case starts a conditional statement and it spans until the matching esac.
Off-side rule languages
- ISWIM, the abstract language that introduced the rule
- ABC, Python's parent
- Miranda, Haskell's parent
- Occam
- Pliant