This Blog continues on http://aliafshar.github.io/blog

Thursday, November 24, 2011

Things I love about Dart (so far)

I've only been using Dart for a few hours in total, but already there are things to love about it. Forget the obvious "It's not JavaScript", because I don't hate JS, and if I did hate JS it would be because of the lack of decent structure (who decided prototypes were nice to use?). Certainly, I don't hate dynamically typed languages, and in fact, I quite like them, and have built my career on them. This article is about small things, I'll leave that huge stuff for the clever people.

1. I already know Dart without having learned a single thing:


Ever used Java or C#? Dart is essentially the same as those familiar languages that we learned at school. A class is defined like:

class Banana {
  String getColor() {
      return "yellow";
  }
}

See, this could be any language you already know, and to illustrate that nicely the syntax highlighter treats it as Java, and no one really cares.

2. Class members are public by default:


If you define an instance variable like this:

class Banana {
  int age;
}

then anyone using an instance of the Banana class is free to read and write to that variable. "OMG you just broke encapsulation!". No, I didn't, you are free to define private variables if you like by prefixing them with the _ (underscore) character (not sure how much I love that syntax, but still). Who in the Java world decided that exposing an attribute is a bad thing to do? It's just part of the external interface of a class, use it as such, and move on with life having escaped 1. writing setAge and getAge, and 2. feeling dirty about using public.

3. Initializing and defining instance variables:


class Banana {
  int age, length, width, ends = 2;
}

Look how I have defined the types of all those variables and actually initialized the value of one of them to 2 — a banana has two ends, usually. These can be assigned to something called "constant expressions", which I am a bit fuzzy about.

There is more; how annoying is the constructor pattern where you have a constructor that takes a number of variables, only to assign them to instance attributes: this.age = age or self.age = age etc.? It must account for something like 5% of all my code. Construct and assign, construct and assign. Dart does this nicely, look here:

class Banana {
  int age;

  Banana(this.age);
}

The above class will assign the passed age to the instance variable on construction.

4. Top-level attributes and multiple classes per file:


I love these in Dart, but mostly because I hate the lack of them in Java. C# somehow gets away with having namespaces, but Python does fine, and Dart follows the Python model. Want 27,000 classes in a single file? be my guest. Want to have some constants defined at the top level of a module instead of in a useless static class, also be my guest.

5. Factory constructors:


These are constructors that actually return new instances. Something like Python's __new__ although that is slightly painful to use in Python, and not something you would wish on someone you like. Dart does it properly with the use of the factory keyword to indicate that a constructor will return a new instance. It is still a constructor so when creating an instance, the new keyword is used; which differs from using a static method to achieve the same thing, as you would in Java, look how:

class Banana {
  Banana._internal();

  factory Banana() {
    return new Banana._internal();
  }
}

The _internal constructor here is a private named constructor which the Banana constructor calls. This is great for doing nasty things like caching instances transparently from the caller of the API.

6. It's not JavaScript:


Just kidding, you know I can't resist trolling! But anyway, try Dart, it's fun, refreshing, safe, and as I said, you already know it. Read more about idiomatic dart if you are interested in the advisable ways of doing things.

Comments on Google+