In my quest to get a feel for all somewhat relevant programming languages I inevitably came across Dart. Contrary to Prolog, the language I recently covered, it was not initially developed over 40 years ago but its first stable version only saw the light of day a few months ago.

Dart is, together with Go, one of two languages developed by Google. It’s own tagline describes Dart as “a new platform for scalable web app engineering”, and a leaked internal email states its goal as to “ultimately replace JavaScript as the lingua franca of web development”.

It’s very important to note that the Dart effort was founded and is still headed by Lars Bak and Kasper Lund. Before working on Dart, they were demonstrating their abilities by creating V8, the ridiculously fast JavaScript VM that played a major part in lifting the popularity of the language to its current heights. V8 is also what powers node.js under the hood.

But the team felt that they hit a ceiling with V8 and decided to overcome this by creating a new language: Dart. The whole language was designed with the goal of being able to create a highly performant VM to power it. This effort was clearly a success because the Dart VM is even in its current (not especially optimized) state already twice as fast as V8. Supposedly they claimed during the announcement of the Dart 1.0 release at the Devoxx conference, that they think they can reach the performance of the JVM.

Since the Dart VM does not ship with any browser yet, a Dart to JavaScript compiler is provided with the Dart SDK. Furthermore, Dart can also be used on the server-side.

Last Saturday the GDG Vienna organized the first Dart Flight School at sektor5. These events are sponsored by Google and are set out to take place in different countries around the world to bring Dart to the masses. In Vienna Nik Graf from Blossom and Herbert Poul introduced Dart in two talks and then the 40 to 50 attendees were encouraged to discover the language by building a small project of their choice.

I’ve been tired of UI-development lately and so I decided to explore how Dart works on the server-side. I created a simple RESTful web service that uses a local Redis store to read and persist data. The source code of my experiment can be found on GitHub.

Language

Dart’s syntax looks instantly familiar to anyone who ever used a language like JavaScript, Java, or C. This was a conscious design decision in order to not create a high barrier to entry for new developers and to enable them to be productive quickly. I found this to be true and was able to write code easily.

The language is optionally typed which means that type information can either be supplied or also left out.

fetchArticles(type, offset) {
  //...
}

// ... or with types and an optional parameter ...

List<Article> fetchArticles(String type, int offset, {int limit: 10}) {
  //...
}

This puts Dart in the category of dynamic languages like JavaScript or Ruby because the type information is not needed – and does not even affect runtime behavior. It’s only used during development by tools like dartanalyzer or the official Dart Editor to point out mistakes. For production code it’s considered good style to supply the type information in order to make the life of future developers easier and for documentation purposes.

Cascading

One very helpful piece of syntactic sugar is the cascade operator .. that I have yet to see in any other programming language.

new Ellipse(10, 20)..rotation = 45 * PI / 180
                   ..color = 'rgb(0, 129, 198)'
                   ..outlineWidth = 0;

This operator enables cascading operations on an object without the need of it returning itself after every method call, which results in much cleaner code.

Futures

Support for futures is baked right into the language. The following example shows how to wait on the completion of two futures.

var futHttp = HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 4040);
var futRedis = RedisClient.connect('localhost:6379');

Future.wait([futHttp, futRedis])
  .then((List responses) {
    HttpServer server = responses[0];
    RedisClient redisClient = responses[1];

    // ... do stuff ...
  })
  .catchError((e) => print('An error occurred.'));

Client-Side Web Development

Here is a very basic example for client-side web development, lifted straight from an official tutorial.

void main() {
  querySelector('#inputName').onInput.listen(updateBadge);
}

void updateBadge(Event e) {
  querySelector('#badgeName').text = e.target.value;
}

There are also official ports of the popular JavaScript libraries Angular and Polymer for the creation of rich client-side web applications.

Package Manager

Dart comes with a package manager, called pub. The official repository is available under pub.dartlang.org. Dependencies are supplied in a pubspec.yaml file and can be installed via pub install.

name: Dartra
description: Small Dart demo application, created at Dart Flight School in Vienna on Feb 1st, 2014.
dependencies:
  redis_client: any

In many other languages the dependency management system is only an afterthought and is not provided by the language creators themselves. Usually this results in a bunch of community projects rallying against each other until one becomes the most adopted. I think that is a great decision by the creators of Dart because it will help to reduce friction and provide focus.

Documentation

I found the documentation to be excellent which unfortunately can not be said about a lot of other programming languages or related technologies. Check out the Dart by Example guide to get a further feel for how common things are achieved like handling HTTP requests and responses, using Websockets, or working with files.

Conclusion

It’s very obvious that Dart and its ecosystem are still in a very early stage. Even Chrome does not ship with the Dart VM yet. Furthermore, App Engine does not support Dart (or JavaScript) which is why the official Dart documentation even recommends Heroku as a hosting provider for server-side applications.

Personally I doubt that any other browser than Chrome will ever ship with a Dart VM. This is not a huge problem considering that the dart2js compiler is a big priority of the Dart team, but it does mean that most users on the client-side won’t see any performance gains if an application is developed in Dart instead of JavaScript.

Rumor has it that Google is looking into replacing Java with Dart for native Android development because Java is now owned by Oracle. This could turn out to be something very interesting but is most likely still a few years away.

I really like Dart and can attest to the fact that the familiar syntax enables one to be productive quickly. The standard libraries seem thought out and are well documented. However, there are some things that bug me like the lack of proper access modifiers and the mandatory semicolons among others.

I’m curious to see how the Dart story will play out. The ground is laid and if Google pushes the language it could see a widespread adoption which I’m certainly rooting for. Especially for client-side web development, it’s a much nicer experience for developers than JavaScript.