15

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?

2
  • 1
    Do you refer to language-design? What do you mean with something?
    – Dario
    Sep 7, 2009 at 17:52
  • 1
    Fundamentally subjective, I think. Sep 7, 2009 at 21:14

9 Answers 9

28

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.

2
  • 1
    I've thought about your answer many times in the past few months and the more I apply it to situations, the more I agree with it.
    – Dinah
    Jan 12, 2010 at 20:59
  • 1
    Interestingly, there is what you could call a "dialectic" relationship between the way of thinking and the language features. A new language feature does not appear out of the blue -- instead, it is the answer to a perceived need, theoretical work and experimental implementations. And, conversely, the new feature supports a new way of thinking in the community. When you have grokked C++ you'll start using structs and functions operating on them, including factory functions and destructor functions. Likewise, your assembler programming paradigm changes after you learned C... Sep 2, 2022 at 6:38
12

Syntactic sugar is a feature.

1
  • 2
    In broad sense, yes. But I don't think you answered in the spirit of OP's question.
    – nawfal
    Jun 7, 2013 at 8:34
5

All of software is a giant stack of abstractions built on top of other abstractions. A string may be nothing more than an array of characters, but there are many operations that feel natural on strings, but awkward on character arrays. The goal of all of these abstractions is the same: remove irrelevant details so that the developer can focus on the important parts of the problem.

As you point out, all modern programming languages could be eliminated, and we could go back to working in assembly language. But our productivity would plummet.

I guess people call something syntactic sugar when they feel they get little benefit from it, and a feature when they feel the get a large benefit from it. That makes the distinction very fuzzy, and quite subjective.

5

When the change provides value? I have coded in assembler. I switched to C and looked at the output from the compiler. It's code was 95+% as good as my hand crafted assembler and it was much easier to write. For me that provided value so I'd say it wasn't sugar.

C++ helps me translate my object oriented thoughts into code. As long as the overhead isn't terribly high then I think it's a feature.

I'm a practical sort. "If I can see it's valuable" is my answer

3

"Syntactic sugar" is a feature you don't like

2
  • 3
    Not by my understanding of the term. Syntactic sugar makes the language sweeter.
    – Jon Skeet
    Sep 7, 2009 at 18:08
  • 2
    But then you have to describe taste of sweetened curried syntax - in pleasant terms. On the serious side, if feature was added to the language evolutionarily and wasn't received exceptionally well, it will be called syntactic sugar.
    – ima
    Sep 7, 2009 at 20:04
3

It seems that syntactical surgar is a syntax that changes nothing about the abilities of the language, and using a different construct accomplishes exactly the same thing. A String (thinking in Java) is not just syntatical sugar over a char array. A char array is mutable (in content if not in length). You could not make a char array immutable with an existing language feature without a String array.

On the other hand, the plus operator working on Strings is indeed syntatical sugar for using a StringBuilder and calling append.

2

I would have to say when the same result is cannot be achieved simply by writing different code, with the same type of "time-constraint" as using the syntactical sugar.

My Example would be a Lambda expression, writing a foreach loop doesn't take a lot of effort, but using .Foreach() sure is nice too; versus rewriting the whole HttpRequest class on your own. One is syntactical, one is a feature. Both save time, one in a much bigger way than the other.

1

Generally the term "syntactic sugar" refers to language features which never allowed a programmer to do something which could not be done before, but rather provided a nice means of expressing something that could already expressed in the language, even if somewhat more awkwardly.

Certain constructs may be unambiguously regarded as syntactic sugar. For example, in VB.NET, code to test for whether two references weren't equal used to require If Not (ref1 Is Ref2) but newer versions of the language allow If ref1 IsNot Ref2. Nothing can be expressed in the new syntax that couldn't be expressed in the old, but the new syntax is cleaner, introduces no ambiguities, and the only reason not to use it would be if code had to be back-compatible with old versions of the language.

Some constructs may be a bit harder to define as sugar. In particular, if a language adds constructs which will work identically to existing construct when used with other types, but will fail compilation with others, such constructs may provide a means of compile-time type verification which did not exist previously. Java generics may generally be viewed in this light. One can add a Cat to an ArrayList<Cat> just as easily as to an ArrayList; what the ArrayList<Cat> adds is a guard to reject Dogs at compile time. Since compile-time constraints don't allow one to write any program that couldn't be written without them, some people may view them as syntactic sugar. On the other hand, even though type verification is performed at compile-time rather than run-time, it might still be viewed as one of the jobs of a program.

0

Syntactic sugar and language feature are basically describing the same thing, even if syntactic sugar is sometimes used in a pejorative way whereas feature is often associated with deeper changes in the language architecture (introducing lambdas etc.). But this distinction is very dependent on a individual point of view (and its subjectively felt usefulness).

Regarding language-design aspects and your example with strings and char-arrays, I would say that this should be neither a feature nor sugar, but simply expressible in the languages basic syntax (LOP - language-oriented programming). Generic concepts (typeclasses, metaprogramming etc.) allow you to express many new and useful constructs by yourself without waiting for the language to get a new feature. Just look at Haskell or C++'s metaprogramming capabilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.