Dart for Hipsters
by Chris Strom
Version: P1.0 (June, 2012)
Copyright 2012 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Table of Contents
Copyright 2012, The Pragmatic Bookshelf.
Early Praise for Dart for Hipsters
A fun and easy read for anyone wanting to understand what Dart is and how to use it with current generation browsers. The commentary onfeatures planned for future releases of Dart is reason enough to buythis book.
At first I was somewhat skeptical of Dart. This book made me understand its promise, gave me a good idea of its current state, and will serve as a solid reference for me to lean on.
This is the first book on this exciting and promising programming language, a clear and approachable text that engages the reader and that certainly will contribute to Darts success. I particularly liked his treatment of the functional aspects of the language and the discussion of isolates.
Introduction
Why Dart?
When I ask that question, I do not wonder why Google is pursuing Dart. I am not asking what the language designers are hoping to accomplish. To be sure, we will touch on both those questions and many more in the course of this book.
When I ask Why Dart? it is a question meant for myself. What on Earth makes me think this is a good language to learn, let alone write an entire book about? Especially at the 0.08 release.
The answer to that question is a personal and professional journey to understand how to make the Internet as fast as possible. Back in the day, I was a simple Perl hacker. I rather liked the language and what I could do with it. But when Ruby and Ruby on Rails hit the scene, I jumped. The combination of simple, clean code and strong convention won me over. For a while.
Next, I explored smaller frameworks like Sinatra, which retain the beauty of the Ruby language but lead to smaller and faster code. The two frameworks sufficed as a nice continuum for web development, and I was happy.
But perhaps there was more? This eventually led me to Node.js and various JavaScript frameworks built on top of it. And it seemed that no more speed could possibly be eked out of servers.
And then I found the SPDY Here, at last, was an attempt not just to improve on what we have but to redefine the rules of the game.
One of the things that I noticed while working with SPDY was that no matter how much I took advantage of what the protocol offered, the ultimate speed of web applications was limited by how quickly the web page and client-side scripts, CSS, and so on, could be evaluated.
JavaScript has been around for seventeen years. When it was first introduced, there was no Web 2.0, no Ajax, no CSS, and very little client-side interactivity at all. When JavaScript first came out, the primary use case was validating client-side forms with alert boxes!
Over the next seventeen years, JavaScript the language has evolved from a proprietary language owned and slowly developed by Netscape Communications to a web standard that regularly adds new features. But as fast as a committee can add new features to the standard, the Web evolves infinitely faster.
And then along came Dart. Dart asks, given what we know about the Web today, how might we build JavaScript from scratch? How can it load and run as fast as possible? How can we write it so that we can easily define and load external libraries?
How can we make it easy for developers to write beautiful code?
If Dart is the answer to those many questions (and I will try to make the case that it is), then Dart is quite possibly the most exciting technology to come our way in a very long time.
And that is the answer to Why Dart?
Who Should Read This Book (Besides Hipsters)?
I am writing this book primarily for any developer looking to keep their JavaScript skills as fresh as possible. The best way to improve JavaScript skills is through practice and reading other peoples code. But sometimes it can radically help to see what the competition is up to. In this case, as we explore what Dart brings to the table, we can better understand the gaps in an admittedly wonderful language.
I also hope that this book will prove useful for the newly converted. Dart is already a worthy platform for building insanely fast web applications for todays web browsers. I hope that after reading this book, you will be well armed to produce the next generation of web applications.
This book should be of interest to developers learning languages for the sake of learning. I focus quite a bit on the Dart language, especially in those places that it surprised and delighted me.
And of course, hipsters should read as well. Dart is just different enough to make it intriguing to the typical language hipster and yet powerful enough to make it worthwhile for the hipster who hopes to change the world.
How the Book Is Organized
I am trying something different with this book. Rather than introducing slices of the language in each chapter, I bite off chunks. Each section starts with an actual Dart project, including some commentary on the choices being made. My goal in these sections is to leverage Darts avowed familiarity to make significant headway in giving a real feel for what the language is. Since these are real projects, they are great opportunities to point out Darts strengths and, yes, some of its weaknesses.
Each of these project chapters is followed by smaller topic-specific chapters that go into a bit more depth about aspects of the language. I use these to cover material that is too detailed for the project chapters and material that cannot be found in current Dart reference material.
So, if you want a quick introduction to the language, you can certainly start by reading the project chapters alone. If you want a more traditional book, then skip the project chapters and read just the topic chapters. Or read it allI will try to make it worth your time!
The first project is Chapter 1, .
The next project is taking the simple Ajax application from Chapter 1, .
Following that, we will take a look at dependency injection in Dart with Chapter 11, . Unlike JavaScript, Dart is not primarily a dynamic language, though as we see in that chapter, it is still possible to perform some tricks of traditional dynamic languages. The follow-up to that project is an introduction to Dart testing, which is an important topic, even if not quite baked in Dart.