language-icon Old Web
English
Sign In

Syntactic sugar

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 human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer. 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 human 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 a procedure of two arguments: an array and a subscript vector, which could be expressed as get_array(Array, vector(i,j)). Instead, many languages provide syntax such as Array. Similarly an array element update is a procedure of three arguments, for example set_array(Array, vector(i,j), value), but many languages provide syntax such as Array = value. 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. Language processors, including compilers and static analyzers, often expand sugared constructs into more fundamental constructs before processing, a process sometimes called 'desugaring'. 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. Some programmers feel that these syntax usability features are either unimportant or outright frivolous. Notably, special syntactic forms make a language less uniform and its specification more complex, and may cause problems as programs become large and complex. This view is particularly widespread in the Lisp community, as Lisp has very simple and regular syntax, and the surface syntax can easily be modified.For example, Alan Perlis once quipped in 'Epigrams on Programming', in a reference to bracket-delimited languages, that 'Syntactic sugar causes cancer of the semi-colons'. 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 that programmers must jump through just to prove that they know what is going on, rather than to express a program action. For example, in Java and Pascal assigning a float value to a variable declared as an int without additional syntax explicitly stating that intention will result in a compile error, while C and C++ will automatically truncate any floats assigned to an int. However this is not syntax, but semantics.

[ "Syntax", "Semantics", "Programming language" ]
Parent Topic
Child Topic
    No Parent Topic