About confidence, code, and tests
Whether you're reading a classic like “Test-driven Development by Example” by Kent Beck or a yet another article on medium. When it's about testing, there's always some talk about confidence. That made me think.
Can you be confident about your code when you don't have tests?
The question seems ridiculous. Of course, you can. Millions of developers out there are. At least I was. Well, I still am. But something has changed.
Although I'm a big advocate of TDD, I wasn't born like that. Actually, I came pretty late to the party. I've started coding as a kid about 30 years ago and have been working for the past 20 years as a professional. But it wasn't until 10 years ago that I got into automated testing. Given that, I wrote a lot of code without any tests and for sure felt confident in doing so.
Precisely because I wrote a lot of code, I was able to build confidence. With every year, with every project and with every mistake I grew experience and with it came confidence. But still, nowadays I rarely write anything that's not test-driven. When I do, it's usually a one-off job or prototype. And while some people claim that they have a hard time coming up with a test first (or at all), I get anxious when I don't.
Tests have taught me one thing: Even though I feel confident that I won't break anything, I will. It might not happen often, but when it does, the red bar will hit me unexpectedly. It's a funny little feeling when it happens that is hard to describe. But I'll try it anyway:
Imagine having deployed a new release to production. Your phone rings just a few minutes later. It's your client yelling about something that's broken. Adrenaline kicks in. While he's still explaining the issue you know exactly what is broken, and why it's broken, and you hate yourself because it's such an obvious thing.
Now take away all the bad elements and you're left with a harmless little “gotcha”. The instant you see the red bar, you probably still know what's broken and why but it doesn't do any harm. You fix it and move on.
Let's get back to the original question. Can you be confident without tests? Of course, you can. But maybe you shouldn't. Reading about the percentage that automated tests may drop your defect rate is nice, but seeing that it actually does was a game-changer for me. There's a chance that I'm just a lousy programmer who is overly confident, but with TDD, I found something that works for me. Maybe you should give it a try, too.