请输入您要查询的百科知识:

 

词条 Syntactic sugar
释义

  1. Origins

  2. Notable examples

  3. Criticism

  4. Derivative terms

      Syntactic salt    Syntactic saccharin  

  5. Notes

  6. References

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.

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 of three arguments, for example set_array(Array, vector(i,j), value), but many languages provide syntax such as Array[i,j] = value.

A construct in a language is called "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 more fundamental constructs before processing, a process sometimes called "desugaring".

Origins

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]{{sfn|Abelson|Sussman|1996|loc=Chapter 1, footnote 11}} 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.[2] 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.[3]

Notable examples

  • In COBOL, 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 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.[4][5]
  • In Perl, 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.
  • In the C language, the a[i] notation is syntactic sugar for (a + i).[6] Likewise, the a->x notation is syntactic sugar for accessing members using the dereference operator (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.[7]
  • The C# language allows variables to be declared as 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.
  • Python list comprehensions (such as [xx for x in range(10)] 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.
  • In R, 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.[8] This provides for more linear flow and design of data manipulation. The tidyverse is written to accommodate pipes.

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 community, as Lisp has very simple and regular syntax, and the surface syntax can easily be modified.{{sfn|Abelson|Sussman|1996|loc=Chapter 1, [https://web.mit.edu/alexmv/6.037/sicp.pdf#page=43 footnote 11]}}

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".{{sfn|Perlis|1982|loc=Epigram #3}}

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.[9] 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 and Pascal 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++ 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.[10] 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.[11] (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 and syntactic syrup, meaning gratuitous syntax that does not make programming any easier.[12][13][14][15]

Notes

1. ^{{cite journal |last=Landin |first=Peter J. |date=1964 |title=The mechanical evaluation of expressions |url=http://www.cs.cmu.edu/~crary/819-f09/Landin64.pdf |journal= The Computer Journal|publisher=Computer Journal |volume=6 |issue=4 |pages=308–320 |doi=10.1093/comjnl/6.4.308 |accessdate=21 July 2014}}
2. ^Barbara Liskov, "A History of CLU", MIT Laboratory for Computer Science Technical Report 561 (1993)
3. ^{{cite journal|last=Felleisen|first=Matthias|date=December 1991|title=On the Expressive Power of Programming Languages|url=http://www.cs.rice.edu/CS/PLT/Publications/Scheme/scp91-felleisen.ps.gz|journal=Science of Computer Programming|publisher=Springer-Verlag|volume=17|issue=1–3|pages=35–75|doi=10.1016/0167-6423(91)90036-W|accessdate=19 July 2014}}
4. ^{{cite web |url=https://msdn.microsoft.com/en-us/library/e1wf2hxf.aspx |title=C Compound Assignment |author= |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.}}
5. ^{{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}}
6. ^{{cite book|author=Eric S. Raymond|title=The New Hacker's Dictionary – 3rd Edition|url=https://books.google.com/books?id=g80P_4v4QbIC&pg=PA432|accessdate=5 August 2012|date=11 October 1996|publisher=MIT Press|isbn=978-0-262-68092-9|page=432}}
7. ^{{cite web|title=using Statement (C# Reference)|url=http://msdn.microsoft.com/en-ca/library/yh598w02.aspx|accessdate=16 September 2014}}
8. ^{{cite web |title=magrittr: Vignette |url=https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html |accessdate=24 December 2018}}
9. ^{{cite web |url=http://www.catb.org/jargon/html/S/syntactic-salt.html |title=The Jargon File - syntactic salt |date=2003-06-12 |access-date=2018-03-19 |archive-url=https://web.archive.org/web/20030612232319/http://www.catb.org/jargon/html/S/syntactic-salt.html |archive-date=2003-06-12}}
10. ^{{cite web|url=http://msdn.microsoft.com/en-us/library/435f1dw2.aspx|title=new Modifier (C# Reference)|publisher=Microsoft|work=microsoft.com|accessdate=3 August 2015}}
11. ^{{cite web |url=http://msdn.microsoft.com/en-us/library/vstudio/06tc147t.aspx |title=switch (C# Reference) |publisher=Microsoft |work=microsoft.com |accessdate=3 August 2015}}
12. ^{{cite web|url=http://www.catb.org/jargon/html/S/syntactic-sugar.html|title=syntactic sugar|work=catb.org|accessdate=3 August 2015}}
13. ^{{cite web|url=https://books.google.com/books?id=OfWiZNhyRGgC&pg=PA93&lpg=PA93&dq=syntactic+saccharin+example&source=bl&ots=JG088C25Mw&sig=_DnqFej7t-Wuw20YKYgHXKsD24E&hl=en&sa=X&redir_esc=y#v=onepage&q=syntactic%20saccharin%20example&f=false|title=Mathematics of Program Construction|work=google.com|accessdate=3 August 2015}}
14. ^{{cite book |last=Dean |first=Thomas |date=2004 |title=Talking with Computers: Explorations in the Science and Technology of Computing |url=https://books.google.com/books?id=mp72pk9QW0gC&pg=PA115 |publisher=Cambridge University Press |page=115 |isbn=9780521542043}}
15. ^{{cite conference |url=https://pdfs.semanticscholar.org/f396/bc99c7e444f70db69fb3f42e76e94e9d39a3.pdf |title=Fine control of demand in Haskell |last1=Harrison |first1=William |last2=Sheard |first2=Tim |date=July 8–10, 2002 |publisher=Springer Berlin Heidelberg |book-title=Mathematics of Program Construction: 6th International Conference, MPC 2002, Dagstuhl Castle, Germany, July 8–10, 2002. Proceedings |pages=93 |location=Dagstuhl Castle, Germany |conference=International Conference on Mathematics of Program Construction }}

References

{{refbegin}}
  • {{Cite book | isbn = 0-262-51087-1 | title = Structure and Interpretation of Computer Programs | last1 = Abelson | first1 = Harold | authorlink1 = Harold Abelson | last2 = Sussman | first2 = Gerald Jay | authorlink2 = Gerald Jay Sussman | first3 = Julie | last3 = Sussman | year = 1996 | origyear = 1984 | publisher = MIT Press | location = Cambridge, MA | ref = {{SfnRef|AbelsonSussman1996}} }}
  • {{cite journal | last = Landin | first = Peter J. | title = A Correspondence Between ALGOL 60 and Church's Lambda-Notation: Parts I and II | journal = Communications of the ACM | volume = 8 | issue = 2.3 | pages = 89–101, 158–165 | date = February–March 1965 | doi=10.1145/363744.363749}}
  • {{cite journal | last = Landin | first = Peter J. | title = Programming Without Imperatives – An Example | journal = UNIVAC Systems Programming Research | date = March 1965 }}
  • {{cite journal | last = Landin | first = Peter J. | title = Getting Rid of Labels | journal = UNIVAC Systems Programming Research | date = July 1965 }}
  • {{cite journal | last = Landin | first = Peter J. | title = A Generalization of Jumps and Labels | journal = UNIVAC Systems Programming Research | date = August 1965 }}, reprinted in {{cite journal | title = Higher-Order and Symbolic Computation | citeseerx = 10.1.1.85.2610 | volume = 11 | pages = 125–143 | date = 1998 }}
  • {{Cite journal | last1 = Perlis | first1 = A. J. | doi = 10.1145/947955.1083808 | title = Epigrams on programming | journal = ACM SIGPLAN Notices | publisher = Association for Computing Machinery| location = New York, NY, USA| volume = 17| issue = 9| pages = 7–13 | date=September 1982 | url = http://www-pu.informatik.uni-tuebingen.de/users/klaeren/epigrams.html| archiveurl = https://web.archive.org/web/19990117034445/http://www-pu.informatik.uni-tuebingen.de/users/klaeren/epigrams.html| archivedate = January 17, 1999 | ref = {{SfnRef|Perlis1982}} }}
{{refend}}{{FOLDOC}}

4 : Programming language syntax|Computer jargon|Source code|Programming language design

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/20 13:38:14