Syntactic sugar
Encyclopedia
Syntactic sugar is a computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

 term that refers to syntax
Syntax
In linguistics, syntax is the study of the principles and rules for constructing phrases and sentences in natural languages....

 within a programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 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.

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
Function (engineering)
In engineering, a function is interpreted as a specific process, action or task that a system is able to perform .-In engineering design:In the lifecycle of engineering projects, there are usually distinguished subsequently: Requirements and Functional specification documents. The Requirements...

 and expressive power
Expressive power
In computer science, the expressive power of a language describes the ideas expressible in that language.For example, the Web Ontology Language expression language profile lacks ideas which can be expressed in OWL2 RL . OWL2 EL may therefore be said to have less expressive power than OWL2 RL...

 will remain the same. All applications of the construct can be systematically replaced with equivalents that do not use it. For instance, in imperative programming languages, for loop
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....

s can be systematically replaced with while loop
While loop
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement....

s, which in turn can be systematically replaced with goto
Goto
goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control...

s.

More generally, the term is used to characterize syntax as being designed for ease of expression. For instance, in C#, the property construct may be called syntactic sugar: it is roughly, but not exactly equivalent to a getter-setter pair of functions.

Origins

The term syntactic sugar was coined by Peter J. Landin
Peter J. Landin
Peter John Landin was a British computer scientist. He was one of the first to realize that the lambda calculus could be used to model a programming language, an insight that is essential to development of both functional programming and denotational semantics.- Academic :Landin was born in...

 in 1964 to describe the surface syntax of a programming language which was defined semantically in terms of the applicative expressions of lambda calculus
Lambda calculus
In mathematical logic and computer science, lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion. The portion of lambda calculus relevant to computation is now called the untyped lambda calculus...

.

Later programming languages, such as ML
ML programming language
ML is a general-purpose functional programming language developed by Robin Milner and others in the early 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM...

 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.

Criticism

Some programmers feel that these syntax usability features are either unimportant or outright frivolous. For example, Alan Perlis
Alan Perlis
Alan Jay Perlis was an American computer scientist known for his pioneering work in programming languages and the first recipient of the Turing Award.-Biography:...

 once quipped, in a reference to bracket-delimited languages, that "syntactic sugar causes cancer of the semicolon
Semicolon
The semicolon is a punctuation mark with several uses. The Italian printer Aldus Manutius the Elder established the practice of using the semicolon to separate words of opposed meaning and to indicate interdependent statements. "The first printed semicolon was the work of ... Aldus Manutius"...

". (See Epigrams on Programming
Epigrams on Programming
Epigrams on Programming is an article by Alan Perlis published in 1982, for ACM's SIGPLAN journal. They are a series of short, programming language neutral, humorous statements about computers and programming, which are widely quoted....

.)

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
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 will not allow you to declare a variable as an int and then assign it a float or double value, while C and C++ will automatically truncate any floats assigned to an int.

Syntactic saccharin

Another extension is syntactic saccharin
Saccharin
Saccharin is an artificial sweetener. The basic substance, benzoic sulfilimine, has effectively no food energy and is much sweeter than sucrose, but has a bitter or metallic aftertaste, especially at high concentrations...

, meaning gratuitous syntax that does not actually make programming easier.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK