2008-10-04 Programmer’s Block and Unit Testing

It all started with Radomir Dopieralski asking me how to get around “programmer’s block” on the Oddmuse channel, #oddmuse.

Radomir Dopieralski

#oddmuse

Here is what I wrote back:

First: I don’t code for weeks. That’s the beauty of free software, I guess.

But what about that dread feeling when you look at your code and think that you ought to be fixing something but it just makes you tired just to think about it?

This is tricky. I’ve been at that point several times. I felt down when I had to rewrite a large section of my unit tests when I realized that I had to use XPath instead of simple regular expressions. I felt down when I had to dump my attempts at rewriting the Recent Changes code. I had to do that at least twice.

XPath

I discovered that writing unit tests and modularizing my code (moving things into plugins) helped a lot against that feeling of hopelessness.

It helps because you there are no subtle fears and doubts lingering in the back of your mind. If there is doubt, write a test to dispell it. If you can’t, rewrite the code until you can write the tests you need. That part is important. Tests start to drive code organization.

It’s important to get that feeling of closure. Do something, verify that it works, and put it behind you. There’s no need to look back; no need to worry. That’s what unit tests can get you.

I remember starting to write unit tests when Oddmuse already felt like a big project. There was no way I would ever achieve decent code coverage. I don’t have the time to write tests for testing’s sake. So I started writing the tests I needed right then and there. If there was a bug, I wrote a test to prove the bug, fixed it, and kept the test. If I wrote something weird and complicated, I wrote some tests to prove that it worked. I don’t write tests to check every option and every plugin combination. But when I’m worried, I’ll write a test about the stuff that worries me.

“Worries lead to uncertainty. Uncertainty leads to doubt. And doubt leads to suffering.”

I was lucky. Perl CGI scripts can be called from the command line. But you can’t use all the HTTP stuff from the command line. That’s why my atom tests require the script to be installed at localhost/cgi-bin/wiki.pl. If it is, I run more tests involving actual HTTP requests. It’s slow, but it works.

There’s also an excellent XML library that allows me to write XPath expressions to test my HTML output. That was also an important step.

Start small, and keep in mind that unit tests aren’t there to prove anything. They’re there to divide and conquer the code. Do something, then move on and don’t look back. Unit tests help you fight bit-rot.

bit-rot

​#Oddmuse ​#Testing ​#Programming

Comments

(Please contact me if you want to remove your comment.)

wonderful citation from star-wars! Greetings from nabu dude 😄

– Dom 2008-10-05 13:17 UTC

Dom

---

Remember days in Orlando and watching Episode 1 several times!? Those were the days, hahaha.

– Alex Schroeder 2008-10-05 13:33 UTC

Alex Schroeder