Sunday, April 3

TDD bowling, and J, revised, part 1

So, I implemented a bowling scorer, in J, using TDD, and I was not very satisfied with the result.  And I keep meaning to come back to it...

The problem, as it were, is that "you get what you test for".  And all I was testing for was a single numeric result that was the total score.  I left the expression of bowling score "frames" as extra credit work.  And then I went back and tried an extra credit exercise, but it had classic second system effects, and I was left feeling dissatisfied, and I am sure that both of the people that bothered to look at my posts were not particularly enthralled, either.

So... I need better tests.

In other words, instead of testing for "just the right score" I also need to test for "the right structure".   And that means that I need to design "the right structure".  So what is "the right structure"?

I am going to go with the structure proposed by Ron Jeffries:  An array with one row for each frame, padded out so that each row has 3 columns.  I am ambivalent about the third column, so I might come back and revise this to say that the number of columns can not exceed 3.   I am also ambivalent about requiring the thing always be 10 rows, so I am going to say that the number of rows cannot exceed 10.  I can come back and add tests requiring exactly 10 rows and exactly 3 columns if I later on decide that these are important.  So what can I test here?

Since this structure represents the game's score, I am going to require that +/@, on the structure gives me the right score.

   assert 0 -: +/@, 20#0

I also need to check that the result has rows and columns (and nothing else)

   assert 2 -: #$ 10 3$0


I also need to check that I have no more than 10 rows and no more than 3 columns:


   assert 10 3 >: 10 3$0


And, I these tests are fundamental, every test that I add needs to incorporate these tests.

   isScore=: -:&(+/@,)  (10 3 >: ]),  2 -: #@$@]
   assert 0 isScore 10 3$0
|length error: isScore
|   assert 0     isScore 10 3$0
   isScore=: -:&(+/@,)  (10 3 >: $@]),  2 -: #@$@]
   assert 0 isScore 10 3$0
|assertion failure: assert
|       assert 0 isScore 10 3$0
   isScore=: -:&(+/@,),  (10 3 >: $@]),  2 -: #@$@]
   assert 0 isScore 10 3$0

Ok, so now I have my test rig.  Part 2 will be to about writing the bowling scorer itself.


I want to stop here so this post remains bite, sized, and because I have already been working on this blog entry for half an hour. 


Hmm... and it looks like some whitespace has crept into the post itself which is not showing up in the blogger edit interface.  I will have to be cautious to avoid that in the future, but after fighting it for 10 minutes I have decided to give up on trying to get that whitespace out of this post.

No comments:

Post a Comment