Off-side rule

Off-side rule

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

Contents

Definition

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."[1]

Code examples

The following is an example of indentation blocks in Python (note also the colons — they are obligatory and not stylistic in Python):[2]

 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, 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 convention of starting blocks with keyword begin and ending them with end. In BASIC and FORTRAN, 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 (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 esac).

Off-side rule languages

References

This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.

  1. ^ 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. http://web.archive.org/web/20070417160748/http://www.cs.utah.edu/~wilson/compilers/old/papers/p157-landin.pdf. 
  2. ^ Python FAQ on colons
  3. ^ The Haskell Report - Layout

Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Rule of 72 — In finance, the rule of 72, the rule of 70 and the rule of 69 are methods for estimating an investment s doubling time. The number in the title is divided by the interest percentage per period to get the approximate number of periods needed for… …   Wikipedia

  • Rule of Rose — Infobox VG title = Rule of Rose developer = Punchline publisher = Sony Computer Entertainment(JP) Atlus (US) 505 GameStreet (EUR) distributor = Ingram Entertainment (EU) designer = engine = version = released = JPN January 19, 2006 NA September… …   Wikipedia

  • Off hand — Hand Hand (h[a^]nd), n. [AS. hand, hond; akin to D., G., & Sw. hand, OHG. hant, Dan. haand, Icel. h[ o]nd, Goth. handus, and perh. to Goth. hin[thorn]an to seize (in comp.). Cf. {Hunt}.] 1. That part of the fore limb below the forearm or wrist in …   The Collaborative International Dictionary of English

  • Off one's hands — Hand Hand (h[a^]nd), n. [AS. hand, hond; akin to D., G., & Sw. hand, OHG. hant, Dan. haand, Icel. h[ o]nd, Goth. handus, and perh. to Goth. hin[thorn]an to seize (in comp.). Cf. {Hunt}.] 1. That part of the fore limb below the forearm or wrist in …   The Collaborative International Dictionary of English

  • Slide rule — For other uses, see Slide rule (disambiguation). A typical ten inch student slide rule (Pickett N902 T simplex trig). The slide rule, also known colloquially as a slipstick,[1] is a mechanical analog computer. The slide rule is used primarily for …   Wikipedia

  • Company rule in India — For usage, see British Empire in India Company rule in India Colony of the East India Company ↓ …   Wikipedia

  • Tarrasch rule — The Tarrasch rule is a general principle that applies in the majority of chess middlegames and endgames. Siegbert Tarrasch stated the rule that rooks should be placed behind passed pawns – either yours or your opponent s. The idea behind the rule …   Wikipedia

  • Away goals rule — The away goals rule is a method of breaking ties in association football and other sports when teams play each other twice, once at each team s home ground. By the away goals rule, the team that has scored more goals (points, baskets) away from… …   Wikipedia

  • Face-off — For the 1997 film, see Face/Off. For other uses, see Face Off …   Wikipedia

  • 180 degree rule — The 180° rule is a basic film editing guideline that states that two characters (or other elements) in the same scene should always have the same left/right relationship to each other. If the camera passes over the imaginary axis connecting the… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”