Syntactic sugar: definition, origin and examples

Table of contents:

Syntactic sugar: definition, origin and examples
Syntactic sugar: definition, origin and examples
Anonim

In computer science, syntactic sugar is linguistics in a programming language. It is designed to make code lighter, more readable, and more expressive. This sugar makes the tongue "sweeter" for human use. That is, things can be expressed clearly, concisely, or in an alternative style that some may prefer.

Syntactic sugar: what is it?

Syntactic sugar, definition
Syntactic sugar, definition

Many programming languages provide a special grammar department for updating elements. Abstractly, a reference to a given object is a procedure of two arguments: an array and a subscript, which can be expressed as

get_array(Array, vector(i, j))

. Instead, many languages provide syntax such as

Array [i, j]

. Similarly, updating an array element like

set_array(Array, vector(i, j), value)

is a three-argument procedure, but many pros provide code like

Array[i, j]=value.

A construct in a language is called "syntactic sugar" if it can be removed from a program without any impact on functionality and expressiveness.

Various processors, including compilers and static analyzers, often extend sweetened constructs to more fundamental devices before processing. This process is called “desagering.”

Origin

The term "syntactic sugar" was coined by Peter J. Landin in 1964 to describe the surface grammar of plain ALGOL, a programming language that was defined semantically in terms of lambda calculus applicative expressions centered on the lexical replacement of λ with "where "".

Later programming languages such as CLU, ML and Scheme expanded the term to mean a derivative in a language that can be defined as syntactic sugar in terms of the core of basic constructs. Higher-level convenience functions can be "disaggregated" and decomposed into a subset. This is, in fact, the usual mathematical practice of building from primitives.

Based on Landin's distinction between basic language constructs and the properties of syntactic sugar, in 1991 Matthias Felleisen proposed a codification of "expressive power" to conform to widely held beliefs in literature. He defined it as "more meaningful" to mean that without the language constructs in question, the program would have to be completely reorganized.

Famousexamples of syntactic sugar

Syntactic Sugar Examples
Syntactic Sugar Examples

In COBOL, many of the intermediate keywords are "sweet", meaning they can be omitted if desired. For example, the sentence MOVE A B. and MOVE A TO B. do exactly the same thing, but the second one makes the action to be performed clearer.

Extended compound assignment operators: e.g. a +=b is equivalent to a=a + b in C and similar languages, assumes a has no side effects, e.g. a is a regular variable if.

In Perl,

unless (condition) {…}

is syntactically

if (not condition) {…}

. Also, any statement can be followed by a condition such that

statement if condition

is equivalent to

if (condition) {statement}, but the former is more naturally formatted in one line.

In the C language, pointers to the beginning of a memory element can be written without using special syntaxes:

(a + i)

. Although there is a special syntax for this process in this language: a[i]. Likewise,

a->x

, the notation is syntactic sugar for accessing members using the

(a) dereference operator. x.

Using

Declaration in C ensures that certain objects are disposed of correctly. The compiler expands the statement into a try-finally block.

The Clanguage allows variables to be declared as

var x=expr

, which allows the compiler to infer the type

x

from the expression

exprinstead of requiring an explicitannouncements.

Lists also contain Python syntactic sugar (e.g.

[xx for x in range (10)]

for a list of squares) and decorators (

@staticmethod).

In Haskell, a quoted string is semantically equivalent to a number of characters.

In the rvest package, the designation %>% occurs, and indicates that the data (or function output) preceding it will serve as the first argument of the next tool. This provides a more linear flow and data manipulation design. Tidyverse is written to accommodate values.

Criticism

property syntactic sugar
property syntactic sugar

Some programmers feel that these syntax uses are either not important or just not serious. Notably, special linguistic forms make the language less uniform and its specification more complex, and can cause problems as programs get larger. This representation is especially widespread in the Lisp community, as it has a very simple, regular, and shallow syntax that can be easily modified.

Derived terms

syntactic sugar what is it
syntactic sugar what is it

Syntactic s alt. The metaphor has been expanded to include this term, which refers to a feature designed to make bad code harder to write. In particular, the syntax s alt is a hoop that programmers have to jump over to prove they know what's going on, not to express the program's action. For example, in Java andPascal assigning a float value to a variable declared as int without additional syntax explicitly stating that the intent will result in a compile error, while C and C++ automatically truncate all floats assigned to int. However, this is not syntax, but semantics.

In C, hiding an inherited class member generates a compiler warning unless a keyword is used to indicate that the hiding is intentional. This is to avoid possible errors due to the similarity of the switch statement syntax to that of C or C++, C requires a break for every non-empty case of the switch label, even if it does not allow implicit falls.

Syntax s alt can defeat its purpose, making the code unreadable and thus degrading its quality. In extreme cases, the body may be shorter than the overhead introduced to meet language requirements.

An alternative to this concept is to generate compiler warnings when there is a high probability that the code appears to be the result of an error, a practice common in modern C/C++ compilers.

Syntactic Saccharin

cup of coffee with cream
cup of coffee with cream

Another extension is also syrup. It, like saccharin, means gratuitous syntax that does not make programming easier.

It may seem strange to call a language "sweet", but if you work in Rubyist, then it will be justified. This program has more syntacticsugar than in many languages because it emphasizes human understanding rather than computer understanding. The creator of Ruby, Yukihiro Matsumoto, wanted to make the language not only efficient, but also fun. Compilers and interpreters may like such a highly structured, unambiguous grammar section, but it may be difficult for humans to understand. This is where syntactic sugar comes in - it makes the language “sweeter” both in writing and in reading.

Writing code

python syntactic sugar
python syntactic sugar

It must be remembered that "syntactic sugar" is not a technical term, but a construct meant to help describe the way a language is expressed. Simply put, this term means optimized code for people. The goal is to simplify the syntax so that it is easy to read, even if it reduces some of the technical clarity. Of course, writing sweet code doesn't mean you can skip an important understanding step.

As in real life, knowing how much sugar is being used is important for overall he alth. Sugar keeps the code simple and expressive, but it also causes ambiguity. This is usually due to the fact that not everyone knows and applies this concept when programming.

Popular topic

Editor's choice