Syntactic sugar

Syntactic sugar syntax extensions are in programming languages ​​, which are used to simplify notations. These extensions are alternate spellings, but not extend the expressive power and functionality of the programming language.

Syntactic sugar can be traced back ( " desugar ", dt entsüßen ) by pure text transformations on basic elements of the language.

The term syntactic sugar was coined by the British computer scientist Peter J. Landin.

Examples

Syntactic sugar in C

An example of syntactic sugar, the treatment of fields in the programming language C. C is different is not, strictly speaking. Between pointers to objects and pointers to arrays of objects If the variable p of type "pointer to byte" (char * type ), so you can with * (p 3 ) to access the third byte in memory for the address p. This can be done in C also write short than p.

Another example is the syntactic sugar infix notation. When infix notation is the operator between the operands, eg 3 5 This can be by a translator directly into classical notation of a function call add ( 3.5 ) to be transferred.

Do- notation in Haskell

In the functional programming language Haskell are used for many purposes, but in particular the Ein-/Ausgabe uses so-called monads. To read, for example, a line and a character from the standard input, front to attach the letters to the line, and outputting the result back, you would have to write

GetLine >> = \ s -> getchar >> = \ c -> putStrLn ( c: s) Wrap Better results in:

GetLine >> = \ s -> getchar >> = \ c -> putStrLn ( c: s) Because you need such constructs very often, the so-called do- notation has been introduced. The following code is exactly equivalent to the example above:

Do      s < - getLine      c < - getchar      putStrLn ( c: s) This form is strongly reminiscent of an imperative program and allows an understanding of the content to be easier.

Syntactic salt

The counterpart to the syntactic sugar is the syntactic salt - a language feature that makes it difficult to write bad or poorly readable code without extending the functionality.

Also, one must take a simple end stop (eg SPSS) in some languages ​​a loop or conditional branching with or end while end if. This has the advantage that the programmer immediately recognizes on which function block is for this end statement.

758429
de