||This article needs additional citations for verification. (December 2011)|
A computer programming language is said to adhere to the off-side rule if the scope of declarations (a block) in that language is expressed by their indentation. The term and the idea are attributed to Peter J. Landin, and the term can be seen as a pun on the offside law of football (soccer).
Peter J. Landin, in an article called "The Next 700 Programming Languages", defined the off-side rule thus: "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."
def is_even(a): if a % 2 == 0: print('Even!') return True else: print('Odd!') return False
The primary alternative to delimiting blocks, popularized by C, is to ignore whitespace and mark blocks explicitly with curly brackets (i.e.
}) or some other delimiter. While this allows for more freedom – the developer might choose not to indent small pieces of code like the break and continue statements – sloppily indented code might lead the reader astray.
Lisp doesn't differentiate statements from expressions, and parentheses are enough to control the scoping of all statements within the language. As in curly bracket languages, the lengths of the whitespaces are ignored.
Another alternative is for each block to begin and end with explicit keywords. For example in Pascal blocks start with keyword
begin and end with keyword
end. In some languages (but not Pascal), this means that newlines are important (unlike in curly brace languages), but the indentation is not. In BASIC and FORTRAN, blocks begin with the block name (such as
IF) and end with the block name prepended with
END IF). The Bourne shell (sh, and bash) is similar, but the ending of the block is usually given by the name of the block written backward (e.g.
case starts a conditional statement and it spans until the matching
Off-side rule languages
- F# (if
#light "off"is not specified)
- Haskell (only for
ofclauses when braces are omitted)
- ISWIM, the abstract language that introduced the rule
- Landin, Peter J. (March 1966). "The next 700 programming languages". Communications of the ACM 9 (3): 157–166. doi:10.1145/365230.365257. Archived from the original on 2007-04-17.
- Python FAQ on colons
- The Haskell Report - Layout
|This programming language–related article is a stub. You can help Wikipedia by expanding it.|