Wednesday, October 20, 2010

I Promise I Will Test My Code

I never thought I would say this, but today I had a revealing experience, which is forcing me to admit. You really really need to test your code.

This was an application written long ago. It was a Java desktop application using Swing. It was very small and we, as usual, had no time. It was already after deadline when we started. It was coded and not tested and delivered to the client. They did UAT, came up with some issues. We fixed them. And that was the end of the story.

Just a few days back we needed that application for sending to the client as a sample code. I downloaded the thing from our version controller. Dusted it a bit. It looked good. I reviewed the code and was impressed that even in such a hurry it was nicely written and commented. Pleased, I thought I had some time to make it real nice. I thought I will add some JUnit tests to impress the prospective clients. I wrote the tests at first with quite some effort, but after 30 minutes or so I got the hang of it. Even then I thought it was stupid and waste of time. But when I started running the tests, creating some entities, updating them and deleting them, it ran successfully the first time. From the second time I got an error from the creation use case. And I found out we weren't actually cleaning the things properly in the delete method. Some details data wasn't getting deleted. I was truly amazed and promised myself that I would write tests for each line of code I write in future.