Write testable code even if you don't write tests

17 Aug 2010 - By Karl Seguin

Testability is a misunderstood concept in software development. Most developers who don't practice it assume that its dependent on actually testing your code - say by writing unit tests. The truth is that its just a metric; much like how you might write maintainable code without actually having to maintain it. The difference between testability and more generalized quality metrics like maintainability is that testability is easy to measure.

If you're code isn't testable, then your code isn't good. It doesn't matter if you actually write tests or not. What is testable code? First and foremost, its loosely coupled, taking advantage of dependency injection (and auto-wiring), composition and interface-programming. Testable code is also readable - meaning it leverages single responsibility principle and Liskov substitution principle.

Isn't testable code just well designed code? Yes, but it also specifies a way to validate the design of your code: by writing tests. Its great to say that you should write maintainable code, but how do you verify that its actually maintainable? By maintaining it? Not very pro-active.

So doesn't that mean that testability is about writing tests? Yes and no (but mostly no). Someone who knows how to write testable code doesn't have to write tests. But initially, yes, you'll need to write tests. You should write tests to learn about flaws in your design and to learn how to be a better programmer. Trust me, you'll learn very quickly, and painfully, how to write quality code. Once you do, you'll know whether a class, method or behavior is testable just by looking at it.

There are other, very good reasons to write unit tests. But that's an entirely different discussion. We can flip the conversation around, and instead state that unit testing isn't just about testing that your code works, its also about testing that your design works. Either way, the point is the same.

For the first half decade of my career I often wondered if I was any good, and whether or not I was programming the right way. Hopefully you have too, because there's nothing worse than thinking you're doing it right. My advice to someone who finds themselves in that same position is to write tests and to learn from the mistakes that make your code untestable.

Honestly, making your code testable (by writing tests) might be the best advice you'll ever get on how to become a better programmer.

blog comments powered by Disqus