I added a book index. Pretty straightforward cutting and pasting, followed by some refactoring.

The refactoring was a little different from normal. For one thing, it was my first experiment with writing generic classes in Java. (I’d written a generic function before, but not a generic class.) I learned a little more about what you can and can’t do; it turns out that you can’t construct objects of a type given by a generic parameter (not surprising – you don’t even know what arguments the constructor will take, if any) or check if a type is an instanceof a generic parameter (doesn’t work because of type erasure – they need to provide a single implementation, basically, and the implementation wouldn’t work if you replaced the parameter by Object). Too bad, though, since my code would have been a little shorter if I’d been able to use those features.

For another thing, I made more mistakes than normal. I’ve avoided learning to use a Java debugger so far; with TDD it’s rarely necessary. But I did make one mistake that took me a while to figure out; I ended up undoing some refactorings, redoing them, and using printfs, but it would have been a lot faster if I’d gone into a debugger. So next time this happens, I really should learn how to use one. (My problem was that I was doing something complicated in a constructor before the object was fully initialized; I should know better than that. It’s always hard to find the right balance when writing a class that basically exists to calculate a single value, though: what goes in the constructor, what goes in the other member function?) And then I ran into a bug that my acceptance tests caught that had slipped through my unit tests, and of course I wasn’t sure when I’d last run the acceptance tests, so I didn’t know when I’d introduced the bug. Fortunately, I found the bug after thinking about it for just a couple of minutes, but I was starting to regret that I wasn’t regularly saving the output of svn diff on my source tree, like I do at work.

And getting the balance of the classes just right, and figuring out which should be nested classes and which wouldn’t, took a while. I like what I ended up with, though; in fact, it was a strange refactoring in that it didn’t save me many, if any, lines of code, but it did clarify the code nicely. So the next time I implement an index, it will only be three or five lines shorter than it would have otherwise been, but I’ll know exactly what I need to type.

I also used the new code to help me generate the list of books by an author. It turns out (I’d realized this a week ago) that that code had a bug in it: if I had two books with the same name by the same author, they would have only shown up once. Which would sound a bit far-fetched, except that books in the database largely represent physical objects; so if I wanted to enter both of my copies of Delany’s The Mad Man in the database, they wouldn’t have both shown up everywhere. But now they will! The same bug still exists in a couple of other ways: multiple series with the same title, mutiple volumes in a series with the same number. There, it’s not so clear to me how to come up with realistic examples, so I’m not worrying about it for now. It’ll probably all get naturally fixed when I move to an SQL representation of the database, anyways.

Post Revisions:

There are no revisions for this post.