News for hackers. Stuff that matters.
Re:Ask YC: Does anybody enjoy unit testing their code? by allenbrunson on Thursday June 12, 2008 @01:19AM (#215676)
I really do enjoy unit testing.

Before I got the bug, I was wary of refactoring and adding new features. I would do a few simple tests when I originally wrote the code to satisfy myself that it worked. But then if I'd change the program flow or move things around, the benefit of that ad hoc testing was now lost, and I'd have to do it again.

Unit tests make me feel confident that the stuff I've got really works, even after I've modified it substantially. I'm a lot more likely to make big sweeping improvements because of it.

Re:Ask YC: Does anybody enjoy unit testing their code? by jamongkad on Thursday June 12, 2008 @01:41AM (#215695)
Your right, but dammit I think I'm just too damn lazy to write and test unit tests. I think that's my main problem...I'm lazy :-)
Re:Ask YC: Does anybody enjoy unit testing their code? by bhb on Thursday June 12, 2008 @10:08AM (#215936)
I actually enjoy writing unit tests _because_ I'm lazy :).

When I write a piece of Ruby code, I have a choice: I could either write a unit test or load the code into IRB and manually test it (or even load the real app and test the code in there).

For me, doing it manually is often more work. Let's say I have five tests in my head. My code passes the first four, but fails the fifth. After I fix the bug I found, I have to manually go back and test all five behaviors again. Unfortunately, my code often has more than one bug, so this process can take awhile. Once my code passes the tests, I always run the real app to make sure there are no bugs in my tests.

Plus, for me, writing code is just more fun than doing tedious manual testing, even if it takes a little longer initially.

As with everything, there are exceptions. If I'm fixing bugs, I tend to have a TDD approach, but if I'm doing exploratory programming, I tend to not write tests until the code is pretty stable. It's also hard to unit test GUI code, so think about the expected return on your time investment.

One thing to keep in mind: tests are more fun to write (and more importantly, more effective) as you get better at them. Writing tests is an art unto itself - just because you write good production code doesn't mean you'll instantly write great test code. It takes practice.

I sometimes hear people say, "Unit testing takes too time to write and maintain!" While there will always be some costs, I've found that as I've gotten better at writing tests, those costs have gone way down, while the benefit has gone up.

Re:Ask YC: Does anybody enjoy unit testing their code? by TimothyFitz on Thursday June 12, 2008 @04:46AM (#215771)
But that's exactly the point. You can be MORE lazy. You spend so much more time than you realize testing and re-testing your own code... doing monkey-level QA! Stop doing all that work. Be more lazy. Write your tests first!
Re:Ask YC: Does anybody enjoy unit testing their code? by jamongkad on Thursday June 12, 2008 @08:05AM (#215848)
Ha ha I guess you're right! Test Driven development right? I'll try to make some time for it.
Re:Ask YC: Does anybody enjoy unit testing their code? by DougBTX on Thursday June 12, 2008 @11:41AM (#216016)
You assume he tests his code manually in the first place ;-)
Re:Ask YC: Does anybody enjoy unit testing their code? by ojbyrne on Thursday June 12, 2008 @08:57AM (#215880)
A quote I bring up whenever I deal with job interviews where it's obvious the employer likes effort more than results:

"We will encourage you to develop the three great virtues of a programmer: laziness, impatience, and hubris." - Larry Wall