Programming Languages – Syntactic Sugar for Old Languages

programming-languagessyntax

I first learned about the existence of syntactic sugar languages, like CoffeeScript and SASS while working in Rails. This got me thinking… why do we not have well-known (if any at all) syntactic sugar for languages like C, C++ and Java?

Imagine a language that reads like Python or Ruby that can be preprocessed to C, and then compiled to generate really fast code that also looks beautiful.

For example, instead of:

#include <stdio.h>

int divide(int a, int b)
{
    if(b != 0)
        return a / b;

    return 0;
}

int main()
{
    int a, b;

    scanf("%d", &a);
    scanf("%d", &b);

    printf("%d", divide(a, b));

    return 0;
}

Why not have:

include stdio.h

fn divide(int a, int b):
  a/b if b != 0
  ret 0

fn main:
  int a, b

  scanf '%d', a
  scanf '%d', b

  print '%d', divide(a,b)
  ret 0

And have this preprocess down to the same C code above?

Why stick to language implementations that are either fast or easy to read when you could have both?

Best Answer

There are two well known syntactic sugar languages for C:

  • C++
  • Objective C

And two less well known, but more truly syntactic sugar as they are set on always compiling to C while the above switched to direct assembly generation:

Both C++ and Objective C started as translators to C. They switched to direct assembly generation because the code generation was getting complicated for some features. They were never designed to generate interfaces easily callable from C, but both embed complete C.

The other 2, GOB and Vala on the other hand are designed to generate interfaces easily callable from C. GOB only provides generating some boilerplate for object oriented programming while the function bodies are written in plain C, Vala is completely different language that maps to C.

All those languages provide features, not just simply different syntax. Because frankly, providing just different syntax has absolutely no benefit. It is not the syntax that governs how complicated it is to write anything in a language. It is the structural complexity of expressions and the amount of things the programmer has to keep track of. Saving a few keystrokes won't help. Programmers can generally type quite a bit faster than they think. So a different language can only help by:

  • Saving a lot of keystrokes. This is the case of GObject Builder and in part Vala. This is because boilerplate needed to create a GObject is rather large.
  • Providing actual features, like automatic reference counting in Vala.

Even then, of the four above mentioned languages, only C++ became universally popular and only because it added really powerful features. Objective C is only used on single platform and GOB and Vala are mainly niche languages for the Gnome project.

Because it is a lot of work to learn a new language. It's not enough if a programmer knows a language. Most projects are work of large teams these days and all members of the team have to know the language to use it on a project. And because people leave jobs, project leaders hesitate to use new language not only until they have enough programmers proficient in it, but also until they are sure they can hire more if needed. So new languages only catch up if they have really important new features. (Java while being rather weak language in itself has a huge advantage in comprehensive standard library. For productivity the standard library is even more important than features of the language, not to mention syntax).