A look at Dart from the eyes of an OO programmer
Join the DZone community and get the full member experience.
Join For FreeDart is a new language for client web development unveiled by Google in the last week. Let's try to do a balanced analysis of it, without the prejudice of being a JavaScript or Java developer.
The features... mostly Good Things(TM)
Most of Dart features were not in JavaScript (but may be in frameworks or other languages like CoffeeScript.) For example:
- a main() method which is executed at startup.
- Real classes and almost everything useful that Java does.
- Scala-like features, such as variables declared with mandatory var or final (val in scala) and Scala constructors.
Dart is not statically typed, although there are optional type hints like in PHP's case.
Some (understatement) of its features are taken from Java, one of Google's standard languages: for example, interfaces for collections are borrowed from the Java Collections Framework. But why invent yet another terminology?
Dart supports Java generics with their type checks, and easy-to-write getters and setters. However, it does not lack JavaScript power tools like first-class functions.
Innovative ideas
I can't remember seeing some ideas of Dart in any other language. Some are puzzling, while some are worth a second look.
A Factory class for each interface creates objects of a default class. Even int and double are just interfaces. This is indeed innovative, but it's early to say if it's actually useful.
Type checking implemented only for documentation and warning. PHP's type hints are the most similar concept I know: but they are checked at runtime and produce serious errors. Dart's type annotations instead produce compiler warnings, and are oriented also to documentation. This will avoid Java-esque complaints and boilerplate code auto generated just to make the compiler happy.
A checked mode is available for development, that works like PHP type hints and forces types to be respected.
Each page has a standard entry point: main() triggered by the DOMContentLoaded event.
The isolation of different <script> tags does not let you access variables declared in another script block (but you can load new code with special instructions like #source). This is only one of the moves to simplify JavaScript messes, like not supporting inline event handlers in HTML elements:
- a simplified DOM Api, which does not take into account XML support.
- query() and queryAll() methods for CSS-based selectors, borrowed from jQuery.
- DOM collection types are the language ones: lists, maps and sets. So a single Api can access both a map and and element's attributes.
- Definition of event listeners directly on DOM elements (element.on property). There are also add()/remove() primitives to avoid overwriting listeners.
The alternatives
If you want some more expressive power than plain JavaScript, there are plenty of alternatives more diffused than Dart:
- JavaScript frameworks such as Ext JS and jQuery.
- has a new syntax and idioms embedded into the language, and compiles to JavaScript.
- Traceur offers features of the future JavaScript available today.
- Flash, Java, Google
Web ToolkitGears... are all extensions comparable to Dart apart from the fact that they are not retrocompatible with JavaScript.
It's not clear whether Dart's main deployment platform is a browser supporting JavaScript or a Dart virtual machine. If the language is just oriented to its VM, it's an alternative to Flash and Java applets, not JavaScript.
However, the compiled examples run fine (I didn't perform quantitative benchmarks). I heard some people complaining about the size of compiled JavaScript coming out of Dart (17K lines for an Hello World example). However, this example comprehends a bunch of library functions; an equivalent jQuery-based Hello World "compiles" to 9K lines just with the core jQuery.js file.
On the server-side, the competition isn't really with JavaScript, but with Java, C#, PHP, and Ruby... The point of Dart could be about having a single language, more elegant than JavaScript, which run reliably on both tiers.
Other opinions
Dart's types shouldn't even be called types, since they're thrown away in compilation.
It's neither a static nor a dynamic language. But this article has been called FUD on Hacker News.
Ars Technica points out that the development process of JavaScript, which is improving as a language, is open, while Dart has been created behind closed doors. No way to craft a new web standard.
In fact, if Microsoft built Dart we would be out with torches and pitchforks. Well, they did create SilverLight, one of the most hated languages at least here in Italy; you cannot watch the national television on Linux because it's served via SilverLight. They did it with VBScript, already compared to Dart. It's a little better than Flash, since it probably can't not eat into battery life as much.
In synthesis, if you like Java and cannot code JavaScript, you can write Dart code. But it's up to Dart to prove itself: after this user response, it must provide a significant advantage over JavaScript for being adopted in real web applications, instead of being forgotten as yet another new language. I'm still glad there is competition and innovation at work in the browser's environment, JavaScript needs to improve just as HTML5 and CSS3 are doing.
Opinions expressed by DZone contributors are their own.
Comments