The old-fashioned style of programming is a three-stage process: Design, Program, Test. By that, I should be designing for a few more weeks, but I’ve already started writing some code.
I’m trying a newer paradigm that suggests that you write some test cases first - basically write the tests for what you want the program to do. Then write some code that satisfies the tests.
Finally think about what else the program might have to do (which drives another round of writing test cases and then writing code to pass those tests). Test, Program, Design.
The idea is that it is easier to refactor the code as you understand it than try to buld to a grand design.
It’s called ‘Test Driven Development’ (TDD). It is an evolution of the ‘Extreme Programming’ disciplines that we used a few years ago.
I’m not very good at it. I’ve absorbed the ‘Extreme’ ideas of liberally sprinkling Asserts into the code and leaving them there, and being able to run test cases at the push of a button. But I can’t get the hang of writing the test cases before you design, it just feels really weird.
Maybe it’s because modern programming isn’t really about writing code, but about correctly applying functions from some massive framework. In modern programming languages, the real work is done by the libraries, often in mysterious ways.
And maybe it’s because I’m too old and weather-beaten. But I’m going to give it a try anyhow.
I spent about two hours trying to install a popular testing framework called SimpleTest. I’m planning to write Agenda as a component of Joomla! (I’ll talk about that soon). So I created a skeleton component for Joomla!, and installed SimpleTest in it.
But I couldn’t get it to work. It kept trying to debug the entire Joomla! framework, and couldn’t understand that I was just interested in the 10-line stub I had created.
I went hunting through the SimpleTest code, and patched enough to get it running. But as soon as I added a new class (a test case for the database handler I need to write next), SimpleTest was lost again - it saw the tests but got lost in Joomla! trying to report on them.
If I had to guess, Joomla! has some architecture issue or security code that keeps SimpleTest from running under it.But it didn’t matter. The exercise gave me a good understanding of what SimpleTest did, and I simply wrote a quick-and-dirty class to run my own tests. (And no, I didn’t write a test first, I wrote the testing class and THEN wrote a test for it - I’m already falling off the path).
It’s working, and I’ve also got some simple tests written for the next step - to check whether I can create and drop database tables.
0 comments:
Post a Comment