Syntactic Sugar
Pronunciationsin-TAK-tik SHUG-er
Definition
Syntactic sugar is syntax that makes code easier to write or read but does not add new ability: it means exactly the same as a longer form.
Where you hear it
In language discussions, tutorials explaining decorators or async/await, and interviews asking what happens underneath.
Examples
The
@decoratorline is just syntactic sugar forfunc = decorator(func).A list comprehension is sugar over a loop that appends to a list.
The for-in loop is syntactic sugar over calling next() on the iterator.
Common mistake
Treating the sugar as magic. Knowing what it expands to helps you debug when it behaves unexpectedly.
Don't confuse with
A new feature, which adds something you could not do before. Sugar only changes how it is written.
Say it at work
- In a meeting or chat
That's just syntactic sugar for a function call.
- In an email or ticket
Under the hood the sugar expands into a plain loop.
Keep exploring
More in Programming Fundamentals