views:

234

answers:

9

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?

+1  A: 

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.

Kyle Rozendo
+2  A: 

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.

Ned Batchelder
+9  A: 

Syntactic sugar is a feature.

AraK
A: 

"laziness is the engine of progress" This process will stop when you can map you ideas into machine immediatly.

merin
+1  A: 

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

ima
Not by my understanding of the term. Syntactic sugar makes the language *sweeter*.
Jon Skeet
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
+9  A: 

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.

Rui Craveiro
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
+3  A: 

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

Jay
A: 

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.

Dario
+2  A: 

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.

Yishai