Marcos Placona
Marcos Placona
Developer evangelist at Twilio, GDE and in ❤️ with Open Source
9 min read

Categories

  • Dart

A few weeks ago, I went to a Google sponsored event called Dart Flight School. The aim is to promote the language by doing a road trip and presenting use-cases and samples. The presentations were brief, and mainly focused on discussing the language’s functionalities, and its seamless integration with AngularJS (also maintained by Google)

I had a chance to look at Dart before, and was interested in finding out more things about it. Turns out the language (and platform) are pretty slick, and the development tool-set is pretty complete. The IDE is pretty good (and free), and their package management system is pretty similar to NPM’s in NodeJS.

Dart is also very (very, very) fast, and is in fact faster than V8 (VM used by NodeJS). A performance comparison can be seen below.

Dart vs V8 Engine

Data gathered from https://www.dartlang.org/performance/

It comes bundled with an out of the box converter to JS, which means you can write your entire application in Dart, and then convert it to JavaScript. I must say I was initially sceptical about this conversion, but upon looking on the performance page, it seems even after conversion, JS generated by dart still manages to be faster than V8 (purple line above). The JS converter is a Dart application written in Dart, which means you could then convert it to a JS application using itself…. bewildering eh?

Language

From a language point of view, dart seemed to be very readable, and compliant with contemporary languages such as Java and C#. It is a class based language that allows you to fully use object orientation, and has some very nifty functionalities embedded in it, where you can for example define a method using shorthand syntax as such:

Data Structures

Its core library also provides you with Lists, Sets and Maps, which basically means no imports, as you get that straight out-of-the-box. So for example if you wanted to create a Map and iterate through it, you could simply do:

Unit Tests

A big part of writing great code lies with the ability to provide unit tests that will make sure your code remains awesome even after refactoring. With dart, you can as easily create unit tests by importing unittest

And obviously I could have just as easily grouped my tests in a single group to have them maybe organized in smaller units. I’ve also used shorthand syntax to define my tests here

Reflection

Is supported by a library called Mirrors (enough said?). Though I feel I haven’t played with that enough to give you any better example than this.

Futures

According to Dart’s own website, a Future represents a means for getting a value sometime in the future. When a function that returns a Future is invoked, two things happen:

  1. The function queues up work to be done and returns an uncompleted Future object immediately.
  2. Later, when a value is available, the Future object completes with that value (or with an error).

Why would I use Futures instead of simply calling my expensive processes and waiting for them to complete?

I like to think that the people who read my blog know better than asking the question above, as it makes me feel fuzzy and warm. However if you thought of asking this question but were ashamed to actually do so, I will take you through it, and we will pretend this never happened.

It turns out, that if you do that, you will lock the thread until your application becomes responsive again, which can range from a few milliseconds to God forbid a few seconds. Meaning your users will stay put (or most likely leave) until you finish processing their request and show them some meaningful content. Think of it as “waiting ‘till Friday is upon us”.

Seth Ladd gives a great example of the power a Future can have in your application

You can read more here.

Generics

Need I say anything? Need I?

Libraries

In this day and age, you want to be able to work with a language that offers you integrated package management. I have worked with numerous languages in the past, and managing third party packages has always been the pain of my life. The first time I looked at Ruby, I immediately fell in love with its package management system. Granted some languages try to accommodate for this by adding capabilities to builders such as Gradle, Ant or Maven. But I digress….

Dart comes with a package manager called Pub, which means all you need is a <abbr title=’YAML Ain’t Markup Language’ rel=’tooltip’>yaml</abbr> file inside your project where you can specify any libraries your project needs, as well as which version you would like to be locked to. that way, you only need to package your application with what it really needs, and all the external libraries will be downloaded on the time you deploy your project. This makes your application lean and easy to maintain.

A pubspec.yaml file would look something like:

Then run the following from terminal to download all dependencies:

Cool Factor

Dart is a cool language and very simple to pick up, but as with all the things in life (although you don’t always like to admit it), the known is always a lot simpler, and Dart strives to offer simplicity, which means if you have done any proper language in the past, you’ll be able to read and write Dart code with ease. Dart themselves state

“We did throw in some nice syntactic features such as this. constructor args and => for one-line functions, but we’d agree that Dart chooses familiarity over excitement”

Final Verdict

It’s very exciting to see such fresh language being supported and built by Google. In my opinion, the language offers everything the “cool kids platforms” offer and more. It also has capabilities that allow you to run Dart on the server side, client side and even natively on the browser. According to their documentation, the engineers behind Dart have an Android and Google App Engine integration on the back of their heads, and even though they say it’s not completely down to them, they mention on their FAQ’s you’d need to ask the team. But I’d say the fact they have thought of it is already half the battle won.

From a language perspective, I found nothing that would put me off writing code on it (and I’m pretty fussy about semantics). Instead I have found I genuinely enjoyed writing code in Dart, and was left with a nice after-taste after attending Dart Flight School. Even though I understand we had a much cut-down version of the even here in the UK.

From Here