DEV Community

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

Collapse
 
antonfrattaroli profile image
Anton Frattaroli

I'm a huge fan of syntactic sugar in C# - but its always there. In JavaScript, support is dependent on the environment unless you transpile and polyfill all of your code all of the time (at least for example the spread operator)

C# example (14 lines to 1 with proper linting):

public string Property { get; set; }

Is syntactic sugar for:

private string propertyField;

public string Property
{
    get
    {
        return propertyField;
    }

    set
    {
        propertyField = value;
    }
}
Collapse
 
jenc profile image
Jen Chan

Well I guess coffeescript is complete syntax caramel then!

That is a really clear example, thanks!

Collapse
 
eljayadobe profile image
Eljay-Adobe

lol "complete syntax caramel"... I love it! I'm using that!

I've also used the term "syntactic saccharine" to describe bad sugar. Causes cancer of the semicolon.

I like how CoffeeScript turns this

x?.y?.z = 3

into this

if (typeof x !== "undefined" && x !== null) {
  if ((ref = x.y) != null) {
    ref.z = 3;
  }
}

Short, sweet, and succinct. Now that is a delicious syntactic caramel!

Thread Thread
 
antonfrattaroli profile image
Anton Frattaroli

Those are great. Included with C# 6, probably copied from coffeescript.

Collapse
 
timellison profile image
Tim Ellison

If I recall correctly, the C# language spec or some of Microsoft's engineers made the property syntax above as a means by which to entice VB programmers to start using the language. This was "sugar" for get_Property() and set_Property(string value) respectively, a common naming convention in Java for functions that represent properties. Great article by the way and I appreciate the positive vibes.