What is the most expressive, concise, functional way to learn design patterns? Compendious Thunks, of course! With… Dart?
Dart is the absolute best language for exploring design patterns. In fact, Dart may be the greatest language that you’ve never used.
You know Dart. It’s designed for familiarity. So even if you’ve never seen it before, you know Dart. It’s got the fundamental ingredients for classic design patterns: types, inheritance, interfaces and more. But what makes Dart special for learning patterns is that it is the most compendious, thunkiest language ever created.
So that’s the claim. Let’s see if I can back it up…
Consider a Robot class. It needs x and y integer coordinates. Dart constructors are the name of the class (
Robot), arguments declared in parentheses (x and y), and a body (which assigns the x-y arguments to the x and y instance variables).
We’ll need a getLocation method that reports the x-y coordinate pair. Open-quote, paren, close quote, plus, the x-coordinate, plus, open quote, comma, close quote, plus, the y-coordinate, plus, open quote, paren, close quote, and semi-colon. It’s a little ugly, but show me one language that’s actually got beautiful strings.
Whoops! Well, here’s a benefit of types. I can’t combine strings with integers like x and y. Well,
toString() to the rescue. Thanks to type warnings, I can fix silly mistakes like this immediately.
Dart runs code from a main entry point. In there, I declare a new robot variable,
r, and assign it to a new instance of Robot, starting at 0,0. Some print statements to demonstrate functionality of the robot and… the robot starts at 0,0. Working code: always nice!
So great, it’s familiar. The typing was nice, but, admittedly, the rest might not feel like that big a win. Certainly not compendious or thunky. Let’s keep playing…
So why types? For humans! Types found my mistake with
getLocation before I got too far astray. Plus, there’s code completion, if you go for that sort of thing, and nice doc generators. But really, it helps make code easier to read for people.
Next, note “this”. You almost never need it. Dart just knows that you’re talking about instance variables. Oh, and Dart is a language with beautiful strings — thanks to dollar-sign interpolation.
Dart also has nice affordances for common coding practices. Hash-rocket return makes for nice, small methods. Getter methods eliminate the need for silly method names like getLocation — the location getter looks just like a property. Nice!
Dart constructors are nothing short of glorious. Instead of passing variables only to perform a simple assignment, just declare the assignment in constructor arguments — no body needed. Optional parameters with default values — deceptively hard in languages that force you to roll your own — are also easy in Dart.
The end result, friends, is some compendious code.
So what about thunkiness? For that, let’s add some methods to move the robot right, left, up and down, adjusting the x and y coordinates accordingly.
If we move the robot right, the robot ends at (1,0) — x of 1 and y of 0. Move up instead and the robot ends at (0,1). No surprise there. But what’s really cool is that we can assign the method to a variable, invoke that variable and… still move the robot.
These are tear-off methods. We tore them off the object, but they retain awareness of the object. And, when we invoke the method, thunk! There we are right back in the original object’s context. That is super helpful in so many of the functional patterns that we use.
And, it turns out that this is our first pattern. As we’ll see in the first screencast of season 1, the
move variable is a Command object. It’s the combination of a receiver, the robot, and an action,
Look, Dart’s not perfect and maybe it’s just not for you or your current projects. And, as with any language or tool, there are gonna be annoyances. But I think they’re far outweighed by its features.
While nice, that’s not what make Dart a wonderful learning language. It’s the thoughtful, programmer-friendly design. In four minutes, we went from nothing to introducing our first design pattern — thanks to Dart’s compendious and thunky nature.
Dart is compendious. Dart is thunky. Dart is perfect for exploring design patterns, principles and wanton code play on Compendious Thunks. Please join me!