DEV Community

Cover image for Sh*tpost: can we stop saying "syntactic sugar"?
Jen Chan
Jen Chan

Posted on

Sh*tpost: can we stop saying "syntactic sugar"?

"Syntactic sugar": After self-studying what seems like a narrow and ever-changing field for four years this term keeps rearing its head. I'm not usually one to squabble over the semantic aspects of any language... but this noun has sent me into a confusing tailspin before.

The first time I heard of the term "syntactic sugar" was in relationship to React. I heard about it in terms of JSX in a Wes Bos React video. Then it was described as such in terms of async and await. Hell, there are even talks on this!

syntactic sugar toy story meme

(Without Googling) What I interpret it to mean between seasoned developers, is that syntactic sugar is an aspect of a language or framework that allows devs to write code in an abbreviated, clear and/or easier way.

For example, writing JSX helps with integrating javascript into HTML in a view while writing partials. Async/await makes Javascript look as if it's synchronous while reminding us it's going to run asynchronously, pause the execution of a function and wait til a promise resolves or information returns.

Couldn't it be argued that anything that abbreviates or makes writing code simpler and cleaner "syntactic sugar"? Wouldn't template literals, destructuring or even ++ and += then be syntactic sugar?

Despite the analogy, the term "syntactic sugar" did not/is not helping me, or learners understand what the very sweet, refined characteristic of a language or framework does!

Every time I hear it, I'm just going to omit and continue reading the examples.

rant.end()

Ok. I'm open to hearing how this term is canonical and useful. It's likely I'm just "young" on my second career and disgruntled.


So I did have to google if anyone on the internet shared my opinion, and this post seems to place my sentiments more eloquently.

I needed a definition anyway, so here are some resources I found helpful:
Sweet API – Syntactic Sugar And You
Quora | What is Syntactic Sugar?

Latest comments (56)

Collapse
 
nigelthorne profile image
Nigel Thorne

"It's just Syntactic sugar" says "There is another way to do this in the language already". This statement is only valuable if used in the form "It's just Syntactic sugar for ...". This explains new syntax in terms of the verbose version a person already understands. That has valuable. It's like saying "Don't panic it's just a new way to say ...."

Collapse
 
lespinter profile image
LesPinter

I write similar rants. My pet peeve is using "persist" to describe writing XML to a disk file; and to read it back, "depersist"... Really? It's like saying "cray-cray" instead of crazy. Do you think it makes you sound creative? Or just arrogant and a little dumb.

Collapse
 
ver_kat profile image
Veronika T

I recently had an idea for a t-shirt for women with "syntactic sugar" on it - sort of an appropriation of the trend a few years ago to have "angel" on the back of your sweatpants. Anyway, when I googled to see if I could buy one someone else designed, I found one that said, "Assembly is just syntactic sugar." Probably the most extreme (and I assume ironic) assessment of what you express here.

Collapse
 
jenc profile image
Jen Chan

HAHA oh yeah a couple of my friends recall now and again the 2000s "Juicy" sweatpants. That trend had spread to Victoria's Secret Pink underwear and I recall being kind of distrubed some time back teen underwear seemed to be lined with catcalling phrases.

I don't know Assembly so that statement is lost on me... but lol Date.now() + createdOn... I am finally comfortable with people saying "syntactic sugar" after hearing all the contexts it's used in. I still wish we all agreed on a semantic definition like "shorthand for doing the same thing".

Collapse
 
ver_kat profile image
Veronika T

Assembly is just a step above binary, so VERY low level - the lowest you can get without using ones and zeroes. It's not particularly human friendly - well I guess it is compared to ones and zeroes. :-) So technically it IS syntactic sugar, but not in the way people mean to use that phrase. I guess it implies people who complain about syntactic sugar might as well be using ones and zeroes if they're so above whatever they're talking about.

Collapse
 
smolinari profile image
Scott Molinari

A good example of syntactic sugar is the class keyword in JavaScript. It is syntactic sugar for a constructor function. And usually, when saying syntactic sugar, there is the negative connotation of "it's not really necessary" added to it. So, those who like the class syntax don't use the term syntactic sugar for the class keyword and those who don't like it do.

