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, many programming languages provide special syntax for referencing and updating array elements. Abstractly, an array reference is a procedure of two arguments: an array and a subscript vector, which could be expressed as get_array(Array, vector(i,j))
. Instead, many languages provide syntax such as Array[i,j]
. 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[i,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 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".
The term syntactic sugar was coined by Peter J. Landin in 1964 to describe the surface syntax of a simple ALGOL-like programming language which was defined semantically in terms of the applicative expressions of lambda calculus, [1] [2] centered on lexically replacing λ with "where".
Later programming languages, such as CLU, ML 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. [3] 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 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. [4]
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.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. [5] [6] Some languages, such as Python [7] may allow overloading augmented assignment operators, so they may behave differently than standard ones.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) {statement}
, but the former is more naturally formatted on a single line.a[i]
notation is syntactic sugar for *(a + i)
. [8] Likewise, the a->x
notation is syntactic sugar for accessing members using the dereference operator (*a).x
.using
statement in C# ensures that certain objects are disposed of correctly. The compiler expands the statement into a try-finally block. [9] var x = expr
, which allows the compiler to infer 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.[x*x for x in range(10)]
for a list of squares) and decorators (such as @staticmethod
).%>%
, declares that the data (or output of the function) preceding the pipe will serve as the first argument for the function following the pipe. [10] So, x %>% f(y)
is equivalent to f(x,y)
.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
.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
.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
.(x) => x + 1
, which is equivalent to the longer form (x) => { return x + 1; }
. ???
) is equivalent to throw new NotImplementedError
. This is useful to mark a place for code that has not yet been written. [11] {name: name}
is equivalent to {name}
. This is called the Shorthand Property.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 community, as Lisp has very simple and regular syntax, and the surface syntax can easily be modified. [12] For example, Alan Perlis once quipped in "Epigrams on Programming", in a reference to bracket-delimited languages, that "Syntactic sugar causes cancer of the semi-colons". [13]
The metaphor has been extended by coining the term syntactic salt, which indicates a feature designed to make it harder to write bad code. [14] 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.
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. [15] To avoid potential bugs owing to the similarity of the switch statement syntax with that of C or C++, C# requires a break
for each non-empty case
label of a switch
(unless goto
, return
, or throw
is used) even though it does not allow implicit fall-through. [16] (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.
Other extensions are syntactic saccharin and syntactic syrup , meaning gratuitous syntax that does not make programming any easier. [17] [18] [19] [20]
Data types with core syntactic support are said to be "sugared types". [21] [22] [23] Common examples include quote-delimited strings, curly braces for object and record types, and square brackets for arrays.
However, the compound-assignment expression is not equivalent to the expanded version because the compound-assignment expression evaluates expression1 only once, while the expanded version evaluates expression1 twice: in the addition operation and in the assignment operation.
optimization can [be done] if 'finding x' has no side effects
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values, rather than a sequence of imperative statements which update the running state of the program.
Scheme is a dialect of the Lisp family of programming languages. Scheme was created during the 1970s at the MIT Computer Science and Artificial Intelligence Laboratory and released by its developers, Guy L. Steele and Gerald Jay Sussman, via a series of memos now known as the Lambda Papers. It was the first dialect of Lisp to choose lexical scope and the first to require implementations to perform tail-call optimization, giving stronger support for functional programming and associated techniques such as recursive algorithms. It was also one of the first programming languages to support first-class continuations. It had a significant influence on the effort that led to the development of Common Lisp.
Lua is a lightweight, high-level, multi-paradigm programming language designed mainly for embedded use in applications. Lua is cross-platform software, since the interpreter of compiled bytecode is written in ANSI C, and Lua has a relatively simple C application programming interface (API) to embed it into applications.
In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.
In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement is a fundamental construct.
Oberon-2 is an extension of the original Oberon programming language that adds limited reflective programming (reflection) and object-oriented programming facilities, open arrays as pointer base types, read-only field export, and reintroduces the FOR
loop from Modula-2.
In computer science, conditionals are programming language constructs that perform different computations or actions or return different values depending on the value of a Boolean expression, called a condition.
In computer science, a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for-loop functions by running a section of code repeatedly until a certain condition has been satisfied.
In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, conditional expression, ternary if, or inline if. An expression if a then b else c
or a ? b : c
evaluates to b
if the value of a
is true, and otherwise to c
. One can read it aloud as "if a then b otherwise c". The form a ? b : c
is the most common, but alternative syntax do exist; for example, Raku uses the syntax a ?? b !! c
to avoid confusion with the infix operators ?
and !
, whereas in Visual Basic .NET, it instead takes the form If(a, b, c)
.
This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries.
In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality and inequalities.
C# is a general-purpose high-level programming language supporting multiple paradigms. C# encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
In computer science, the syntax of a computer language is the rules that define the combinations of symbols that are considered to be correctly structured statements or expressions in that language. This applies both to programming languages, where the document represents source code, and to markup languages, where the document represents data.
this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity which the currently running code is a part of. The entity referred to thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, these keywords can disambiguate variables and functions with the same name.
C# and Visual Basic (.NET) are the two main programming languages used to program on the .NET framework.
This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.
The programming language C# version 3.0 was released on 19 November 2007 as part of .NET Framework 3.5. It includes new features inspired by functional programming languages such as Haskell and ML, and is driven largely by the introduction of the Language Integrated Query (LINQ) pattern to the Common Language Runtime. It is not currently standardized by any standards organisation.
In programming jargon, Yoda conditions is a programming style where the two parts of an expression are reversed from the typical order in a conditional statement. A Yoda condition places the constant portion of the expression on the left side of the conditional statement.
Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level system programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.
{{cite journal}}
: Cite journal requires |journal=
(help)