hello friends! new(ish)!
Syntactic sugar: Difference between revisions
>Thelismor (Created page with "==== Syntactic sugar ==== In discussions of syntax, you will come upon this term. Syntactic sugar is basically shorthand for certain kinds of commands. Depending on what you d...") |
>Thelismor |
||
Line 1: | Line 1: | ||
In discussions of syntax, you will come upon this term. Syntactic sugar is basically shorthand for certain kinds of commands. Depending on what you do, syntactic sugar can make your life a lot easier by making code much more legible. | In discussions of syntax, you will come upon this term. Syntactic sugar is basically shorthand for certain kinds of commands. Depending on what you do, syntactic sugar can make your life a lot easier by making code much more legible. | ||
Revision as of 03:58, 1 April 2015
In discussions of syntax, you will come upon this term. Syntactic sugar is basically shorthand for certain kinds of commands. Depending on what you do, syntactic sugar can make your life a lot easier by making code much more legible.
For example, in many languages assignment to a variable is done as follows:
x = 1
This means, "let x be 1" (in many languages, =
is not like the mathematical equals sign, but more like the :=
sign).
In order to increase x by one, you would do:
x = 1+x
This means "let x be 1 plus its current value". Incrementing a number is an extremely frequent task to do in programming, and so some languages have a shorthand:
x += 1
This means "add 1 to x" (although really the compiler just interprets x += y
as x = x+y
in most implementations). The +=
is syntax sugar: In most languages you can do it, but in Matlab there is no +=
and you are forced to type x = x+1
.
Of all the numbers you can increment by, 1 is by far the most common in actual programming tasks. Therefore, some languages go a step further, and allow:
x++
This means "increment x by 1". C allows this, but many languages don't. The difference may seem trivial, but it can have a large impact on legibility and clarity in a complex block of code. However, once again not all languages support the ++
operator.
Another example: In most languages, if you need a series of numbers such as 1, 2, 3, ... 15 or 12, 15, 18, ... 32 you must call a specialized function, such as:
series = range(12, 32, 3)
Matlab has syntactic sugar for this:
series = 12:3:32
This is very helpful when you work with arrays:
cols = MyArray[7:2:end]
will get you every second row of MyArray
starting from the 7th (end
is Matlab sugar for referring to the last element of an array). In other languages, not only must you call range inside the brackets (making code look busier) but you often can't even do this in one line at all - being able to select multiple elements of an array is another Matlab sugar. In languages like C, you would have to get each column separately and then combine them into a new array, which may take a little more work:
j = 0; for(i=7; i<sizeof(MyArray); i += 2) { cols[j] = MyArray[i]; j++; }
Syntactic sugar is convenient, but it is not necessarily good or bad. If you happen to often do the things your language's sugar tries to facilitate, and you like the sugar, it's good. Using the above two examples, you can probably see that Matlab has very nice sugar if you shuffle around arrays a lot, but C is a bit nicer if you work with algorithms that increment things a lot (as a novice, it is impossible to decide which one is relevant to you, but after you've programmed for a while you will quickly get a feel for it).
If the sugar does not help you, you can ignore it, but don't make the mistake of thinking that ignoring bad sugar will make it go away. If your language has some retarded sugar that everyone loves using for some reason, it can make reading other people's code hell. For instance, R has feature where assignment can be done in two equivalent ways:
x = 5 y -> 7
Presumably the arrow is supposed to avoid the confusion with equality. Since I am used from other languages to associate =
with inequality, ->
is unusual for me and I never use it. However, every once in a while I need to look up how to do something, and seeing a page of code full of ->
s is very jarring - I often have to first search/replace them before I can even read the code. That's not to say it's a bad feature, but clearly for me the ->
notation sugar hurts more than it helps (this is also an example of intrinsics influencing extrinsics).