subscribe

On syntactic sugar

Ever so often the term ‘syntactic sugar’ comes when people discuss language features, and it’s not uncommon to see the word ‘just’ right in front of it; some examples:

The ‘just’ has a lot of meaning here. To me it suggests that language features that are ‘just’ syntactic sugar, aren’t quite as important as features that aren’t. Maybe it even suggests to me that the language would be fine without.

So while the above two examples both argue that both Javascript classes and async/await aren’t syntactic sugar, they also kind of come to the defence of those features and justify their existence. In other cases when people call something syntactic sugar, it’s often in a context that’s somewhat dismissal of the feature.

I think this is a bit odd and also confuses people, especially since any actual definition I’ve found is generally positive, such as this one from Wikipedia.

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.

The thing is, isn’t every language feature beyond the bare minimum of what makes a language turing-complete syntax sugar?

  • You don’t need classes because you can use functions and structs.
  • You don’t really need types because everything can fit in a string.
  • Functions can be implemented with goto.
  • Multiply can be implemented with addition.
  • or and and, xor can be implemented with nand.
  • async/await can be implemented with generators.

These are all incredibly useful features that make it easier to read and write code and express ideas. Almost every language feature could be considered syntactic sugar. That’s not a bad thing, it’s just uninteresting to point out.

Web mentions