Friday, March 6, 2015

Beautiful automated code formatting for Dart

I spend a lot of time reviewing Dart code. At Google, we use the Dart style guide, but we humans are poor at consistently following rules. I end up writing a lot of review comments like "put a newline here" and "remove this space". Although we have an automated formatter, dartfmt, to address this problem, I wasn’t happy with its output.

Over the past few months, I spent a bunch of time adding a sophisticated rule engine to the formatter, helping it choose how best to format a chunk of code. There are still improvements to make, but I believe the formatter is now good enough to be run on all of the world's Dart code.

We've been using this version of the formatter internally for a while, and people really like it. Everyone loves that it ends style debates and makes code reviews easier. Today, I'm happy to announce that the new formatter has landed in the dev channel SDK. Using it is as simple as:

    $ dartfmt -w <file or directory>

I'd love to have you try out the new formatter. Check out the docs, and if you have feedback, throw an issue my way.

– Bob Nystrom, Chief Formatting Officer