Off-side rule


A computer programming language is said to adhere to the off-side rule if blocks in that language are expressed by their indentation. The term was coined by Peter J. Landin, possibly as a pun on the offside rule in football. This is contrasted with free-form languages, notably curly-bracket programming languages, where indentation is not meaningful and indent style is only a matter of convention and code formatting.

Definition

, 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."

Code examples

The following is an example of indentation blocks in Python.
The colons are part of the Python language syntax for readability; they are not necessary to implement the off-side rule.
In Python, the rule is taken to define the boundaries of statements rather than declarations.

def is_even -> bool:
"""Determine whether number 'a' is even."""
if int 0:
print
return True
print
return False

Python also suspends the off-side rule within brackets. A statement within brackets continues until its brackets match :


In this dictionary, the keys are indented, and a list is split between two lines.

Implementation

The off-side rule can be implemented in the lexical analysis phase, as in Python, where increasing the indentation results in the lexer outputting an INDENT token, and decreasing the indentation results in the lexer outputting a DEDENT token. These tokens correspond to the opening brace in languages that use braces for blocks, and means that the phrase grammar does not depend on whether braces or indentation are used. This requires that the lexer hold state, namely the current indentation level, and thus can detect changes in indentation when this changes, and thus the lexical grammar is not context-free – INDENT/DEDENT depend on the contextual information of previous indentation level.

Alternatives

The primary alternative to delimiting blocks, popularized by C, is to ignore whitespace and mark blocks explicitly with curly brackets 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 statementssloppily indented code might lead the reader astray.
Lisp and other S-expression-based languages do not differentiate statements from expressions, and parentheses are enough to control the scoping of all statements within the language. As in curly bracket languages, whitespace is mostly ignored by the reader. Whitespace is used to separate tokens. The explicit structure of Lisp code allows it to perform automatic indentation, which acts as a visual cue for human Lisp readers.
Another alternative is for each block to begin and end with explicit keywords. For example, in ALGOL 60 and its descendant Pascal blocks start with keyword begin and end with keyword end. In some languages, this means that newlines are important , but the indentation is not.
In BASIC and Fortran, blocks begin with the block name and end with the block name prepended with END. In Fortran, each and every block can also have its own unique block name, which adds an additional level of explicitness to lengthy code. ALGOL 68 and the Bourne shell are similar, but the ending of the block is usually given by the name of the block written backward.
An interesting variation on this is made by Modula-2, a Pascal-like language which does away with the difference between one and multiline blocks. This allows the block opener. It also fixes dangling else. Custom is for the end token to be placed on the same indentation level as the rest of the block, giving a blockstructure that is very readable.
One advantage to the Fortran approach is that it improves readability of long, nested, or otherwise complex code. A group of outdents or closing brackets alone does not provide any contextual cues as to which blocks are being closed, necessitating backtracking, and closer scrutiny while debugging. In addition, languages that allow a suffix for END-like keywords further improve such cues, such as continue versus continue for x. However, modern code editing applications often provide visual indicators and features such as folding to assist with these drawbacks.

Off-side rule languages

; Programming languages
; Other languages