HOME

TheInfoList



OR:

The off-side rule describes
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 ...
of a
computer A computer is a machine that can be Computer programming, programmed to automatically Execution (computing), carry out sequences of arithmetic or logical operations (''computation''). Modern digital electronic computers can perform generic set ...
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
that defines the bounds of a code block via indentation. The term was coined by Peter Landin, possibly as a pun on the offside law in
association football Association football, more commonly known as football or soccer, is a team sport played between two teams of 11 Football player, players who almost exclusively use their feet to propel a Ball (association football), ball around a rectangular f ...
. An off-side rule language is contrasted with a free-form language in which indentation has no syntactic meaning, and indentation is strictly a matter of
style Style, or styles may refer to: Film and television * ''Style'' (2001 film), a Hindi film starring Sharman Joshi, Riya Sen, Sahil Khan and Shilpi Mudgal * ''Style'' (2002 film), a Tamil drama film * ''Style'' (2004 film), a Burmese film * '' ...
. An off-side rule language is also described as having significant indentation.


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


Example

The following is an example of indentation blocks in Python; a popular off-side rule language. In Python, the rule is taken to define the boundaries of statements rather than declarations. def is_even(a: int) -> bool: if a % 2

0: print('Even!') return True print('Odd!') return False
The body of the function starts on line 2 since it is indented one level (4 spaces) more than the previous line. The if clause body starts on line 3 since it is indented an additional level, and ends on line 4 since line 5 is indented a level less, a.k.a. outdented. The colon (:) at the end of a control statement line is Python syntax; not an aspect of the off-side rule. The rule can be realized without such colon syntax.


Implementation

The off-side rule can be implemented in the
lexical analysis Lexical tokenization is conversion of a text into (semantically or syntactically) meaningful ''lexical tokens'' belonging to categories defined by a "lexer" program. In case of a natural language, those categories include nouns, verbs, adjectives ...
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, grammar is the set of rules for how a natural language is structured, as demonstrated by its speakers or writers. Grammar rules may concern the use of clauses, phrases, and words. The term may also refer to the study of such rul ...
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 characters and mark blocks explicitly with curly brackets (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 bug.
Lisp Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
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 (computing), list (Tree (data structure), tree-structured) data. S-expressions were invented ...
-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, 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 or BASIC may refer to: Science and technology * BASIC, a computer programming language * Basic (chemistry), having the properties of a base * Basic access authentication, in HTTP Entertainment * Basic (film), ''Basic'' (film), a 2003 film ...
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 member of the ALGOL family that was conceived as a successor to the ALGOL 60 language, designed with the goal of a much wider scope of application and ...
and the Bourne shell (sh, and 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 ...
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 *Conditional proof, in logic: a proof that asserts a conditional, a ...
if...then... 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 engineering, debugging is the process of finding the Root cause analysis, root cause, workarounds, and possible fixes for bug (engineering), bugs. For software, debugging tactics can involve interactive debugging, control flow analysis, Logf ...
. Further, languages that allow a suffix for END-like keywords further improve such cues, such as continue versus continue for x, and end-loop marker specifying the index variable NEXT I versus NEXT, and uniquely named loops CYCLE X1 versus CYCLE. However, modern source code editors often provide visual indicators, such as syntax highlighting, 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 text ...
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 was the single most important way Scala 3 improved his own productivity, that it makes programs over 10% shorter and keeps programmers "in the flow", and advises its use.


Notable programming languages

Notable programming languages with the off-side rule: * ABC * Agda * Boo *
Cobra COBRA or Cobra, often stylized as CoBrA, was a European avant-garde art group active from 1948 to 1951. The name was coined in 1948 by Christian Dotremont from the initials of the members' home countries' capital cities: Copenhagen (Co), Brussels ...
*
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. Some added features include list comprehension an ...
* Converge * Curry * Elm * F#, in early versions, when #light is specified; in later versions when #light "off" is not * 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 pioneered several programming language ...
, only for where, let, do, or case ... of clauses when braces are omitted * Inform 7 * ISWIM, the abstract language that introduced the rule * LiveScript * Lobster * Miranda * MoonScript * Nemerle, optional mode * Nim * occam * PROMAL * Python * Scala, optional mode * Scheme, when using one of several Scheme Requests for Implementations, the latest of which i
SRFI 119
* Spin * Woma * XL


Other file formats

Notable non-programming language, text file formats with significant indentation: * GCode, RepRapFirmware dialect * Haml * Make, tab-indented line signifies a command * Pug (formerly Jade), see Comparison of web template engines * reStructuredTextreStructuredText Markup Specification – Indentation
/ref> * Sass *
Stylus A stylus is a writing utensil or tool for scribing or marking into softer materials. Different styluses were used to write in cuneiform by pressing into wet clay, and to scribe or carve into a wax tablet. Very hard styluses are also used to En ...
* YAML


See also

* * Prettyprint


References

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