I really enjoyed reading "Engineer Notebook: An Extreme Programming
Episode" by Robert C. Martin and Robert S. Koss - it's a very
interesting article (available from
http://www.objectmentor.com/publications/xpepisode.htm)
Out of curiosity, I ran Jester (http://www.jesterinfo.co.uk) on the
code/tests from the paper, and it found something I thought really
interesting. Although the code looks fine, it can actually be
simplified quite easily.
Jester found:
D:\jester113\Game.java - changed source on line 16 (char index=249)
from if ( to if (false &&
CurrentFrame(int pins)
{
£if (firstThrowInFrame == true)
{
if (ad
i.e. there is an if statement where the tests still pass even when
only the false branch is ever executed. At first I found this
difficult to believe. Looking at the code I couldn't imagine how it
could be that the true branch is never needed. There were other
things reported by Jester too - I was beginning to wonder if Jester
was flawed.
On closer examination it turns out that all the code in the class
Game related to the instance variables "itsCurrentFrame"
and "firstThrowInFrame" can be removed and the tests still pass. The
method "score" simply needs to return "scoreForFrame(10)" - i.e. the
Game doesn't need to know what frame it's on, it can just ask the
Scorer for the score for frame 10 (the last frame) and the tests
still pass.
This is because the scores for any frames not yet thrown don't add
anything to the score - they are all 0 - so this gives the correct
answers at a minor cost of doing a few unnecessary additions (of 0).
(of course, Scorer could have a method something like
"totalScore" instead of calling "scoreForFrame(10)")
what do people think of this simplification of the code?
Ivan Moore
(simplified Game code below)
public class Game
{
public int score()
{
return scoreForFrame(10);
}
public void add(int pins)
{
itsScorer.addThrow(pins);
}
public int scoreForFrame(int theFrame)
{
return itsScorer.scoreForFrame(theFrame);
}
private Scorer itsScorer = new Scorer();
}