Syntactic sugar
This article needs additional citations for verification. (July 2007) |
In computer science, syntactic sugar in a language is syntax designed to make things easier to read or to express, while alternative ways of expressing them exist. It makes the language "sweeter" for humans to use: things can be expressed more clearly, or 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, in C#, the property construct may be called syntactic sugar: it is roughly, but not exactly equivalent to a getter-setter pair of functions. Even more broadly, programming languages have been called "machine code with a lot of syntactic sugar".
The term was coined by Peter J. Landin, when he was working on a lambda calculus enriched with a few operations, such as assignment. Following Landin's insights, some later programming languages, such as ML and Scheme, were explicitly designed as 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.
Examples
Variables
One example relating to variables is in the C programming language's handling of arrays. In C, arrays are constructed as blocks of memory, accessed via an offset from the array's starting point in memory. However, pointer arithmetic can often be difficult, error prone, and inelegant. Therefore, C provides the a[i]
syntax for what would otherwise be written as *(a + i)
. Similarly a[i][j]
is easier to understand than *(*(a + i) + j)
.
String literals
A common feature of many programming languages is the ability to specify string literals directly in source code. The conventions for this vary, and there are some languages whose conventions are more restrictive than others. This is one example where proposals to change syntax in order to "save typing" have been characterized as "syntactic sugar."[1]
Object-oriented programming
Object-oriented programming is possible in C by way of function pointers, type casting, and structures. However, languages such as C++ make object-oriented programming more convenient by introducing syntax specific to this coding style. The specialized syntax works to encourage the object-oriented approach especially for new programmers. Features of the C# programming language, such as properties and interfaces, similarly do not enable new functionality but instead make specific programming practices more prominent and more natural.
Criticism
Some programmers feel that these 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 semicolons" (see Epigrams on Programming). The developers of the Linux kernel insist upon using C as opposed to C++, citing the lack of evidence supporting the advantages. However, there are also practical reasons specific to the project.[2]
Alternative terms
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. Some programmers consider required type declarations to be syntactic salt. A requirement to write "end if", "end while", "end do", etc. to terminate the last block controlled by a control construct (as opposed to just "end" or even simpler syntax using braces "}") is widely considered syntactic salt.[citation needed]
Syntactic saccharin
Another extension is syntactic saccharin, meaning gratuitous syntax which does not actually make programming easier[citation needed].
Notes and references
- 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 is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.