Off-side rule
Encyclopedia
A computer programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 is said to adhere to the off-side rule if the scope of declarations (a block) in that language is expressed by their indentation
Indent style
In computer programming, an indent style is a convention governing the indentation of blocks of code to convey the program's structure. This article largely addresses the C programming language and its descendants, but can be applied to most other programming languages...

. The term and the idea are attributed to Peter J. Landin
Peter J. Landin
Peter John Landin was a British computer scientist. He was one of the first to realize that the lambda calculus could be used to model a programming language, an insight that is essential to development of both functional programming and denotational semantics.- Academic :Landin was born in...

, and the term can be seen as a pun on the offside
Offside (football)
Offside is a law in football which states that if a player is in an offside position when the ball is touched or played by a teammate, he may not become actively involved in the play...

 law of football (soccer).

Definition

Peter J. Landin
Peter J. Landin
Peter John Landin was a British computer scientist. He was one of the first to realize that the lambda calculus could be used to model a programming language, an insight that is essential to development of both functional programming and denotational semantics.- Academic :Landin was born in...

, 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
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 (note also the colons — they are obligatory and not stylistic in Python):

def is_even(a):
if a % 2

0:
print('Even!')
return True
else:
print('Odd!')
return False

Alternatives
The primary alternative to delimiting blocks, popularized by C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, is to ignore whitespace and mark blocks explicitly with curly brackets (i.e. { and }) or some other delimiter. The main advantage of this is that it makes it possible for tools to correct the indentation automatically. A disadvantage is that if such a tool is not used, it is possible to have a program that works correctly but whose indentation leads a 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. Often, this means that newlines are important (unlike in curly brace languages), but the indentation is not. Examples of this rule are the Pascal
Pascal (programming language)
Pascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...

 convention of starting blocks with keyword begin and ending them with end. In BASIC
BASIC
BASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use - the name is an acronym from Beginner's All-purpose Symbolic Instruction Code....

 and FORTRAN
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

, blocks begin with the block name (such as IF) and end with the block name prepended with END (e.g. END IF). The Bourne shell
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...

 (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
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...

 and it spans until the matching esac).
Off-side rule languages
  • ABC
  • Boo
  • BuddyScript
    BuddyScript
    BuddyScript is a domain-specific language originally developed by ActiveBuddy. The main purpose of the language is to be able to process natural language queries and return results in natural language form....

  • Cobra
    Cobra (programming language from Cobra Language LLC)
    Cobra is an object-oriented programming language produced by Cobra Language LLC. Cobra is designed by Chuck Esterbrook, and runs on the Microsoft .NET and Mono platforms. It is strongly influenced by Python, C#, Eiffel, Objective-C, and other programming languages. It supports both static and...

  • CoffeeScript
    CoffeeScript
    CoffeeScript is a programming language that transcompiles to JavaScript. The language adds syntactic sugar inspired by Ruby, Python and Haskell to enhance JavaScript's brevity and readability, as well as adding more sophisticated features like array comprehension and pattern matching...

  • Curry
  • F# (if #light "off" is not specified)
  • Genie
    Genie (programming language)
    Genie is a modern, general-purpose high-level programming language in active development since 2008. It was designed as an alternative, simpler and cleaner dialect for the Vala compiler, while preserving the same functionality of the Vala language. Genie uses the same compiler and libraries as...

  • Haml
    Haml
    Haml is a lightweight markup language that is used to describe the XHTML of any web document without the use of traditional inline coding. It’s designed to address many of the flaws in traditional templating engines, as well as making markup as elegant as it can be...

  • Haskell
    Haskell (programming language)
    Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

     (only for where, let, do, or of clauses when braces are omitted)
  • ISWIM
    ISWIM
    ISWIM is an abstract computer programming language devised by Peter J. Landin and first described in his article, The Next 700 Programming Languages, published in the Communications of the ACM in 1966...

    , the abstract language that introduced the rule
  • Miranda
  • Nemerle
  • Occam
    Occam (programming language)
    occam is a concurrent programming language that builds on the Communicating Sequential Processes process algebra, and shares many of its features. It is named after William of Ockham of Occam's Razor fame....

  • PROMAL
    PROMAL
    PROMAL is a C-like programming language from Systems Management Associates for MS-DOS, Commodore 64, and Apple II. PROMAL featured simple syntax, no line numbers, long variable names, functions and procedures with argument passing, real number type, arrays, strings, pointer, and a built-in I/O...

  • Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

  • Spin
  • XL
  • YAML
    YAML
    YAML is a human-readable data serialization format that takes concepts from programming languages such as C, Perl, and Python, and ideas from XML and the data format of electronic mail . YAML was first proposed by Clark Evans in 2001, who designed it together with Ingy döt Net and Oren Ben-Kiki...

  • Nimrod
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK