Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntactic sugar vs. feature

In C# (and Java) a string is little more than a char array with a stored length and a few methods tacked on. Likewise, (reference vs. value stuff aside) objects are little more than glorified structs with inheritance and interfaces added.

On one level, these additions feel like clear features and enhancements unto themselves. On another level, they feel like a marginal upgrade from the status of "syntactic sugar."

To take this idea further, consider (I may have some details wrong, but the point remains):

transistor
elementary logic gate
compound gate
  |         |
 ALU    flip-flop
   |    |       |
   | register  RAM
   | |
   CPU
   microcode
   assembly
   C
   C++
   | |
MSIL JavaScript
C#   jQuery

Many times, any single layer of abstraction looks a lot like syntactic sugar but multiple layers of separation feel very removed from each other.

How do you know when something has stopped being syntactic sugar and started being a bona fide feature?

like image 560
Dinah Avatar asked Sep 07 '09 17:09

Dinah


People also ask

What does syntactic sugar mean?

“Syntactic sugar” is a term for syntax changes in computer programming which make it easier for humans to code. There are several different types of syntactic sugar and synonyms include “syntactic saccharine” and even “candygrammar,” which is often used to describe superfluous or unhelpful “syntactic sugar” changes.

What is an example of syntactic sugar?

A common example of syntactic sugar is an augmented assignment operator, such as +=, which simplifies the expression x = x + y to the compound operation x += y.

What is syntactic sugar in Scala?

Scala provides some useful syntactic sugars. 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.

What is syntactic sugar in Dart?

Cascade notation is syntactic sugar in Dart that allows you to make a sequence of operations on the same object.


1 Answers

It turns out to be a feature instead of syntactic sugar when it implies a different way of thinking.

You are right when you say objects are in fact glorified structs with methods and inheritance. That, however, is just the implementation detail. What objects allow is to think in a different way. You can relate more easily to real world entities when thinking about objects. The same thing happened when even further back in time, we jumped from using go-to's to procedural programming. Under the hood, the processor still keeps on jmp'ing from OP to OP, but we could think in a different, more black-box, way.

Having said that, in extreme, you can say everything is syntactic sugar, but some of that sugar is a feature when it allows you to think differently.

like image 185
Rui Craveiro Avatar answered Oct 11 '22 23:10

Rui Craveiro