If you don’t write tests, you’d better write testable code

Petr Šíma
2 min readJul 19, 2017

--

I’m a promoter of new technologies. I’ll take any buzzword that I can attach to a new framework just to convince people that we need to start using it today. “Why do we need it again?” “It does X,Y and Z and it also makes the code more testable.”

Recently, I’ve been asked the same question by two people: “Well. Do you write tests? Knowing that I have never written an actual test beyond some framework setup helloworlds, I’ve had to come up with an excuse. And I think it’s a decent one:

If your code is testable, chances are it’s already correct. Or maybe better: If your code is testable and you think it’s correct, it’s probably already correct.

Actually, just to play it safe, let me add a third condition: If your code is good and testable and you think it’s correct, it’s probably already correct.

Your testable code is most likely also functional, and composable and all those other sweet words that help make the life of a programmer that much easier. Testability means flexibility during development. It means that you can take a chunk of your app’s code and run it in your defined context or tweak it in an Xcode Playground. Remember when you used to spend a quarter of your development time clicking through the app just to get to the screen you were working on?

If you write tests, good for you. If you have someone else write them, even better. If you have 2 people writing tests and 1 guy printing charts about code coverage, excellent. But when your programmer writes the code, they know which parts may break and why. Sometimes it’s hard to understand a problem, but you always know something about the code that you wrote to solve it. And your feelings about that code can range from “It compiles so it should work.” through “It doesn’t do all the things yet.” to “I can crash this.” If you feel like your code might not do the right thing in a certain context, just run it in that context, do it now. So if you are on a budget or can think of better things to do with your time, don’t feel bad about not writing tests. Just embrace functional programming, write testable code, and get it right on the first commit.

--

--