All Topics  
Syntactic sugar

 

   Email Print
   Bookmark   Link






 

Syntactic sugar



 
 
Syntactic sugar is a term coined by Peter J. Landin
Peter J. Landin

Peter Landin is a United Kingdom 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....
 for additions to the syntax
Syntax

In linguistics, syntax is the study of the principles and rules for constructing Sentence s in natural languages. In addition to referring to the discipline, the term syntax is also used to refer directly to the rules and principles that govern the sentence structure of any individual language, as in "the Irish syntax"....
 of a computer language that do not affect its functionality
Function (engineering)

In engineering, a function is an action or task that a system is designed to perform....
 but make it "sweeter" for humans to use. Syntactic sugar gives the programmer
Programmer

A programmer is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software....
 an alternative way of coding that is often more practical, more conducive to a better programming style, or more natural to read. However, it does not typically affect the expressiveness of the formalism or permit the language to do something new.

Syntactic sugar can often be easily translated ("desugared") to produce a program in some simpler "core" syntax.






Discussion
Ask a question about 'Syntactic sugar'
Start a new discussion about 'Syntactic sugar'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Syntactic sugar is a term coined by Peter J. Landin
Peter J. Landin

Peter Landin is a United Kingdom 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....
 for additions to the syntax
Syntax

In linguistics, syntax is the study of the principles and rules for constructing Sentence s in natural languages. In addition to referring to the discipline, the term syntax is also used to refer directly to the rules and principles that govern the sentence structure of any individual language, as in "the Irish syntax"....
 of a computer language that do not affect its functionality
Function (engineering)

In engineering, a function is an action or task that a system is designed to perform....
 but make it "sweeter" for humans to use. Syntactic sugar gives the programmer
Programmer

A programmer is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software....
 an alternative way of coding that is often more practical, more conducive to a better programming style, or more natural to read. However, it does not typically affect the expressiveness of the formalism or permit the language to do something new.

Syntactic sugar can often be easily translated ("desugared") to produce a program in some simpler "core" syntax. In Landin's case, the core was a lambda calculus
Lambda calculus

In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion....
 enriched with a few operations, such as assignment. Following Landin's insights, some 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 late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM....
 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. However, many modern, "sugar-rich" languages (such as C#) cannot be desugared. Still, their features may be considered "sugar" because primitives exist in predecessor languages (such as C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
) to fully recreate them.

Examples


Variables

One example relating to variables is in the C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 programming language's handling of array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
s. 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.

String literals

A common feature of many programming languages is the ability to specify string literal
String literal

A string literal is the representation of a String value within the source code of a computer program. There are numerous alternate notations for specifying string literals, and the exact notation depends on the individual programming language in question....
s 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."

Object-oriented programming

The C programming language is fully capable of object-oriented programming
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 using its facilities of function pointers, type casting, and structures. However, languages such as C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 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.

Criticism

Some programmers feel that these features are either unimportant or outright frivolous. For example, Alan Perlis
Alan Perlis

Alan Jay Perlis was an United States computer scientist known for his pioneering work in programming languages and the first recipient of the Turing Award....
 once quipped, in a reference to bracket-delimited languages
Curly bracket programming language

Curly brace or bracket programming languages are those which use balanced brackets to make block s in their syntax or formal grammar, mainly due to being C -influenced....
 that "syntactic sugar causes cancer of the semicolon
Semicolon

A semicolon is a conventional punctuation mark with several uses, mainly for pauses in sentences. The Italy printer Aldus Manutius the Elder established the practice of using the semicolon mark to separate words of opposed meaning, and to indicate interdependent statements....
" (see Epigrams on Programming
Epigrams on Programming

Epigrams on Programming is an article by Alan Perlis published in 1982, for Association for Computing Machinery SIGPLAN journal. They are a series of short, programming language neutral, humorous statements about computers and programming, which are widely quoted....
). The developers of the Linux kernel insist on doing object-oriented programming using nothing more than C.

Alternative 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 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.

Syntactic saccharin
Another extension is syntactic saccharin
Saccharin

Saccharin is an artificial sweetener. The basic substance, benzoic sulfinide, has effectively no food energy and is much sweeter than sucrose, but has an unpleasant bitter or metallic aftertaste, especially at high concentrations....
, meaning gratuitous syntax which does not actually make programming easier.