get_array(Array, vector(i,j))
. Instead, many languages provide syntax such as Array ,j/code>. Similarly an array element update is a procedure consisting of three arguments, for example set_array(Array, vector(i,j), value)
, but many languages also provide syntax such as Array ,j= value
.
A construct in a language is syntactic sugar if it can be removed from the language without any effect on what the language can do: functionality
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-oriente ...
and expressive power will remain the same.
Language processors, including compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
s and static analyzers, often expand sugared constructs into their more verbose equivalents before processing, a process sometimes called "desugaring".
Origins
The term ''syntactic sugar'' was coined by Peter J. Landin
Peter John Landin (5 June 1930 – 3 June 2009) was a British computer scientist. He was one of the first to realise that the lambda calculus could be used to model a programming language, an insight that is essential to the development of bo ...
in 1964 to describe the surface syntax of a simple ALGOL
ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
-like programming language which was defined semantically in terms of the applicative expressions of lambda calculus
Lambda calculus (also written as ''λ''-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation ...
, centered on lexically replacing λ with "where".
Later programming languages, such as CLU, ML and Scheme A scheme is a systematic plan for the implementation of a certain idea.
Scheme or schemer may refer to:
Arts and entertainment
* ''The Scheme'' (TV series), a BBC Scotland documentary series
* The Scheme (band), an English pop band
* ''The Schem ...
, 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
Matthias Felleisen is a German-American computer science professor and author. He grew up in Germany and immigrated to the US when he was 21 years old.
He received his PhD from Indiana University under the direction of Daniel P. Friedman.
Afte ...
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.
Notable examples
* In COBOL
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily us ...
, 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 the action to be performed clearer.
* Augmented assignment
Augmented assignment (or compound assignment) is the name given to certain
assignment operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operat ...
or compound assignment operators: For example, a += b
is equivalent to a = a + b
in C and similar languages, assuming a
has no side effects such as if a
is a regular variable. Some languages, such as Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pro ...
may allow overloading augmented assignment operators, so they may behave differently than standard ones.
* In Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
, 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)
, but the former is more naturally formatted on a single line.
* In the C language
C (''pronounced like the letter c'') is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities o ...
, the a /code> notation is syntactic sugar for *(a + i)
. Likewise, the a->x
notation is syntactic sugar for accessing members using the dereference operator
In computer programming, the dereference operator or indirection operator, sometimes denoted by "*" (i.e. an asterisk), is a unary operator (i.e. one with a single operand) found in C-like languages that include pointer variables. It operates ...
(*a).x
.
* The using
statement in C# ensures that certain objects are disposed of correctly. The compiler expands the statement into a try-finally block.
* The C# language allows variables to be declared as var x = expr
, which allows the compiler to infer
Inferences are steps in reasoning, moving from premises to logical consequences; etymologically, the word '' infer'' means to "carry forward". Inference is theoretically traditionally divided into deduction and induction, a distinction that in ...
the type of x
from the expression expr
, instead of requiring an explicit type declaration. Similarly, C++ allows auto x = expr
since C++11 and Java allows var x = expr
since Java 11.
* Python list comprehensions
A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical ''set-builder notation'' (''set comprehension'') as distinct from the use of ...
(such as *x for x in range(10)/code> for a list of squares) and decorators (such as @staticmethod
).
* In Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
, a string, denoted in quotation marks, is semantically equivalent to a list of characters.
* In the tidyverse
The tidyverse is a collection of open source packages for the R programming language introduced by Hadley Wickham and his team that "share an underlying design philosophy, grammar, and data structures" of tidy data. Characteristic features of t ...
collection of R packages, the ''pipe'', denoted by %>%
, declares that the data (or output of the function) preceding the pipe will serve as the first argument for the function following the pipe. So, x %>% f(y)
is equivalent to f(x,y)
.
* In SQL, a mere JOIN
is equivalent to an INNER JOIN
, the latter clarifying that the join statement is specifically an inner join operation as opposed to an outer join operation. Likewise, you may omit the OUTER
from the LEFT OUTER JOIN
, RIGHT OUTER JOIN
and FULL OUTER JOIN
.
*Method calling in OOP languages in the form of myObject.myMethod(parameter1, parameter2, parameter3)
is syntactic sugar for calling a global function as myMethod(myObject, parameter1, parameter2, parameter3)
. The reference to the object is passed as a hidden argument, usually accessible from within the method as this
.
* A parameter called by reference is syntactic sugar for technically passing a ''pointer'' as the parameter, but syntactically handling it as the variable itself, to avoid constant pointer de-referencing in the code inside the function.
* In Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
, an import
declaration enables the compiler to find classes that are not otherwise specified with fully qualified names. For example import javax.swing.*;
allows the programmer to reference a Swing object such as javax.swing.JButton
using the shorter name JButton
.
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
A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech.
Types
* A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
community, as Lisp has very simple and regular syntax, and the surface syntax can easily be modified.
For example, Alan Perlis
Alan Jay Perlis (April 1, 1922 – February 7, 1990) was an American computer scientist and professor at Purdue University, Carnegie Mellon University and Yale University. He is best known for his pioneering work in programming languages and was t ...
once quipped in " Epigrams on Programming", in a reference to bracket-delimited languages, that "Syntactic sugar causes cancer of the semi-colons".
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 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
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and Pascal
Pascal, Pascal's or PASCAL may refer to:
People and fictional characters
* Pascal (given name), including a list of people with the name
* Pascal (surname), including a list of people and fictional characters with the name
** Blaise Pascal, Fren ...
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++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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. To avoid potential bugs owing to the similarity of the switch statement
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Switch statements function some ...
syntax with that of C or C++, C# requires a break
for each non-empty case
label of a switch
(unless goto
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function ca ...
, return
, or throw
is used) even though it does not allow implicit ''fall-through''. (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
Saccharin (''aka'' saccharine, Sodium sacchari) is an artificial sweetener with effectively no nutritional value. It is about 550 times as sweet as sucrose but has a bitter or metallic aftertaste, especially at high concentrations. Saccharin is u ...
'' and ''syntactic syrup
In cooking, a syrup (less commonly sirup; from ar, شراب; , beverage, wine and la, sirupus) is a condiment that is a thick, viscous liquid consisting primarily of a solution of sugar in water, containing a large amount of dissolved sugars ...
'', meaning gratuitous syntax that does not make programming any easier.
Sugared types
Data types with core syntactic support are said to be "sugared types". Common examples include quote-delimited strings, curly braces for object and record types, and square brackets for arrays.
Notes
References
*
*
*
*
* , reprinted in
*
{{refend
Programming language syntax
Computer jargon
Source code
Programming language design