Skip to content

Instantly share code, notes, and snippets.

@DanielBMarkham
Last active January 21, 2020 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielBMarkham/ec9dee4903bb22615c2d22b73928999f to your computer and use it in GitHub Desktop.
Save DanielBMarkham/ec9dee4903bb22615c2d22b73928999f to your computer and use it in GitHub Desktop.
Oversimplifying is always wrong, but many times it's useful

YAGNI is an old term from Extreme Programming. It stands for "You Ain't Going To Need It". What the acronym tries to teach is that many, many times, we add new features, UI, or other code structures simply because we want to, not because we have to.

In the programming community, we've long had an argument over static typing versus dynamic typing.

Dynamic typing in many ways is great. You just declare variable names and use them as you want to. It frees the programmer from having to figure out every place and every way a variable may be used. Instead they just use it. I love it for rapid prototyping, for testing, and for a dozen other use cases. Yes, sometimes it drives me crazy, but all programming is like that.

Static, or Strong typing, is great. You just delcare a variable and tell the computer exactly what kind of variable it is. It frees the programmer from having to figure out every place and every way a variable may be used. Instead, if you use it in a bad way, the computer prevents it. I love it for more complex programming, for code I may return to years later, and for a dozen other use cases. Yes, sometimes it drives me crazy, but all programming is like that.

Note the parallel construction in the last two paragraphs. There are many good things to be said about both kinds of typing, which means the area is ripe for lots of "discussion" in the community.

Lately I've been writing pure functional code in an environment where I can choose which kinds of typing I want, writing microservices. And you know what? I use both. Nort only do I use both, but I think my way of using both should be the default for people who write this kind of code.

Please allow me to oversimplify to make a point. There are three and only three kinds of statements in a programming language.

  • Assignment of a value to a variable
  • Interacting with something outside the immediate scope of the code (return from function, monads, mutable deep state, etc)
  • Changing the control flow (branching)

There may be a bunch more, say when you evaluate a complex function as part of a branching statement. In this case you're assigning to a variable, it's just an anonymous one. (You're actually calling an anonymous function here too, but I digress. Remember I am oversimplifying)

In pure FP, assignment is done once and only once. In addition, aside from a return value or some kind of monad, any in or out changes are verbotten. Finally, and this is key, you can couple branching directly into types such that types control branching. (I am not talking about the functional equivalent of polymophism, it's better than that)

This means, as many have figured out, that the types can effectively be the code. That is, you can create a type system such that it is impossible for the code to be in an illegal or unforeseen state. Of course, you could always do this in C++ or one of the other build-it-all-from-scratch programming systems, but it was butt-ugly to do.

But why do that? Or more to the point, who the hell wants to lock down all of his code such that any change is a huge pain in the ass? Not me. But I don't want to walk away from any benefits. I want my cake and want to eat it too, dang it.

Incremental Strong Typing

Instead, I'm working with what I call Incremental Strong Typing, which is adduing just enough strong typing in at the right moment to build solutions with minimal code that work every time and can be easily maintained years later. I started on a video series to show how this would work in action. Here's the intro.

I've had this discussion many times, and I've fallen in love with various languages that handle either static or dynamic typing. It seems to me there are at least three topics that get conflated into one discussion here.

  • What is the world like? That is, can I faithfully represent the world inside a static type system? I think the answer is no, Category Theory seems to clearly show that, but I admit to being weak on CT.

  • What should my code be like? That is, what types of code do I want to create and look at while I’m programming? This is what most of the discussion on larger forums like HN is about.

  • What is the frickin’ actual problem I’m trying to solve? This is what I'm trying to get to. This is YAGNI. I don’t care about anything else in the universe aside from “what is this thing here and what do I have to do to it?” The static-vs-dynamic type discussion seems to be around what sorts of superstructures you want around executing this solution. Many times we lose track of just actually solving the problem by getting caught up in #1 or #2

We need incremental strong typing. That is, we need enough type control over our data in order to do one small thing and only that. Once we start modeling the universe or debating what makes good code we lose track of that. We end up debating hammers instead of fixing the roof.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment