Wednesday, May 2, 2012

Dart



Background

For web developers today, there is a relatively small range of tools for client side coding. Most developers turn to Javascript, which is supported well by all major browsers and has very many libraries written for it such as jQuery. Some developers choose to use Flash or Silverlight, but those are on the decline as it appears that Microsoft is switching over to Javascript with their Windows 8 runtime, Apple still refuses to support Flash, and Adobe just announced that they will stop supporting Flash for Linux machines. HTML5 and CSS3 aim to give developers more power with the client-side functionality that Javascript has, but they are not yet standard ready nor production ready, and they also don't have the sheer power that Javascript does. So at the end of the day, almost all client-side web developers are stuck using Javascript, they have little other choice.


Why Javascript Is Bad

Javascript may be the most commonly used client-side scripting language, but it has a lot of disadvantages that make it very difficult for programmers to use. For starters, Javascript is dynamically typed. This means that given a variable in Javascript called "windowSize", a person does not know if it is an integer, a string of text, or some other object. For small programs, this is a feature, as drafting out a program can be really quick when there's little text to write, and you know exactly what all of your variables are. But when programs start getting big dynamic typing starts to get painful. For example, look at this snippet of code:

 var i = 1;  
 // some code
 i = i + ""; // oops!
 // some more code 
 i + 1;  // evaluates to the String '11'
 i - 1;  // evaluates to the Number 0

The person originally had i as the number one. But then he added an empty string of text to the number. At this point, most languages would simply cause an error, but Javascript is fine with it. And after that, weird things start happening. i + 1 evaluates to the string of text '11', but i - 1 evaluates to the number zero.

That's a pretty bad scenario, but most of the time people won't make such silly mistakes in their own code. However, people will make such silly mistakes when they use other people's code. Look at this example:

 function substr(str, start, end) {
 
   if (arguments.length !== 3) {
     throw new Error();
   }
 
   if (typeof str !== "string" ||
       typeof start !== "number" ||
       typeof end !== "number") {
     throw new TypeError();
   }
 
   // actual code is commented out

 }

This is the kind of code that needs to be written every time someone writes a function in Javascript. The declared function clearly has 3 arguments, but the language doesn't stop people from using it with 2 or 4, so the programmer does a manual check that it has been given 3 arguments. And of course, because Javascript is dynamically typed, the programmer also had to check that the first argument is a string, and the other two arguments are numbers. If the programmer hadn't done this, then someone else might have been using their code and passed in a string instead of a number (like in the first example). And it might even happen, that the function would work, it would just do something very unexpected. This manual checking makes programming in Javascript painful and obnoxious, but if you don't do it, you're allowing your code to fail and work wrong without you noticing. A team of engineers at Google recognized these problems (and many more!) in Javascript, and strongly believed that that they were insolvable within the language. They decided that they had to make their own scripting language, similar to Javascript, but without all the mess and problems. They decided to call this language Dart.



Dart

The Dart language is like Javascript in the fact that it is designed for web scripting. Its syntax is very similar so that people that use Javascript would have an easier time switching to it. However Dart is much more friendly towards programmers. Dart is optionally typed, which means that like Javascript, the type of variables does not have to be declared. However, this does not mean that it works the same way. For example, if someone were to try to add a number to a string in Javascript, it would work but have unpredictable results. In Dart, it would cause an error, because doing a math operation with a number and text doesn't make any sense. The reason why it is called optional typing is because specifying the type is optional, so that if a programmer wants to, he can declare that a variable is a number, or a string of text, or a function. Then the dart editor checks this code for correctness while it is compiling, while Javascript can only be checked for correctness by being run.
document.queryAll('a.person').on.click.add((e) => print('Person clicked'));


One thing that makes Javascript so powerful is the jQuery library. jQuery makes it simple to scan through HTML documents, animate them, and handle events. Dart also has support for all the things that jQuery does, but they are built into the language rather than being part of a separate library. Dart also has support for classes and inheritance, something which Javascript tries to emulate this with something called prototypal inheritance, but it can noticeably decrease the performance of a Javascript program.

Javascript is unsafe by default, it has to be thoroughly tested before people can deploy it. Dart is safer, but it nothing can be perfectly safe in just compile time, so Dart also has two modes, production mode and checked mode. Production mode causes the Dart compiler to produce code that is optimized for speed that is considered ready to be deployed. Checked mode causes the Dart compiler to emit extra debugging code that helps testing of code so it can be deployed faster.

Dart also has support for generic programming. Combined with its support for classes and interfaces, Dart allows programmers to write generic container objects that can get compiled to very efficient code.

interface Cache<T> {
    T getByKey(String key);
    setByKey(String key, T value);
}


Dart has a lot of features that Javascript doesn't, such as generics and inheritance. But it is also a simpler language than Javascript. One controversial feature of Javascript is the eval() function, which can execute arbitrary Javascript code. This function is very powerful and also very dangerous. For example, a lot of malware hidden inside PDF files is actually embedded Javascript in the PDF that calls eval() on malicious code. 

Javascript has many flaws, which is why some folks over at Google decided that a new language was needed to replace it. And that is their aim, to replace Javascript, not to have a competing language. So far, Dart is still in development and the language is still changing, but it is changing for the better. Today, Javascript is pervasive in browsers. In two years, Dart might take its place.


References



No comments:

Post a Comment