Testing and Courage

Engineering Insights

Talin
Machine Words
Published in
2 min readJan 9, 2019

--

In the course of this series, I have several times waxed eloquent about the virtues of automated tests. The are many reasons why we write tests; from documenting your assumptions to ensuring that a future maintainer doesn’t muck things up.

One of the most important fruits of automated testing is courage.

What I mean is that a comprehensive suite of unit tests allows you to take risks that you would otherwise be afraid to take.

For example, suppose you have some particular function or class in your program that is particularly complex and hard to understand. It badly needs to be refactored. You’d love to be able to reach in and tear it’s beating heart out and replace it with a shiny new one.

Unfortunately, there’s a problem. This piece of code is a critical part of your system. It has to be bug-free, because if it isn’t, there’s going to be big, expensive consequences for you and your team.

And you worry that after the big refactoring, there could possibly be bugs. After all, the piece of code is complex. It has heavy responsibilities. You might screw something up. And, in your experience, there are frequently bugs in brand new code, because it takes time to find all of the mistakes.

So instead of doing the refactoring that you have longed to do, you leave well enough alone. You let sleeping dogs lie. Despite how much it pains you, you live with the substandard, messy code for years, just patching it and tweaking it around the edges, because you are worried about what might happen if you re-wrote the whole thing.

But now imagine that this piece of code has a comprehensive test suite. Every aspect of it’s operation is tested.

What a difference! Now, you can rip out that old code and replace it with complete confidence. You laugh manically, typing at a furious pace, as you target and destroy that crufty old code. Back to your graves, you fiends!

You know that the new code won’t have hidden bugs, because the tests would have caught them during development. You can have the courage to do the right thing.

It means that instead of being forced to crawl, you can fly.

See also

--

--

Talin
Machine Words

I’m not a mad scientist. I’m a mad natural philosopher.