Off-side rule
   HOME

TheInfoList



OR:

A computer
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
is said to adhere to the off-side rule of
syntax In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituenc ...
if
block Block or blocked may refer to: Arts, entertainment and media Broadcasting * Block programming, the result of a programming strategy in broadcasting * W242BX, a radio station licensed to Greenville, South Carolina, United States known as ''96.3 ...
s in that language are expressed by their
indentation __FORCETOC__ In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph. Many computer languages have adopted this technique to designate "paragraphs" or o ...
. The term was coined by Peter Landin, possibly as a pun on the offside rule in
association football Association football, more commonly known as football or soccer, is a team sport played between two teams of 11 players who primarily use their feet to propel the ball around a rectangular field called a pitch. The objective of the game is ...
. This is contrasted with
free-form language In computer programming, a free-form language is a programming language in which the positioning of characters on the page in program text is insignificant. Program text does not need to be placed in specific columns as on old punched card syst ...
s, notably curly-bracket programming languages, where indentation has no computational meaning and
indent style In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and oft ...
is only a matter of coding conventions and formatting.


Definition

Peter Landin, in his 1966 article " 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 needed to implement the off-side rule. In Python, the rule is taken to define the boundaries of statements rather than declarations. def is_even(a): """Determine whether number 'a' is even.""" if a % 2

0: print('Even!') return True print('Odd!') return False
Python also suspends the off-side rule within brackets. A statement within brackets continues until its brackets match (or mismatch): In this
dictionary A dictionary is a listing of lexemes from the lexicon of one or more specific languages, often arranged alphabetically (or by radical and stroke for ideographic languages), which may include information on definitions, usage, etymologie ...
, the keys are indented, and a list is split between four lines.


Implementation

The off-side rule can be implemented in the
lexical analysis In computer science, lexical analysis, lexing or tokenization is the process of converting a sequence of characters (such as in a computer program or web page) into a sequence of ''lexical tokens'' ( strings with an assigned and thus identified ...
phase, as in Python, where increasing the indenting results in the lexer outputting an INDENT token, and decreasing the indenting 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 indent level, and thus can detect changes in indentation when this changes, and thus the lexical
grammar In linguistics, the grammar of a natural language is its set of structural constraints on speakers' or writers' composition of clauses, phrases, and words. The term can also refer to the study of such constraints, a field that includes doma ...
is not context-free: INDENT and DEDENT depend on the contextual information of the prior indent level.


Alternatives

The primary alternative to delimiting blocks by indenting, popularized by broad use and influence of the language C, is to ignore
whitespace character In computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area ...
s and mark blocks explicitly with
curly bracket A bracket is either of two tall fore- or back-facing punctuation marks commonly used to isolate a segment of text or data from its surroundings. Typically deployed in symmetric pairs, an individual bracket may be identified as a 'left' or 'r ...
s (i.e., ) or some other delimiter. While this allows for more formatting freedom – a developer might choose not to indent small pieces of code like the break and continue statements – sloppily indented code might lead the reader astray, such as the
goto fail In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program. Unreachable code is sometimes also called ''dead code' ...
bug.
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
and other
S-expression In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming la ...
-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 (i.e., the read function). Whitespace is used to separate tokens. The explicit structure of Lisp code allows automatic indenting, to form a visual cue for human readers. Another alternative is for each block to begin and end with explicit keywords. For example, in
ALGOL 60 ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
and its descendant
Pascal Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Frenc ...
, 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 BASIC (Beginners' All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use. The original version was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College ...
and Fortran, blocks begin with the block name (such as IF) and end with the block name prepended with END (e.g., END IF). In Fortran, each and every block can also have its own unique block name, which adds another level of explicitness to lengthy code.
ALGOL 68 ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously d ...
and the
Bourne shell The Bourne shell (sh) is a shell command-line interpreter for computer operating systems. The Bourne shell was the default shell for Version 7 Unix. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic l ...
(sh, and
bash Bash or BASH may refer to: Arts and entertainment * ''Bash!'' (Rockapella album), 1992 * ''Bash!'' (Dave Bailey album), 1961 * '' Bash: Latter-Day Plays'', a dramatic triptych * ''BASH!'' (role-playing game), a 2005 superhero game * "Bash" ('' ...
) are similar, but the end of the block is usually given by the name of the block written backward (e.g., case starts a
switch statement In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map. Switch statements function s ...
and it spans until the matching esac; similarly
conditionals Conditional (if then) may refer to: *Causal conditional, if X then Y, where X is a cause of Y *Conditional probability, the probability of an event A given that another event B has occurred *Conditional proof, in logic: a proof that asserts a co ...
if...then... code>elif...[else...fi_or_for_loop.html" ;"title="code>else....html" ;"title="code>elif...[else...">code>elif...[else...fi or for loop">''for'' loops for...do...od in ALGOL68 or for...do...done in bash). An interesting variant of this occurs in Modula-2, a Pascal-like language which does away with the difference between one and multiline blocks. This allows the block opener ( or END). It also fixes dangling else. Custom is for the end token to be placed on the same indent 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 provides no contextual cues as to which blocks are being closed, necessitating backtracking, and closer scrutiny while
debugging In computer programming and software development, debugging is the process of finding and resolving '' bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems. Debugging tactics can involve i ...
. Further, languages that allow a suffix for END-like keywords further improve such cues, such as continue versus continue for x. However, modern
source code editor A source-code editor is a text editor program designed specifically for editing source code of computer programs. It may be a standalone application or it may be built into an integrated development environment (IDE) or web browser. Source-code ed ...
s often provide visual indicators, such as
syntax highlighting Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms ...
, and features such as
code folding Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of tex ...
to assist with these drawbacks.


Productivity

In the language Scala, early versions allowed curly braces only. Scala 3 added an option to use indenting to structure blocks. Designer Martin Odersky said that this improves productivity overall by 10%, turns out to be the most productive change introduced in Scala 3, and advises its use.


Off-side rule languages


Programming languages

* ABC * Boo * BuddyScript * Cobra *
CoffeeScript CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehe ...
* Converge *
Curry A curry is a dish with a sauce seasoned with spices, mainly associated with South Asian cuisine. In southern India, leaves from the curry tree may be included. There are many varieties of curry. The choice of spices for each dish in trad ...
* Elm * F# (in early versions, when #light is specified; in later versions when #light "off" is not) *
Genie Jinn ( ar, , ') – also romanized as djinn or anglicized as genies (with the broader meaning of spirit or demon, depending on sources) – are invisible creatures in early pre-Islamic Arabian religious systems and later in Islamic myt ...
* GDScript (Godot Engine) *
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
(only for where, let, do, or case ... of clauses when braces are omitted) *
Inform 7 Inform is a programming language and design system for interactive fiction originally created in 1993 by Graham Nelson. Inform can generate programs designed for the Z-code or Glulx virtual machines. Versions 1 through 5 were released between ...
*
ISWIM ISWIM (acronym for If you See What I Mean) is an abstract computer programming language (or a family of languages) devised by Peter Landin and first described in his article "The Next 700 Programming Languages", published in the Communications ...
, the abstract language that introduced the rule * LiveScript * Miranda * MoonScript * Nemerle, optional mode * Nim * occam * PROMAL * Python * Scala, optional mode *
Scheme A scheme is a systematic plan for the implementation of a certain idea. Scheme or schemer may refer to: Arts and entertainment * ''The Scheme'' (TV series), a BBC Scotland documentary series * The Scheme (band), an English pop band * ''The Schem ...
, when using one of several
Scheme Requests for Implementation Scheme Requests for Implementation (SRFI) is an effort to coordinate libraries and extensions of standard Scheme programming language, necessitated by Scheme's minimalist design, and particularly the lack of a standard library before Scheme R6RS. SR ...
s, the latest of which i
SRFI 119
*
Spin Spin or spinning most often refers to: * Spinning (textiles), the creation of yarn or thread by twisting fibers together, traditionally by hand spinning * Spin, the rotation of an object around a central axis * Spin (propaganda), an intentionally ...
* Woma * XL


Other languages

*
Haml Haml (HTML Abstraction Markup Language) is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Haml gives the flexibility to have some dynamic content in HTML. Similar to other template s ...
* Make (build tool: tabs introduce commands in Make rules) *
reStructuredText reStructuredText (RST, ReST, or reST) is a file format for textual data used primarily in the Python programming language community for technical documentation. It is part of the Docutils project of the Python Doc-SIG (Documentation Special Inte ...
reStructuredText Markup Specification – Indentation
/ref> * Sass *
Stylus A stylus (plural styli or styluses) is a writing utensil or a small tool for some other form of marking or shaping, for example, in pottery. It can also be a computer accessory that is used to assist in navigating or providing more precision ...
*
YAML YAML ( and ) (''see '') is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Ext ...
* Pug (formerly Jade), see
Comparison of web template engines The following table lists the various web template engines used in Web template systems and a brief rundown of their features. See also * Template processor * Web template system * JavaScript templating * :Template engines Java template e ...


See also

* *
Prettyprint Pretty-printing (or prettyprinting) is the application of any of various stylistic formatting conventions to text files, such as source code, markup, and similar kinds of content. These formatting conventions may entail adhering to an indentatio ...


References

{{Types of programming languages Programming language topics Articles with example Python (programming language) code