Thursday, 11 December 2014

Book Review - 'Mastering Dart'

There's a steadily growing number of Dart books. Is 'Mastering Dart?' by Sergey Akopkokhyants worthy of a place on your bookshelf? Find out in this review!

The book consists of twelve chapters over three hundred and thirty pages, covering a broad range of topics in detail. Topics covered include Async, Reflection, Isolates and Javascript Interoperation. This title goes well beyond the introductory level, even discussing the inner workings of the Dart VM.

It doesn't attempt to cover everything comprehensively - some of these topics (for example HTML5) could easily be entire books in themselves - so have your expectations set realistically there. However it does cover each topic well and in the context of Dart. The chapter on classes feel a little basic at first but it soon goes deeper into Mixins and Generics. Most chapters waste no time at all and go straight into the deep end. No chapter feels like a rushed addition so that another buzzword could be added to the cover!

The layout is pleasing to the eye and clear. The supporting website is also well laid out and the code ZIP file can be downloaded. The credits page shows a good number of eyeballs have worked on this volume including four technical reviewers which boosts confidence in the accuracy of the text. I also appreciated the 12 page index!

I only have a few very minor quibbles with the book - a couple diagrams of the microtasks and event queues would have gone a long way. Also I spotted a few minor typos but nothing major for a first edition.

In summary, this is a good addition to any programmer who has had enough of introductory texts and wants to go deeper with Dart and understand some of its internal workings. It is very readable and I will keep it close to hand. Recommended.

(Packt Publishing were very helpful in providing an electronic version of the book for me to review. This is a genuine review and not an advert.)

Monday, 8 December 2014

Munching Squares

Munching Squares is a graphical that goes back to the PDP-1

The code (available on GitHub) is very simple and easy to experiment with:

  for (int x = 0; x < 256; x++) {
    for (int y = 0; y < 256; y++) {

      ch = (x ^ y) % 256;

      c2d..fillStyle = "rgb(0, 0, $ch)"
         ..fillRect(x, y, w, w)
         ..fillStyle = "rgb($ch, 0, 0)"
         ..fillRect(256 + x, y, w, w)
         ..fillStyle = "rgb(0, $ch, 0)"
         ..fillRect(x, 256 + y, w, w)
         ..fillStyle = "rgb($ch, $ch, 0)"
         ..fillRect(256 + x, 256 + y, w, w);
Changing the x^y to have constants or a different operator yields different results. As this is rather a static demo, I have not included a live Dart version this time. If you want a couple of exercises, try cycling the colours using a timer or sliders to adjust the size. Have fun with it!

Update Thanks for the suggestion from Seth Ladd to use the cascade operator '..' which helps make the code much more readable!

Thursday, 4 December 2014

Coming Soon On The Blog and Dart Releases

After spending November writing a novel, it is great to get back to spending time with Dart! Coming up on the blog this month will be a review of 'Mastering Dart' published by Packt. Also there will be some colourful Munching Squares.

Dart 1.8 was released just days ago but, in keeping with the rolling development, 1.9 has already appeared in the dev channel. Dart 1.8 includes experimental support for ENUM's.

Friday, 24 October 2014

Writing A Writing Tool - Part Two

Wow - Part One got a few visitors! Thanks for popping by - now on with Part Two.

Seth Ladd from the Dart team dropped a comment on the code - you'll notice all fields are lower case now :-) This was an easy change thanks to Dart Editor's built in refactoring. I have added more fields to the Chapter class and we can now create a chapter based on just the title. Named constructors coming in handy here. Two methods on Novel allow adding of Chapters and they return a reference for adding more details.

class Novel {

  String title;
  String author;
  List chapters;
  Chapter recent;

  Novel(this.title, {
    chapters = new List();

  Chapter addPlaceholder(Title) {
    var c = new Chapter.minimal(title);
    recent = c;
    return c;

  Chapter addChapter(Title, Summary, Characters) {
    var c = new Chapter.detailed(Title, Summary, Characters);
    recent = c;
    return c;

class Chapter {
  String title = "TODO";
  String summary = "TODO";
  String characters = "TODO";
  String pacing = "TODO";
  String atmosphere = "TODO";
  String length = "TODO";
  String locations = "TODO";

  Chapter.detailed(this.title, this.summary, this.characters);

Of course, our presentation layer needs a little update for all these new fields:

void txtOut(Novel n) {

  print("${n.chapters.length} Chapters");
  n.chapters.forEach((c) {
    print("~" * 80);
    print("- " * 40);
    print("CHARACTERS : " + c.characters);
    print("LOCATIONS  : " + c.locations);
    print("PACING     : " + c.pacing);
    print("ATMOSPHERE : " + c.atmosphere);
    print("LENGTH     : " + c.length);

The test novel needs expand to use the new methods and fields.

void main() {

  print("Novel Planner v0.2");


  Novel n = new Novel("Dart Chronicles", "D. Art");
  Chapter cur;

  cur = n.addChapter("On A Boat", "Sailing the sea and meeting the crew.", "Sam the Sailor");
  cur.pacing = "Lazy";
  cur.atmosphere = "Inviting";
  cur.length = "short";
  cur.locations = "The North Sea";

  cur = n.addChapter("On A Plane", "Flying High and introduce ticking bomb plot.", "Petra the Pilot");
  cur.pacing = "Rapid";
  cur.atmosphere = "Tense";
  cur.length = "medium";

  cur = n.addChapter("The Moonbase", "Introduces the new location and Rab the Robot.", "Rab the Robot");
  cur.pacing = "Slow";
  cur.atmosphere = "Sterile";
  cur.length = "long";
  cur.locations = "Earth's Moon";

  cur = n.addPlaceholder("Epilogue");


Grab the code from Github -

Right, I think that is looking good now. I am going to feed in my current novel plan. Part Three will be based on tweaks from using it for real and suggestions from comments. Dart has been a very productive language to write this in - glad I chose it over Python for this task. Happy coding and novel writing!

Thursday, 23 October 2014

Writing A Writing Tool - Part One

This is first in a series of blog posts where I will blog the creation of a simple writing tool as a Dart command line application. The background to this is NaNoWriMo which challenges anyone in the world to write a 50,000 word novel during November. Luckily you are allowed to plan in advance (there is not last minute revealed theme or requirement). I started with pen and paper which has its benefits but is too static for my liking! In the past I have written presentation materials in Python to escape Powerpoint and don't want to live in a database or spreadsheet for storytelling.

Important : This series will probably not have any challenging code but is a real world problem for me! Sorry to bore the experts :-)

Okay to get started we will need a basic data structure.

class Novel
  String Title;
  String Author;
  List Chapters;

  Novel(this.Title, this.Author){
    Chapters = new List();

  void addChapter(Title, Summary, Characters ){  
    Chapters.add(new Chapter(Title, Summary, Characters));

class Chapter
  String Title = "TODO";
  String Summary = "TODO";
  String Characters = "TODO";


We can output using print - maybe HTML later.

void txtOut(Novel n)
  n.Chapters.forEach( (c) {
    print("~" * 80);
    print("- " * 40);
    print("CHARACTERS : " + c.Characters);
  } );

Finally we can create a 2 chapter novel very easily!

void main() {

  print("Novel Planner v0.1");

  Novel n = new Novel("Dart Chronicles", "D. Art");

  n.addChapter("On A Boat", "Sailing the sea and meeting the crew.", "Sam the Sailor");
  n.addChapter("On A Plane", "Flying High and introduce ticking bomb plot.", "Petra the Pilot");


Our novel plan so far:

Novel Planner v0.1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Sailing the sea and meeting the crew.

CHARACTERS : Sam the Sailor

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Flying High and introduce ticking bomb plot.

CHARACTERS : Petra the Pilot

I did say this is very simple so I hope Dart newbies can chip in! All code will be kept on GitHub. I welcome patches.

Grab the code from Github -

Next iteration will add more fields for a chapter and improve the output. Already I like it - I can fiddle with details and the order but still get a neat output each time.

Saturday, 27 September 2014

Drone Onslaught Game

Hello! 'Diving Into Dart' has been on hiatus but I am getting things line up to get it going again. I happened upon my first ever Dart project - a conversion of my Python game Drone Onslaught. Unfinished but playable! Hope you enjoy it. Code should appear somewhere soon is on github.

How to play : Use Arrow Keys To Move and 'S' to fire. Click here to play.

Sunday, 13 April 2014

Dart 1.3 Released

Another big performance jump for Dart - this time on the server.

"Dart News & Updates: Dart 1.3 dramatically improves server-side perform...: With Dart SDK 1.3, we’ve improved performance of async code running in the Dart VM and made substantial improvements to dart:io."

It has been a while since this blog covered server side code - hope to have a sample up soon covering a simple app.

Tuesday, 1 April 2014

Conway's Game Of Life

The classic computer simulation of Conway's Game of Life, still seems to be popular with programmers. It is not so much a game as a simulation. The action takes place of a grid with a selection of them being populated with cells. The grid is then updated to the next generation of cells. Cells make it to next generation based on a few rules such as number of neighbours. New cells can also be born. End result it a captivating pattern and repeated sequences of movement that has been used to produce music and video games.

I have stumbled across (and covered in this blog) Dart versions such as this one and this one. Rosetta Code provides src to another Dart version (as well as other languages).

Background to Conway's Game of Life is covered in this wikipedia article. The original article from Scientific American can be found here.

Here's the Diving Into Dart version! Of course, it had to be colourful so cells of different ages are shaded. This still looked a bit plain so I added 4 cultures of cells to the same display. To vary things still, the number of iterations is used too and my cells are round rather than square.

Live version is here and the source is here. Have a great game of life!

Friday, 21 February 2014

This Week In Dart #21

The road to Dart 1.2 continues with the release of to the DEV channel. Full announcement here. Highlights include the addition of Dart Formatter, increased performance and stability, plus more support for attributes.

Diego Rocha has a great article on the all important topic of concurrency - vital in today's multi-core world. Worker: Dart concurrent programming does not have to be hard. Even features Fibonacci numbers!

cozy-porcupine. There - I said it! As you this is the codename for the latest angular.dart release which is at 0.9.8 so approaching 1.0 sometime soon. All the details over on Github.

I always enjoy talks from the Dart team themselves. Bob Nystrom's recent talk 'Dart for the Language Enthusiast' is available as an MP3 download and the slides are available too.

If you are interested in Dart game development, there's some interesting early footage of Fredrik Bornander's 'Lost Souls'

Bruno Skvorc has written a great article A Client-Server Dart App From Scratch. This is part one of a series starting with setting up a development environment and a Vagrant VM. SQL and PHPStorm are also touched upon.

Finally, I'd just like to mention that this blog should be picking up the pace soon. There's some more demo's on the way and a game. Have fun building with Dart this week! Thanks for reading - the visits, sharing and responses make it all worthwhile.

Thursday, 6 February 2014

Sierpinski Carpet

Returning to the world of fractals, we travel back nearly 100 years to the Sierpinski Carpet which was first described in 1916. Wacław Sierpinski was a Polish mathematician who made a great contribution to Set Theory. Not just carpets - the Sierpinski Curve and Sierpinski Triangle are the the other two famous fractals that carry his name.

This recursive function is quite intensive involving dividing the X,Y values repeatedly until they hit a multiple of 3 or 0. At this point X,Y can be determined to be Carpet or non-Carpet. As this blog requires it's demo's to be colourful, the colour is determined based on the raw X,Y values rather than the Carpet factor! To add some interactivity, the colour can be shifted and the size of the output adjusted.

Live example is visible here and you can get the Dart code on GitHub.

Thursday, 23 January 2014

Dart 1.1 Release

Dart 1.1 was released on the 16thof January. Congratulations to the team and community! Official blog post is here.

This release came fairly soon after the 1.0 release and the emphasis has been on the speed increase of 25% in JS but also the expansion of server side Dart.

The release obtained some coverage in the tech press:

  1. Infoworld - covers the release and the usual Dart vs JS controversy.
  2. SDTimes - covered the release (and covered the article with and advert... ).
  3. Dr Dobbs - fairly detailed coverage.
  4. InfoQ - Covers 1.1 and ECMA.
  5. The Register - covers the release with some discussion of the language and details of the recent updates.

Over on the book press, Chris Strom's book Dart For Hipsters has been updated for 1.0/1.1.

Saturday, 11 January 2014

This Week In Dart #20

TWID is back for 2014 - hope you had a good break. Due to the holidays, this is a catch-up edition so may contain some older news.

The Dart Editor and SDK on the DEV channel were updated to 1.1.0-dev.5.6. The current push is towards a 1.1 release. The numerous updates are detailed here - dart:io and server side related functionality is moving forward.

Looking further out, Bob Nystrom from Google put out a proposal 'for handling directories outside of "web" in pub build and pub serve'. A document was also shared on G+ for pub build

Notch finished his Dart Game for Ludum Dare. Have fun playing 'Last Minute Christmas Chopping' and look at the source code too.

Sticking with WebGL and gaming, check out Alphabets - 'Alphabets is a game designed for young children learning alphabets.The game supports English, Finnish and Norwegian languages'.

Hope you are planning to build something great with Dart in 2014!