ArticleslgStudy

computer science

Syntactic sugar

Syntactic sugar is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Syntactic sugar rather than just read about it. In short: 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.

Key takeaways

  • Syntactic sugar belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Syntactic sugar to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Syntactic sugar from memory before moving on to harder problems.

Reference excerpt

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".

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

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. 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) {...}, 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). 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. 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 [x*x 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. 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 (x) => { return x + 1; }. In Scala, triple question marks (???) is equivalent to throw new scala.NotImplementedError("an implementation is missing"). This is useful to mark a place for code that has not yet been written.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Syntactic sugar

Start with the simplest possible case. Write down what Syntactic sugar claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Syntactic sugar before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Syntactic sugar ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Syntactic sugar

In research
Syntactic sugar appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Syntactic sugar in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Syntactic sugar is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer jargon, Metaphors referring to food and drink, Programming language design, so understanding it makes those chapters shorter.
In everyday life
Look for Syntactic sugar outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Syntactic sugar in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Syntactic sugar means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Syntactic sugar out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Syntactic sugar in simple terms?

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.

Why does Syntactic sugar matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Syntactic sugar?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Syntactic sugar.

Tags

  • Computer jargon
  • Metaphors referring to food and drink
  • Programming language design
  • Programming language syntax
  • Source code

Keep exploring