Syntactic sugar

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer science, 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 the construct.

Syntactic sugar 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.

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.

Contents

[edit] Examples

[edit] 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). Because of this, i[a] is the same as a[i] even if i is an integer.

[edit] 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]

[edit] Object-oriented programming

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.

[edit] 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 semicolon" (see Epigrams on Programming). The developers of the Linux kernel insist on doing object-oriented programming using nothing more than C.[2]

[edit] Alternative terms

[edit] 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 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.[citation needed]

[edit] Syntactic saccharin

Another extension is syntactic saccharin, meaning gratuitous syntax which does not actually make programming easier.

[edit] 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 was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.

Personal tools