In computer science, syntactic sugar is 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.
For example, many programming languages provide special syntax for referencing and updating array elements. Abstractly, an array reference is simply a procedure of two arguments: an array and a subscript vector, which could be expressed as get_array(Array,vector(i,j))
; but many languages provide special syntax like Array
; similarly an array element update is abstractly something like set_array(Array,vector(i,j),value)
, but many languages provide syntax like Array := value
.
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. For instance, in the C language the a
notation is syntactic sugar for *(a+i)
.
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".
Famous quotes containing the words syntactic and/or sugar:
“The syntactic component of a grammar must specify, for each sentence, a deep structure that determines its semantic interpretation and a surface structure that determines its phonetic interpretation.”
—Noam Chomsky (b. 1928)
“There is no sugar cane that is sweet at both ends.”
—Chinese proverb.