Software engineering is hard, and I’m lazy. I strongly dislike unnecessary work. So, if code is going to be around for a while, I build it to be easy to maintain.

I work in object oriented languages, usually Dart, and a key consideration in such languages is whether to make data mutable or immutable. Many, myself included, recommend immutable data; see for example Effective Java item 15, “minimize mutability”.

Here’s a snippet of Dart code that illustrates why mutability can be problematic:

// Biggest first. var cities = ['Tokyo', 'Delhi', 'Shanghai', 'Mumbai', 'Beijing']; print('Biggest cities of the world in alphabetical…


Flutter and Flutter Web are generating plenty of buzz, and deservedly so; they are pushing the boundaries of UI development. Flutter is written in Dart, and Dart has just gained a number of features under the banner “UI as Code” that will bring joy to every Flutter developer’s day. These are exciting times.

But, wait! Not everything should move fast. Sometimes it pays to be meticulous, fussy, fastidious, finicky, or—dare I say it—pedantic. So, over at Dart’s package:pedantic, we’ve been slowly gathering a list of precisely correct lints that you can apply to your code.

Of course, to check lints…


Rejoice, all ye faithful, for Dart 2.0 is coming, and it has types.

This is Dart’s first major breaking change — hence the “2” — and it’s precisely because it’s a breaking change that it’s such an important development. Let’s dig into why that is. First, some Dart 1 code:

void main() {
cleanUp([new TempFile(), new BankAccount()]);
}
void cleanUp(List<TempFile> files) =>
files.forEach((f) => f.delete());
class TempFile {
void delete() => print('TempFile deleted.');
}
class BankAccount {
void delete() => print('BankAccount deleted. Whoops!');
}
> TempFile deleted.
> BankAccount deleted. Whoops!

The code is a little surprising.

Both “TempFile” and…


Dart’s built_value provides powerful, convenient immutable values for Dart. The values might be immutable, but the package hasn’t been standing still! In the six months since I last wrote about built_value there have been ten major improvements to the package.

I also spotted some interesting uses of built_value in the Dart community:

Anyway, without further ado…


I heard some are moving from JavaScript to Dart. Well, my journey was a bit different: I moved from Java to Dart. And, part of adwords.google.com came with me.

AdWords is at the heart of Google’s revenue stream and is currently being rewritten in Dart. But the corner of AdWords I worked on — the part that sells skippable ads on YouTube — has been running Dart since 2014.

The interesting part of the story is that we weren’t told to use Dart, or even asked to use it; we saw what Dart had to offer and decided it was…


Building good end to end tests is hard. Having good end to end tests is wonderful. How do you get from the former to the latter?

Last week I introduced a simple chat server and client written in Dart. Half way through writing that code I decided to stop and add end to end tests — so they could help me make faster progress. It worked, and saved time overall.

Testing can be a big time sink; but I was sure it would work, because I’d used the same pattern before in similar code. …


Having thoroughly covered the basics of the “built” packages I’m now ready to up the ante by showing them in action.

I wrote this code in a day and a half to use as an example in my talk at the Dart summit (video). It was designed to show off built_value and source_gen, but it also shows something about Dart: that you can do a lot with just the SDK and the core packages from dart-lang.

The built_value chat example in action.

Getting the Code

You could read the code here but it’ll be more fun if you download and run it. To do that you’ll need dart and…


Last week I introduced built_value for immutable object models. We saw how to define object models in built_value; they’re immutable, easy to work with, and, if you like that sort of thing, a lot of fun.

This article covers the rest of the built_value package. The biggest item is that, as you may have guessed from the title, they’re also serializable.

Here’s what built_value serialization looks like to use:

// Value type defined using built_value.
abstract class Login implements Built<Login, LoginBuilder> {
// Add serialization support by defining this static getter.
static Serializer<Login> get serializer => _$loginSerializer;
...
}
//…


Last week I wrote about built_collection. I finished by remarking that to really make use of immutable collections, you need immutable values. So here we are: built_value. This is the second major piece behind my talk at Dart Developer Summit (video).

Value Types

The built_value package is for defining your own value types. The term has a precise meaning, but we use it informally to mean types where equality is based only on value. For example, numbers: my 3 is equal to your 3.

Not only that: my 3 will always equal your 3; it can’t change to be 4, or null…


A month ago I spoke at the Dart Developer Summit about object models with source_gen (video). I mentioned some packages and techniques that are highly deserving of a closer look, so here I am to tell the full story.

First up: built_collection.

The built_collection package provides SDK-like collections with immutability. Immutablity for:

  • Simplicity, convenience. An immutable collection can be passed around without worrying about who might modify it.
  • Performance. Mutability leads to expensive patterns like defensive copying and change detection.

The fact that built_collection is supposed to be convenient poses some challenges for the API. Let’s take a look. …

David Morgan

Software Engineer at Google; works on Dart.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store