Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Syntactic sugar
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Notable examples == * In [[COBOL]], many of the intermediate keywords are syntactic sugar that may optionally be omitted. For example, the sentence <code>MOVE A B.</code> and the sentence <code>MOVE A TO B.</code> perform exactly the same function, but the second makes the action to be performed clearer. * [[Augmented assignment]] or compound assignment operators: For example, <code>a += b</code> is equivalent to <code>a = a + b</code> in C and similar languages, assuming <code>a</code> has no side effects such as if <code>a</code> is a regular variable.<ref>{{cite web |url=https://msdn.microsoft.com/en-us/library/e1wf2hxf.aspx |title=C Compound Assignment |author=<!--Staff writer(s); no by-line.--> |website=msdn.microsoft.com |publisher=Microsoft |access-date=20 June 2016 |quote=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.}}</ref><ref>{{cite web |url=http://programmers.stackexchange.com/a/134136 |title=Why are shortcuts like x += y considered good practice? |last1=Garavaglia |first1=Emilio |date=26 July 2015 |website=stackexchange.com |access-date=20 June 2016 |quote=optimization can [be done] if 'finding x' has no side effects}}</ref> Some languages, such as [[Python (programming language)|Python]]<ref>{{cite web |url=https://docs.python.org/3/reference/datamodel.html#object.__iadd__ |title=Python Data model |date=21 December 2020 |website=docs.python.org}}</ref> may allow [[operator overloading|overloading]] augmented assignment operators, so they may behave differently than standard ones. * In [[Perl]], <code>unless (condition) {...} </code> is syntactic sugar for <code>if (not condition) {...}</code>. Additionally, any statement can be followed by a condition, so <code>statement if condition</code> is equivalent to <code>if (condition) {statement}</code>, but the former is more naturally formatted on a single line. * In the [[C (programming language)|C language]], the <code>a[i]</code> notation is syntactic sugar for <code>*(a + i)</code>.<ref name="Raymond1996">{{cite book|first=Eric S.|last=Raymond|title=The New Hacker's Dictionary β 3rd Edition|url=https://books.google.com/books?id=g80P_4v4QbIC&pg=PA432|access-date=5 August 2012|date=11 October 1996|publisher=MIT Press|isbn=978-0-262-68092-9|page=432}}</ref> Likewise, the <code>a->x</code> notation is syntactic sugar for [[C syntax#Accessing members|accessing members]] using the [[dereference operator]] <code>(*a).x</code>. * The <code>using</code> statement in [[C Sharp (programming language)|C#]] ensures that certain objects are disposed of correctly. The compiler expands the statement into a [[Exception handling|try-finally]] block.<ref>{{cite web|title=using Statement (C# Reference)|url=http://msdn.microsoft.com/en-ca/library/yh598w02.aspx|access-date=16 September 2014}}</ref> * The C# language allows variables to be declared as <code>var x = expr</code>, which allows the compiler to [[Type inference|infer]] the type of <code>x</code> from the expression <code>expr</code>, instead of requiring an explicit type declaration. Similarly, C++ allows <code>auto x = expr</code> since C++11 and Java allows <code>var x = expr</code> since Java 11. * Python [[Comparison_of_programming_languages_(list_comprehension)#Python|list comprehensions]] (such as <code>[x*x for x in range(10)]</code> for a list of squares) and [[Python syntax and semantics#Decorators|decorators]] (such as <code>@staticmethod</code>). * In [[Haskell (programming language)|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 (programming language)|R]] packages, the ''pipe'', denoted by <code>%>%</code>, declares that the data (or output of the function) preceding the pipe will serve as the first argument for the function following the pipe.<ref>{{cite web |title=magrittr: Vignette |url=https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html |access-date=24 December 2018}}</ref> So, <code>x %>% f(y)</code> is equivalent to <code>f(x,y)</code>. * In [[SQL]], a mere <code>JOIN</code> is equivalent to an <code>INNER JOIN</code>, 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 <code>OUTER</code> from the <code>LEFT OUTER JOIN</code>, <code>RIGHT OUTER JOIN</code> and <code>FULL OUTER JOIN</code>. *[[Extension method]] in OOP languages in the form of <code>'''myObject'''.myMethod(parameter1, parameter2, parameter3)</code> is syntactic sugar for calling a global function as <code>myMethod('''myObject''', parameter1, parameter2, parameter3)</code>. The reference to the object is passed as a hidden argument, usually accessible from within the method as '''<code>this</code>'''. *[[Evaluation strategy#Call by reference|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 (Programming Language)|Java]], an <code>import</code> declaration enables the compiler to find classes that are not otherwise specified with fully qualified names. For example <code>import javax.swing.*;</code> allows the programmer to reference a [[Swing (Java)|Swing]] object such as <code>javax.swing.JButton</code> using the shorter name <code>JButton</code>. * In the [[ES6]] version of [[JavaScript]], arrow functions have a short form <code>(x) => x + 1</code>, which is equivalent to the longer form <code>(x) => { return x + 1; }</code>. * In [[Scala (programming language)|Scala]], triple questions marks (<code> ??? </code>) is equivalent to <code> throw new NotImplementedError </code>. This is useful to mark a place for code that has not yet been written.<ref>{{cite web|url=https://stackoverflow.com/questions/31302524/what-does-the-triple-question-mark-mean-in-scala|title=Stack Overflow: What does the triple question mark mean in scala?|access-date=23 January 2024}}</ref> * In [[JavaScript]], if the key and value are the same in an object, you can only write it once. For example, <code>{name: name}</code> is equivalent to <code>{name}</code>. This is called the Shorthand Property.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)