Jump to content

Syntactic sugar

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Some Gadget Geek (talk | contribs) at 19:21, 20 April 2016 (References: clean up using AWB). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 like Array[i,j]. Similarly an array element update is a procedure of three arguments, something like set_array(Array, vector(i,j), value), but many languages provide syntax like Array[i,j] = 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.

Language processors, including compilers, static analyzers, and the like, often expand sugared constructs into more fundamental constructs 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,[1][2] centered around lexically replacing λ with "where".

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.[3]

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.[4] This is, in fact, the usual mathematical practice of building up from primitives.

Notable examples

  • In COBOL, many of the intermediate keywords are syntactic sugar that may optionally be omitted. For example, the sentence MOVE A B. and the sentence MOVE A TO B. perform exactly the same function, but the second makes what action is going to be performed somewhat clearer.
  • Augmented assignment or compound assignment operators: For example, a += b is equivalent to a = a + b in C and similar languages.
  • In Perl, unless (condition) {...} is syntactic sugar for if (not condition) {...}. Additionally, any statement can be followed by a condition, so statement if condition is equivalent to if (condition) {statement}, but the former is more naturally formatted on a single line.
  • In the C language, the a[i] notation is syntactic sugar for *(a + i).[5] Likewise, the a->x notation is syntactic sugar for (*a).x.[6]
  • The using statement in C# ensures that certain objects are disposed of correctly. The compiler expands the statement into a try-finally block.[7]
  • Python list comprehensions and decorators.

Criticism

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.[2] 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".[8]

Derivative 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. For example, Java will not allow a float value to be assigned to a variable declared as an int without additional syntax explicitly stating that intention, while C and C++ will automatically truncate any floats assigned to an int. However this is not syntax, but semantics.

In C#, when hiding an inherited class member, a compiler warning is issued unless the new keyword is used to specify that the hiding is intentional.[9] To avoid potential bugs owing to the similarity of the switch statement syntax with that of C or C++, C# requires a break for each non-empty case label of a switch (unless goto, return, or throw is used) even though it does not allow implicit fall-through.[10] (Using goto and specifying the subsequent label produces a C/C++-like fall-through.)

Syntactic salt may defeat its purpose by making the code unreadable and thus worsen its quality – in extreme cases, the essential part of the code may be shorter than the overhead introduced to satisfy language requirements. An alternative to syntactic salt is generating compiler warnings when there is high probability that the code is a result of a mistake – a practice common in modern C/C++ compilers.

Syntactic saccharin

Other extensions are syntactic saccharin and syntactic syrup, meaning gratuitous syntax that does not make programming any easier.[11][12]

Notes

  1. ^ Landin, Peter J. (1964). "The mechanical evaluation of expressions" (PDF). 6 (4). Computer Journal: 308–320. doi:10.1093/comjnl/6.4.308. Retrieved 21 July 2014. {{cite journal}}: Cite journal requires |journal= (help)
  2. ^ a b Abelson & Sussman 1996, Chapter 1, footnote 11.
  3. ^ Felleisen, Matthias (December 1991). "On the Expressive Power of Programming Languages". Science of Computer Programming. 17 (1–3). Springer-Verlag: 35–75. doi:10.1016/0167-6423(91)90036-W. Retrieved 19 July 2014.
  4. ^ Barbara Liskov, "A History of CLU", MIT Laboratory for Computer Science Technical Report 561 (1993)
  5. ^ Eric S. Raymond (11 October 1996). The New Hacker's Dictionary - 3rd Edition. MIT Press. p. 432. ISBN 978-0-262-68092-9. Retrieved 5 August 2012.
  6. ^ http://fredosaurus.com/notes-cpp/structs/arrow.html Arrow operator – Fred Swartz
  7. ^ "using Statement (C# Reference)". Retrieved 16 September 2014.
  8. ^ Perlis 1982, Epigram #3.
  9. ^ "new Modifier (C# Reference)". microsoft.com. Microsoft. Retrieved 3 August 2015.
  10. ^ "switch (C# Reference)". microsoft.com. Microsoft. Retrieved 3 August 2015.
  11. ^ "syntactic sugar". catb.org. Retrieved 3 August 2015.
  12. ^ "Mathematics of Program Construction". google.com. Retrieved 3 August 2015.

References

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.