Possibility and Probability

A Python programmer with a personality thinking about space exploration

11 July 2016

Flow: Why I like writing tests

by nickadmin

Tests are a contentious topic among programmers. Some really like them, others see them as a necessary evil. I think that they are important and if implemented correctly can improve both programmer productivity and code quality. Why? Flow. Flow. It is the state of being so “in-the-zone” that things just happen. Actions are fluid. Progress is constant. Things are happening. This is where the awesome happens. So why doesn’t this happen more frequently? Getting into “flow” can be quite challenging. With intellectually challenging work like programming, the slightest distractions can derail an hours worth of effort. Even just the threat of a distraction (“Can we have a meeting about this today? I’ll let you know when.”) can be enough to prevent a developer from entering the flow. This is where writing tests can help with developer productivity. The point of the test is to exercise some portion of your code. Think of the test as a codified version of all of the thinking that is done to create your code. By running the test you are checking your assumptions about the code without having to load all of those thoughts into your mind. And this is where the magic happens: Humans are great at remembering sweeping generalizations of things. When your tests run and pass, you are confirming your generalizations and assumptions about that code. This allows you to short circuit the need to completely reason about that code when you start working on new features. After all, if everything worked correctly to get us to this point, then you can just focus on that point. In other words, the tests limit the scope of things you need to think about allowing you to get into “flow” faster. The awesome byproduct of this is the tests themselves. In my experience, the mere presence of tests, that are of course run and maintained, tends to increase the quality of the code that the tests cover. This means you are getting better code and probably getting to write it faster thanks to getting into the flow sooner. And if you have something like a python flask app, tests can prove that your app is holding up its end of the REST contract. Interruptions can still disrupt your process, but I have found that simply going to the failing tests allows me to remind myself of what I was working on faster. What about you? Have you noticed that tests help you get into flow faster?