Thursday, September 25, 2003

Sep 25 2003

Test-Driven Programming

So. Let’s say you want to write a program.

First, let’s assume you know what you want to write, and you’ve broken the intended program down into small pieces of functionality. Index cards work well for this; you can write a description of each bit of functionality on an index card.

So. You have a specific bit of code to write. The natural urge is to dive right in and begin coding it.

But what if you could build a testing framework, that simplifies the code that you have to write? Hmmm. “Testing framework” sounds awfully heavy-handed; I mean a little set of tests. Just something that can give you a thumbs-up when the code works.

Why would you want to do this? You know when it’s finished, right? Well, do you? Have you ever forgotten to implement some aspect of the code?

Also, how do you know if everything else works with this new bit of code? Wouldn’t it be nice if you had tests that would tell you?

Even better, wouldn’t it be nice if those tests were completely automated? Wouldn’t it be nice if you could tests every piece of functionality in your entire application with a few keystrokes? I do.

So, let’s do that. Make up a name for the function/subroutine/method/whatever you’re about to write. Create a little program that calls that function, sends it some input, and tests its output.

Run your test. Golly, it doesn’t work! Better write that function. As you develop, keep running your test, to see what works. When the test runs without failing, hey cool! You’re done.

Now, here’s where things get fun. Do the same for the next function you write, but put those tests in the same test program. In other words, run all your tests every time.

Imagine. Before you know it, you’ll have a test suite that covers every piece of your system.

Writing Thoughts

Just uploaded my latest writing exercise, The Last Train. Warning: it’s not very good; it’s very much an exercise. But it sets a mood nicely, in my opinion.

No responses yet

Leave a Reply

I work for Amazon. The content on this site is my own and doesn’t necessarily represent Amazon’s position.