Lably
Skip to content

Syntactic sugar

Programming language syntax designed for ease of use

  • 6 min read
  • 4 chapters

Related on Lably

Through Computer science, Syntax (programming languages) and Python (programming language)

For example, in the Python programming language it is possible to get a list element at a given index using the syntax list_variable.__getitem__(index), but this is frequently shortened to list_variable[index] which could be considered simpler and easier to read, despite having identical behavior. Similarly, list_variable.__setitem__(index, value) is frequently shortened to list_variable[index] = value.

A construct in a language is 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.

Language processors, including compilers and static analyzers, often expand sugared constructs into their more verbose equivalents before processing, a process sometimes called "desugaring".

Origins

The term syntactic sugar was coined by Peter J. Landin in 1964 to describe the surface syntax of a simple ALGOL-like programming language which was defined semantically in terms of the applicative expressions of lambda calculus, centered on lexically replacing λ with "where".

Later programming languages, such as CLU, 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. This is, in fact, the usual mathematical practice of building up from primitives.

Building on Landin's distinction between essential language constructs and syntactic sugar, in 1991, Matthias Felleisen proposed a codification of "expressive power" to align with "widely held beliefs" in the literature. He defined "more expressive" to mean that without the language constructs in question, a program would have to be completely reorganized.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication