Syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express.
≈ 5 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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.
Syntactic sugar is usually a shorthand for a common operation that could also be expressed in an alternate, more verbose, form: The programmer has a choice of whether to use the shorter form or the longer form, but will usually use the shorter form since it is shorter and easier to type and read. For example, in the Python programming language it is possible to get a list element at a given index using the syntax list_variable.__getitem__(index) , but this is frequently shortened to list_variable[index] which could be considered simpler and easier to read, despite having identical behavior.
Similarly, list_variable.__setitem__(index, value) is frequently shortened to list_variable[index] = 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 and expressive power will remain the same.
Language processors, including compilers and static analyzers, often expand sugared constructs into their more verbose equivalents before processing, a process sometimes called "desugaring". C++ and C from C23 onwards allow auto x = expr as a shorthand for decltype(expr) x = expr in C++ or typeof(expr) x = expr in C. Python list comprehensions (such as for a list of squares) and decorators (such as @staticmethod ).
In Haskell, a string, denoted in quotation marks, is semantically equivalent to a list of characters. An optional language extension OverloadedStrings allows string literals to produce other types of values, such as Text, as well.
In the tidyverse 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, one may omit the OUTER from the LEFT OUTER JOIN , RIGHT OUTER JOIN and FULL OUTER JOIN . Extension method 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.
Various languages offer import statements to allow adding symbols from another namespace into the current scope. In C++, a using statement is such an example for importing a single symbol into scope, while a using namespace namespace imports all symbols from that namespace into scope. In C#, a using statement adds all symbols from a namespace into scope.
In Java, an import is such an example. For example import javax.swing.*; allows the programmer to reference a Swing object such as javax.swing.JButton using just the name JButton . In Python, a from import statement imports a single symbol into scope, while a from import * statement imports all symbols from that namespace into scope.
In Rust, a use statement is used for importing symbols into scope. In JavaScript, if the key and value are the same in an object, you have the option to write it just once. For example, {name: name} is equivalent to {name} .
This is called the Shorthand Property. In the ES6 version of JavaScript, arrow functions have a short form (x) => x + 1 , which is equivalent to the longer form . In Scala, triple question marks ( ??? ) is equivalent to .
This is useful to mark a place for code that has not yet been written.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1378588718 under CC BY-SA 4.0; modifications have been made.