Flying With Python (Strong versus Weak Typing)

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Thu Mar 13 03:54:45 EST 2003


> From: Michael Hudson [mailto:mwh at python.net]
> 
> I think the claim most safely attributed to the XP way is that
> test-heavy development makes it *easier* to write correct programs,
> not that it makes it *easy*.  There's definitely an art to writing
> effective tests.

I don't claim to follow the XP way, but I am a strong believer in unit tests.

A good example follows:

Over the last week I've been modifying a C++ application. Specifically, I wanted to change the implementation whilst maintaining the interface. No unit tests existed for the piece of code.

I started hacking away, exploring for a bit. Of course, I never intended to keep the initial results of my explorations.

I did however find that I'd ended up implementing a fair slice of what I wanted to do, so i ran it, just to see what would happen.

Crash and burn.

At that point I reverted to the original code and wrote the unit tests for those parts of the API I wanted to keep. I made the unit tests as complete as possible.

Then I implemented the first part of the changes I needed. This changed the class from inheritance to composition whilst maintaining the same characteristics. After a while the unit tests passed and I was reasonably happy.

Then I implemented stub methods for my new internal functionality, and wrote the unit tests that would use them. Being C++, I couldn't just write the unit tests as I would have preferred.

Of course, the unit tests failed. So I started the modifications to make them work. Eventually they worked. I was happy.

Testing on the full application then revealed that my modifications had exposed an algorithmic weakness in part of the application - it ended up being the absolute worst case for my modifications. This prompted some more design and modifications.

But my unit tests failed! I'd forgotten to make some of the necessary changes. These were changes I'd actually worked out that I needed - I just forgot to put them in. It was immediately obvious where those changes needed to be made.

And it all worked.

I estimate that if I hadn't added the unit tests early on, I would have spent at least double the time chasing down obscure bugs.

Tim Delaney





More information about the Python-list mailing list