Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its functionality but make it "sweeter" for humans to use. Syntactic sugar gives the programmer an alternative way of coding that is often more practical, more conducive to a better programming style, or more natural to read. However, it does not typically affect the expressiveness of the formalism or permit the language to do something new.
Syntactic sugar can often be easily translated ("desugared") to produce a program in some simpler "core" syntax. In Landin's case, the core was 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. However, many modern, "sugar-rich" languages (such as C#) cannot be desugared. Still, their features may be considered "sugar" because primitives exist in predecessor languages (such as C) to fully recreate them.
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
syntax for what would otherwise be written as
*(a + i)
is easier to understand than
*(*(a + i) + j)
. Because of this,
is the same as
is an integer.
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.
The C programming language is fully capable of object-oriented programming
using its facilities 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. Moreover, the specialized syntax works to emphasize the object-oriented approach to 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.
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 semicolon
" (see Epigrams on Programming
). The developers of the Linux kernel insist on doing object-oriented programming using nothing more than C.
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 the programmer must jump through just to prove that he or she knows 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.
Another extension is syntactic saccharin
, meaning gratuitous syntax which does not actually make programming easier.
Notes and references