Scott

Collapse
 
moopet profile image
Ben Sinclair • Edited

This entire post is Semantic Seasoning, which means it's about changing the way things look without changing their functionality, and it's really salty.

I think the phrase "semantic sugar" exists to let use know that something only exists for that reason. If someone introduces a way of abbreviating the language without changing its functionality, that's SS. It's bad because it's not backwards-compatible and doesn't add anything new.

Your example of += is semantic sugar, but because it's been in languages for donkeys' years it's ok. If you were to take a language without that symbol (Python?) then introduced a mod to allow it, that'd make a lot of people happy. But it would require everyone else to use the same mod (whether it be a library or a change to the interpreter) and that mod would be seen as unnecessary by a lot of people.

I think that's the difference. SS is a problem when it makes unnecessary things necessary.

Collapse
 
sebbdk profile image
Sebastian Vargr

I’ve never thought about the fact that it could be misunderstood.

By the then again, I learned to ignore buzz a long time ago. It’s usually accompanied by a stinger or something that sat on a turd a moment ago.

Collapse
 
begrafx profile image
BEGRAFX

Am I the only one questioning whether or not they've been living under a rock... because before coming across this article, I had NEVER heard anyone use the term "Syntactic Sugar before?

Collapse
 
pallenrupp profile image
pallenrupp

i’ve heard this term too, and find it annoying... and i’ve been in i.t for awhile. unfortunately, some nerd thought it was cute, and it caught on. Let’s hope it goes away soon :-)

Collapse
 
beachbc profile image
Bradley Beach

I first heard the term when Rails first started gaining popularity and I was exposed to Ruby. I don't take it as something negative, to me it just means a language syntax feature that simplifies, it's not a different feature, just a different syntax. Like the "5.times do" syntax in Ruby, it can be done with a for or while loop just like in other languages.

Collapse
 
demolishun profile image
Demolishun

Unnecessary features that improve the QOL of the programmer.
In Qt there is the keyword "emit" that compiles to nothing. It is only used to show the intent of the code to follow. Easing understanding later. Someone also mentioned +=, *=, -=, etc.
My gut reaction is something that may cause you to say "SWEET!", but a lot of things fall under that category.

However, QOL could encompass the entire language if the definition is taken too far. Maybe someone could define a mathematical test of syntactic sugar?

Collapse
 
leob profile image
leob

I think 'syntactic sugar' is indeed a useful term (IMO) for a commonly used technique or pattern in software development.

You could say 'syntactic sugar' means putting a "sweet" or easy-to-use layer on top of a complicated piece of 'machinery'. Sort of "sugarcoating" that piece of complexity.

It's called 'sugar' because it's not changing the essence of the 'dish' (the underlying machinery is still there) but only making it taste 'sweeter'.

Async/await is a good example, if you take it away then you get to see all of the 'ugly' (well not really) promise stuff, using async/await hides that away from view but underneath it's still there.

Collapse
 
andreirusu_ profile image
Andrei Rusu

Yes, I never cared much for it. It doesn't add anything useful to the conversation and it will most likely be used with a condescending tone by someone who's job is to lecture you on things you already know.

Collapse
 
kritner profile image
Russ Hammett

I think we need some syntactic sugar to describe syntactic sugar! I love syntactic sugar so much, but there's currently no syntactic sugar to show off my love for that which is syntactic sugar! :trollface:

Collapse
 
sqlrob profile image
Robert Myers

No, I don't think you can say syntactic sugar doesn't give any business value.

Can you write the shorter form more quickly and maintain it easier? There's your business value.

Collapse
 
simov profile image
simo • Edited

When used, the term Syntax Sugar means that you should probably look it up, and see how this thing can be desugared to better understand the topic.

I'm giving you an example:

if (true) {
  let be = 'nice!'
}

is desugared to:

if (true) (() => {
  var be = 'nice!'
})()

You're welcome!