Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Origins  





2 Notable examples  





3 Criticism  





4 Derivative terms  



4.1  Syntactic salt  





4.2  Syntactic saccharin  





4.3  Sugared types  







5 Notes  





6 References  














Syntactic sugar






العربية
Čeština
Dansk
Deutsch
Español
Esperanto
Français

Ido
Italiano
עברית
Nederlands

Norsk bokmål
Polski
Português
Русский
Suomi
Svenska
Türkçe
Українська
Tiếng Vit

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Incomputer science, syntactic sugarissyntax 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".

Origins[edit]

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]

Notable examples[edit]

Criticism[edit]

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]

Derivative terms[edit]

Syntactic salt[edit]

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.

InC#, 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.

Syntactic saccharin[edit]

Other extensions are syntactic saccharin and syntactic syrup, meaning gratuitous syntax that does not make programming any easier.[17][18][19][20]

Sugared types[edit]

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.

Notes[edit]

  1. ^ Landin, Peter J. (1964). "The mechanical evaluation of expressions" (PDF). The Computer Journal. 6 (4). Computer Journal: 308–320. doi:10.1093/comjnl/6.4.308. Retrieved 21 July 2014.
  • ^ Abelson & Sussman 1996, Chapter 1, footnote 11.
  • ^ Barbara Liskov, "A History of CLU", MIT Laboratory for Computer Science Technical Report 561 (1993)
  • ^ Felleisen, Matthias (December 1991). "On the Expressive Power of Programming Languages". Science of Computer Programming. 17 (1–3). Springer-Verlag: 35–75. doi:10.1016/0167-6423(91)90036-W. Retrieved 19 July 2014.
  • ^ "C Compound Assignment". msdn.microsoft.com. Microsoft. Retrieved 20 June 2016. 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.
  • ^ Garavaglia, Emilio (26 July 2015). "Why are shortcuts like x += y considered good practice?". stackexchange.com. Retrieved 20 June 2016. optimization can [be done] if 'finding x' has no side effects
  • ^ "Python Data model". docs.python.org. 21 December 2020.
  • ^ Raymond, Eric S. (11 October 1996). The New Hacker's Dictionary – 3rd Edition. MIT Press. p. 432. ISBN 978-0-262-68092-9. Retrieved 5 August 2012.
  • ^ "using Statement (C# Reference)". Retrieved 16 September 2014.
  • ^ "magrittr: Vignette". Retrieved 24 December 2018.
  • ^ "Stack Overflow: What does the triple question mark mean in scala?". Retrieved 23 January 2024.
  • ^ Abelson & Sussman 1996, Chapter 1, footnote 11.
  • ^ Perlis 1982, Epigram #3.
  • ^ "The Jargon File - syntactic salt". 2003-06-12. Archived from the original on 2003-06-12. Retrieved 2018-03-19.
  • ^ "new Modifier (C# Reference)". microsoft.com. Microsoft. Retrieved 3 August 2015.
  • ^ "switch (C# Reference)". microsoft.com. Microsoft. Retrieved 3 August 2015.
  • ^ "syntactic sugar". catb.org. Retrieved 3 August 2015.
  • ^ Boiten, Eerke A.; Möller, Bernhard (2002-06-26). Mathematics of Program Construction. Springer. ISBN 9783540438571. Retrieved 3 August 2015.
  • ^ Dean, Thomas (2004). Talking with Computers: Explorations in the Science and Technology of Computing. Cambridge University Press. p. 115. ISBN 9780521542043.
  • ^ Harrison, William; Sheard, Tim (July 8–10, 2002). "Mathematics of Program Construction" (PDF). Mathematics of Program Construction: 6th International Conference, MPC 2002, Dagstuhl Castle, Germany, July 8–10, 2002. Proceedings. International Conference on Mathematics of Program Construction. Lecture Notes in Computer Science. Vol. 2386. Dagstuhl Castle, Germany: Springer Berlin Heidelberg. p. 93. doi:10.1007/3-540-45442-X_6. ISBN 978-3-540-43857-1. S2CID 10059915. Archived from the original (PDF) on March 31, 2017.
  • ^ Chugh, Ravi (2013). Nested Refinement Types for JavaScript (PhD). UC San Diego.
  • ^ "C Language LLVM Documentation". clang.llvm.org. Retrieved 30 June 2020.
  • ^ "The Secret Life of Types in Swift". medium.com/@slavapestov. 14 July 2016. Retrieved 30 June 2020.
  • References[edit]

  • Landin, Peter J. (February–March 1965). "A Correspondence Between ALGOL 60 and Church's Lambda-Notation: Parts I and II". Communications of the ACM. 8 (2.3): 89–101, 158–165. doi:10.1145/363744.363749. S2CID 6505810.
  • Landin, Peter J. (March 1965). "Programming Without Imperatives – An Example". UNIVAC Systems Programming Research.
  • Landin, Peter J. (July 1965). "Getting Rid of Labels". UNIVAC Systems Programming Research.
  • Landin, Peter J. (August 1965). "A Generalization of Jumps and Labels". UNIVAC Systems Programming Research., reprinted in "Higher-Order and Symbolic Computation". 11. 1998: 125–143. CiteSeerX 10.1.1.85.2610. {{cite journal}}: Cite journal requires |journal= (help)
  • Perlis, A. J. (September 1982). "Epigrams on programming". ACM SIGPLAN Notices. 17 (9). New York, NY, USA: Association for Computing Machinery: 7–13. doi:10.1145/947955.1083808. S2CID 20512767. Archived from the original on January 17, 1999.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Syntactic_sugar&oldid=1230124888"

    Categories: 
    Programming language syntax
    Computer jargon
    Source code
    Programming language design
    Metaphors referring to food and drink
    Hidden categories: 
    Articles with short description
    Short description is different from Wikidata
    CS1 errors: missing periodical
     



    This page was last edited on 20 June 2024, at 20:00 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki