Friday, February 07, 2014

Dart: I'm probably doing it wrong...

...but it feels pretty good.

This file exists purely so that I can write code like this:

   AnimationBuilder.on(entity)
      ..animate('foo1', 1000)
      ..animate('foo2', 1000);

Instead of:

    new AnimationBuilder(entity)
      ..animate('foo1', 1000)
      ..animate('foo2', 1000)

Or, without the 'DSL wrapper' entirely:

   var ani = new Animation();
   ani.animationSteps.add(new AnimationStep('foo1', 1000));
   ani.animationSteps.add(new AnimationStep('foo2', 1000));


If I could have actually have a function called 'AnimationBuilder' that would call "new AnimationBuilder" under the hood, that would have been even better. The Nokogiri gem for Ruby does this: there's a class called Nokogiri and a function called Nokogiri. The function calls the class, so you can write code like "Nokogiri(xml_content).foo.bar" and just Get It Done.

I am kind of a stickler for these aesthetics issues. I want my code to look good, because it's 2014 and programming is still text-based and I have to be the one staring at the result for 8 hours a day. It doesn't matter that there is literally no savings in terms of characters: the code reads more naturally with the 'DSL' so that's what I went with. That third block of code is simply not an option, but I included it anyway because I actually wrote it once before I recoiled and said "nope" and introduced AnimationBuilder.

I'm probably dragging some of my Rubyisms into Dart, but I figure I'm an odd duck out anyway (everyone seems to be coming to Dart from C# or Java), so whatever.

Gotta have fun, or why code at all?

No comments: