Syntactic sugar

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express.

It makes the language "sweeter" for humans to use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

Specifically, a construct in a language is called syntactic sugar if it can be removed from the language without any effect on what the language can do: functionality and expressive power will remain the same. All applications of the construct can be systematically replaced with equivalents that do not use it. For instance, in imperative programming languages, for loops can be systematically replaced with while loops, which in turn can be systematically replaced with gotos.

More generally, the term is used to characterize syntax as being designed for ease of expression, for instance list comprehension in Python. In C#, the property construct may be called syntactic sugar: it is roughly, but not exactly equivalent to a getter-setter pair of functions.

Language processors, including compilers, static analyzers, and the like, often expand sugared constructs into more fundamental constructs before processing, a process sometimes called "desugaring".

Contents

[edit] Origins

The term syntactic sugar was coined by Peter J. Landin in 1964 to describe the surface syntax of A Programming Language (APL) which was defined semantically in terms of the applicative expressions of lambda calculus.[1]

Later programming languages, such as ML and Scheme, extended the term to refer to syntax within a language which could be defined in terms of a language core of essential constructs; the convenient, higher-level features could be "desugared" and decomposed into that subset.[citation needed] This is, in fact, the usual mathematical practice of building up from primitives.

[edit] Criticism

Some programmers feel that these syntax usability features are either unimportant or outright frivolous. For example, Alan Perlis once quipped, in a reference to bracket-delimited languages, that "syntactic sugar causes cancer of the semicolon". (See Epigrams on Programming.)

[edit] Derivative terms

[edit] Syntactic salt

The metaphor has been extended by coining the term syntactic salt, which indicates a feature designed to make it harder to write bad code. Specifically, syntactic salt is a hoop programmers must jump through just to prove that they know what's going on, rather than to express a program action. For example, Java will not allow you to declare a variable as an int and then assign it a float or double value, while C and C++ will automatically truncate any floats assigned to an int.

[edit] Syntactic saccharin

Another extension is syntactic saccharin, meaning gratuitous syntax that does not actually make programming easier.[2]

[edit] Notes and references

  1. ^ Peter J. Landin, "The mechanical evaluation of expressions", Computer Journal 6:4: 308-320 (1964) doi:10.1093/comjnl/6.4.308
  2. ^ The Jargon File v4.4.7: "syntactic sugar"
  • Landin, Peter J.; A Correspondence Between ALGOL 60 and Church's Lambda-Notation: Parts I and II, Communications of the ACM, 8(2.3):89-101, 158-165 (February and March 1965)
  • Landin, Peter J.; Programming Without Imperatives - An Example, Technical report, UNIVAC Systems Programming Research, March 1965
  • Landin, Peter J.; Getting Rid of Labels, Technical report, UNIVAC Systems Programming Research, July 1965
  • Landin, Peter J.; A Generalization of Jumps and Labels, Report, UNIVAC Systems Programming Research, August 1965, reprinted in Higher-Order and Symbolic Computation, 11, pp. 125–143 (1998)

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

Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export
Languages