Member-only story
Types and Specifications
Previous in the series: https://medium.com/@robert_70579/playing-the-wrong-game-32745fa3bac7
Just because you’ve counted all the trees doesn’t mean you’ve seen the forest. — Anonymous
In my previous post about testing I made the case for setting a different metric for how testing can be used to create high quality code. Using the goal of high coverage is not a sufficient metric to drive good software but what is? Fortunately coverage is not the only tool in our toolbox.
One of the first tools used by languages to put guarantees on the behavior of programs was the type system. With it, invalid operations can be caught at compile time and in more in sophisticated languages polymorphic types can describe the behavior of objects and operations while letting implementation details be determined separately. Types write a contract between the objects and functions of a program that can be enforced by the system leaving the developer to think about other concerns. The way this is done in most languages, though, can create confusion and boiler plate code (I’m looking at you Java abstract classes). Type systems solve some problems while generating new ones and although the net effect is positive they still leave something to be desired.
While a type system, especially one that allows polymorphic interfaces, is a powerful tool it isn’t quite enough to guarantee correctness. Some languages have chosen to discard the type system but others have created a new beast that has many of the advantages of types and unlocks a powerful new set of tools. While this transformation has occurred in many languages I will illustrate in Clojure.
Let’s start with a simple function to take any string formatted day from perhaps a JSON payload and returns if it is my birthday.

Seems reasonable right? Two valid inputs that showed the code produces the expected results. Not really, though, the function is taking an arbitrary function argument and making several assumptions about it.
That exception was not covered by the original tests despite what a coverage tool may say. If it made it to…