Tuesday, May 6, 2014

Dart Is Much Saner Than It's Pre-Alpha Days


Working through bugs and test failures to produce the next edition of Dart for Hipsters, I have come across more than a few that are pretty trivial. And then there are those that are not...

Actually, I hope tonight's fall into the former category (they never do though). First up, I wonder if this bit of Dart is still necessary:
  template(list) {
    // This is silly, but [].forEach is broke
    if (list.length == 0) return '';

    var html = '';
    list.forEach((comic) {
      html = html + _singleComicBookTemplate(comic);
    });
    return html;
  }
For a guy that blogs just about every random thought that he has regarding code, I cannot for the life of me find when I decided that forEach() was broken like that. I definitely remember the pain of it, though. It also lives on in the Dart Comics sample app that (loosely) accompanies the book. Anyhow, it turns out that whatever bug this was has since been solved as iterating over an empty list works just fine now:
  template(list) {
    // // This is silly, but [].forEach is broke
    // if (list.length == 0) return '';

    var list = [];

    list.forEach((comic) {
      html = html + _singleComicBookTemplate(comic);
    });
    return html;
  }
Of course, this is probably most concisely done as:
  template(list)=>
    list.map(_singleComicBookTemplate).join('');
But that is early code in the book, so I will likely stick with the more approachable, less “magical” version in that context.

Completely unrelated—aside from manifesting as another dartanalyzer hint—is the question of what to do with the dozen or so warnings about unused imports:
...
[hint] Unused import (/home/chris/repos/csdart/Book/code/compiling_javascript/web/scripts/HipsterModel.dart, line 4, col 8)
[hint] Unused import (/home/chris/repos/csdart/Book/code/compiling_javascript/web/scripts/HipsterView.dart, line 4, col 8)
[hint] Unused import (/home/chris/repos/csdart/Book/code/compiling_javascript/web/scripts/Views.Comics.dart, line 4, col 8)
[hint] Unused import (/home/chris/repos/csdart/Book/code/events/collection_event_target.dart, line 3, col 8)
[hint] Unused import (/home/chris/repos/csdart/Book/code/events/collection_events.dart, line 3, col 8)
...
This is one of those non-real world, but very much real book example problems that I just need to deal with as best I can. There is no good reason for live dart code to import convert and html libraries but not use them. In books and blogs, it often helps to give an overview of code without using anything:
library skeleton_example;

import 'dart:html';
import 'dart:convert';
main() {
  // Do stuff here
}
One option is to write a method that uses something from each of those libraries:
library skeleton_example;

import 'dart:html';
import 'dart:convert';
main() {
  // Do stuff here
}

_satisfyDartAnalyzer() {
  query('#foo');
  JSON.encode({'foo': 'bar'});
}
Although that does work, it feels wrong. The code is available for download and doing something like that—even with added comments seems like it will cause confusion for people and only satisfy my obsessive need for test and static type analysis to pass.

So, in the end, I opt to add each unused import that I am intentionally keeping to the list of files that are grep-ignored by my Bash test_runner.sh:
results=$(dartanalyzer test.dart 2>&1)
results_ignoring_ok_deprecations=$(
  echo "$results" | \
    grep -v "'query' is deprecated" | \
    grep -v "'queryAll' is deprecated" | \

    # Known / intentional errors:
    # ...

    # Known / intentional unused imports:
    grep -v "your_first_dart_app/web/scripts/skel.dart, line 4" | \
    grep -v "your_first_dart_app/web/scripts/skel.dart, line 5" | \

    grep -v "hints found.$"
)
echo "$results_ignoring_ok_deprecations"
# ...
Now I only need to work through the list of a dozen unused imports to decide which are legitimate and which should lose some import statements...


Day #56


No comments:

Post a Comment