Friday, October 29, 2004

Progress...but daunting challenges loom

I finished the transition to class based pieces and got all 32 tests passing! I then started on checkmate. After some false starts I got most cases done without much effort. After talking to a friend I realized that I was still missing quite a few tests (checkmate because a piece is pinned, stalemate, 3 repeated positions anytime, 50 moves without a pawn move or capture). Whew and I thought I was almost done with the chess rules!!! These aren't going to real easy. I am especially worried about the repetition one because I must keep copies of boards. If I just copy my current ChessBoard then I have to copy the pieces as well...that isn't going to work. All I really need is a simple Position class I think...enough designing I don't have any tests failing yet ;-)

I now have 914 lines of code (BTW this doesn't include the GUI) of which 532 lines are test code. 38 tests and they all pass (for now ;-)

Thursday, October 28, 2004

In a groove...then not!

Well I finished the check tests for all pieces and managed to remove quite a bit of redundancy last night. The code had 32 tests and they all passed. The only missing piece was checkmate. I was feeling good. My productivity was great and then BAM! When I wrote checkmate I realized that I desperately needed subclasses for each piece (I know it is hard to believe that up until this point all code for the chess rules was contained in ChessBoard! But I was trying to do the simplest design that worked.) The signs where everywhere...switches, methods with the names of the pieces in them. How blind I had been.

This wasn't going to be easy. I first yanked the type field out of ChessPiece and then created a subclass for each piece. Nothing compiled! I decided that I would start by just getting one test to pass and then add the code for each test. Incremental refactoring! So I commented out all code that didn't involve setting up the board. My first goal was getting Pawn working then so on and so forth. I didn't get done, but 17 out of 32 tests are passing and all piece move logic is located in the respective class. The quality and amount of my tests gave me confidence to embark on a massive change without much fear. This confidence was fed as I was fairly easily able to get tests to pass with just a little work at a time. I foresee challenges coming with certain rules, but given the track record of my past experiences with TDD I am not afraid at all.

I'm back down to 491 non-commented source lines and 201 of them are test. I have commented out tests 18-32! I also have commented out all code for castling, enpassant and check.

Wednesday, October 27, 2004

Sometimes coding is a Slog

Well last night I got most of the check rules working. It was not easy, but I had so many tests that every time I broke something I found it right away. The biggest time sink was debugging a test that was bad. I was testing for check when no check existed, but I was sure it did. Visualizing chess positions without a graphic has always been difficult for me. I am still enamored with TDD. I have a total of 692 NCLOC with a whopping 384 of it in test code!

The most difficult thing for me is still writing code that I know needs to be refactored. When writing the check rules for bishop the straight forward way was so bloated. I had over a page worth of code just for bishop. Once I got all the tests passing, I focused on refactoring and came up with a function that could be used for both bishop and rook. The rook code took about a dozen lines and these were just minor changes from copying the bishop logic. Only time will tell, but I have gotten further than before with less code (not counting tests). The key is that I did it in less time. I don't remember how long it took me before, but I know I spent more then the dozen or so hours I have expended this time. Tonight I hope to get queen checks (just a combo of rook and bishop) and then moves into check and castling across check.

Tuesday, October 26, 2004

Rediscovering coding through TDD

Last week I was reading a series of articles by Ron Jefferies on Discovering Better Code and it got me thinking. I had read several posts on Test Driven Development (TDD) in the past, but I had been busy enough to simply say, "seems like a good idea, I guess." Last week I wasn't so busy and I spent my free time thinking about TDD and how I hadn't programmed any code for a long time. I pulled up my last coding experiment, a failed attempt at a chess program that played decent chess. I had stopped with all chess rules accounted for except check. I had given up trying to debug check because the code was so hard to follow and understand.

So why had I failed? One could argue that I didn't do enough design, but design wasn't the real issue. There just aren't that many classes in my code, the real issue was the actual code. It started out in good shape, but entropy had the gotten the better of it. It had grown to be a Big Ball of Mud. Also I had ZERO tests! So any major change to the code base was likely to result in more errors that would be even harder to run down and fix. Thus the reason for me giving up.

What does TDD have to do with any of this. The main point of TDD is that you start development with a test that doesn't run, then you write the simplest code that will make the test pass. Repeat until done. I had never really given TDD much thought. I thought it might work for some, but probably not for intelligent, experienced coders like myself! Haw, was I full of it. The real problem is getting over the fear of writing code that you know will be rewritten. I hate waste and I thought that TDD generated way too much waste. Wouldn't it be better just to write the correct code the first time..but I know no one can write correct code the first time. It always requires feedback and iterations to get it to work. So what was my problem again? The more I thought, the more I realized that I just didn't want to change the way I developed software. It had worked for 20 years, so why change it? This is funny, because my biggest obstacle in my current job is getting people to change what they always do. Funny isn't it!

So I was able to get past my fear of change and my fear of doing nothing and try it out. This occurred over the weekend. I downloaded a new Java IDE and started TDD Friday afternoon. So I began writing a test. The first thing I could think up was setting up the chess board. That is going to be a too big, so I decided to start smaller. How about an empty chess board. That was easy and some thoughts of waste started to creep in my head, but I beat them down and continued. Next, setup the pawns and so on and so forth. It wasn't long before the whole board was setup. Well at least my tests told me so, but I didn't trust them so I wanted to see it of myself.

I tried to generate a GUI and quickly decided that would take too long and imported my old GUI that wasn't too bad. It's biggest virtue was it was decoupled completely from the implementation. In no time I had a viewable chess board with all the pieces in the correct place! So then I started on movement of the pieces and then captures and viola. After ~7 hours I was back to the same spot I was before. I had a chess board that one could play a game against oneself except that check wasn't understood and neither was the end of a game.

To an outsider this would seem like a huge waste of time, but it was not. I was so productive. I generated 543 lines of code and 333 of it was test code. My old code, which took me much longer that 7 hours to write, had 495 lines of code with ZERO tests! Plus, I never got bogged down. I had bugs, but I could quickly find them because I was working in such short cycles of write small test, write code to make it pass, run all tests and see if I broke anything. Only one bug slipped though which I caught by trying to play a game. I went to fix it and told myself wait! I must have a failed test first. This discipline might seem silly, but it gave me such a good body of tests that when I went to clean up a section (to get the to queen move, I had cut and pasted the bishop and rook code) I wasn't worried because I knew the tests would show me any problems I might inject.

So that was a long post. I should have posted it in chunks over the weekend, but I spent much of the weekend disconnected from the web (my wife refuses to allow me to go wireless ;-) The next question is how hard will check be?? We shall see.

Tuesday, October 19, 2004

Jon Stewart on Crossfire

I just read Stewart Caught in the Crossfire on Slate. I missed this live, but with the internet, do you really miss anything. I have never seen a more honest person on a political talk show. I wish Jon Stewart did a show that was serious. I don't think many people would watch it, but I would. For those like me (with a short attention span) the transcript offers some of what really happened (although I think you have to see it!)

STEWART: See, the thing is, we need your help. Right now, you're helping the politicians and the corporations. an we're left out there to mow our lawns.

BEGALA: By beating up on them? You just said we're too rough on them when they make mistakes.

STEWART: No, no, no, you're not too rough on them. You're part of their strategies. You are partisan, what do you call it, hacks.
STEWART: ... is that the news organizations look to Comedy Central for their cues on integrity.
STEWART: I would love to see a debate show.

BEGALA: We're 30 minutes in a 24-hour day where we have each side on, as best we can get them, and have them fight it out.

STEWART: No, no, no, no, that would be great. To do a debate would be great. But that's like saying pro wrestling is a show about athletic competition.

STEWART: But the thing is that this -- you're doing theater, when you should be doing debate, which would be great.

BEGALA: We do, do...

STEWART: It's not honest. What you do is not honest. What you do is partisan hackery. And I will tell you why I know it.

STEWART: You know, the interesting thing I have is, you have a responsibility to the public discourse, and you fail miserably.

CARLSON: You need to get a job at a journalism school, I think.

STEWART: You need to go to one.

Enough is enough. I could have quoted the whole thing! Sorry for the length. Watch it or read it for yourself.

Friday, October 15, 2004

A true maverick

I came across this review of Maverick by Ricardo Semler and although I have not read the book, I found this review fascinating. I bet Mr. Semler is an interesting fellow.


I came across Two Great Wastes in Organizations earlier this week. Although I have long believed most waste comes from people not talking to each other, this paper lays out the case in a convincing manner.

Tax Stats

I was reading Lay Off Massachusetts and came across Fiscal Facts. This page has the ratio of federal funds paid versus how much federal revenue is generated per state for 1992 and 2002. The most interesting thing is that the state with the highest ratio (gets more than it gives) for 2002 is North Dakota and the lowest is New Jersey. I would have guess my state (VA) would be bad (11th, 1.47) given its proximity to DC, but I would not have figured the top five (SD, NM, MS, AK, WV). Goes to show that you don't always know what you think you know.

First Post

After thinking about doing this for a number of months, I have finally done it! Who knows if I will actually keep posting, but a journey begins with a first step. I have to give credit to the Google Bar. I was looking at the options and they had a BlogThis! option. When I turned it on, I wound up starting a blog. Funny thing this internet is.