# The Clean Code Blog

by Robert C. Martin (Uncle Bob)

# Flash - TPP

27 May 2013

I read a nice blog today exploring the Transformation Priority Premise using the flash card kata. The author did it in both lisp and C#. Let’s try it in Java.

The flash card kata is pretty straightforward. Given a set of questions and answers, ask the user each question and solicit an answer. If the answer is correct, say so and count it. If the answer is wrong show the correct answer and count the error. At the end, print the number of right and wrong answers.

The first test is wonderfully simple. Given an empty list of questions and answers, end the game immediately with a summary showing nothing right and nothing wrong.

We begin with an empty test which passes:

```public class flashCardTest {
@Test
public void degenerateGame() throws Exception {
}
}
```

We get the test to fail by asserting the answer we want and by doing a couple of ({}–>nil) transforms. (In this case -1 is the integer equivalent of nil, e.g. a degenerate value)

```public class FlashCardTest {
@Test
public void rightAndWrongShouldBeZeroIfGamePlayedWithNoCards() {
List emptyList = new ArrayList();
playGame(emptyList);
}

return -1;
}

return -1;
}

private void playGame(List flashCards) {
}
}
```

To get this to pass just need to do some (nil->constant) transforms.

```private int wrongAnswers() {
return 0;
}

return 0;
}
```

This solution is a bit ugly since it couples the test and the solution. So let’s refactor to create a class.

```public class FlashCardTest {
@Test
public void rightAndWrongShouldBeZeroIfGamePlayedWithNoCards() {
FlashCardGame flashCardGame = new FlashCardGame();
List emptyList = new ArrayList();

flashCardGame.playGame(emptyList);

}
}
```

```public class FlashCardGame {
public FlashCardGame() {
}

return 0;
}

return 0;
}

void playGame(List flashCards) {
}
}
```

For the next test, lets try a game with a single flash card, that the user gets right.

```@Test
FlashCard card = new FlashCard("Q", "A");
List cards = new ArrayList();

flashCardGame.playGame(cards);
}
```

This fails of course. We can make it pass by simply incrementing the right count in `playGame` if the list of cards is not zero. This is a (unconditional->if) transform. That, plus a little refactoring gives us:

```public class FlashCardGame {

public FlashCardGame() {
}

return 0;
}

}

void playGame(List flashCards, FlashCardTest answerer) {
if (flashCards.size() != 0)
}
}
```

OK, so let’s try a wrong answer.

```@Test
FlashCard card = new FlashCard("QW", "A");
List cards = new ArrayList();

flashCardGame.playGame(cards);
}

if (question.equals("QR")) return "A";
else return "W";
}
```

This forced us to create the `answerQuestion` function that pretends to be a user answering questions. If you pass in “QR” you get the right answer “A”. If you pass in “QW” you get the wrong answer “W”. To get this test to pass we’re going to have to get this function called by `playGame`. We can do this by passing the test along in an argument using the Change Signature refactoring. Then we can use a (unconditional->if) transform to check the value of our new function.

```public class FlashCardGame {

public FlashCardGame() {
}

}

}

void playGame(List flashCards, FlashCardTest answerer) {
if (flashCards.size() != 0) {
String question = flashCards.get(0).getQuestion();
else
}
}
}
```

Of course this is hideous, so we need to refactor alot.

```public interface User {
}

----

public class MockUser implements User {
public MockUser() {
}

if (question.equals("QR")) return "A";
else return "W";
}
}

----

public class FlashCardGame {

public FlashCardGame() {
}

}

}

void playGame(List flashCards, User user) {
if (flashCards.size() != 0) {
String question = flashCards.get(0).getQuestion();
else
}
}
}

----

public class FlashCardTest {
private FlashCardGame flashCardGame;
private MockUser user = new MockUser();

@Before
public void setUp() throws Exception {
flashCardGame = new FlashCardGame();
user = new MockUser();
}

@Test
public void rightAndWrongShouldBeZeroIfGamePlayedWithNoCards() {
List emptyList = new ArrayList();

flashCardGame.playGame(emptyList, user);

}

@Test
FlashCard card = new FlashCard("QR", "A");
List cards = new ArrayList();

flashCardGame.playGame(cards, user);
}

@Test
FlashCard card = new FlashCard("QW", "A");
List cards = new ArrayList();

flashCardGame.playGame(cards, user);
}
}
```

Now let’s do two questions, one right and one wrong.

```@Test
public void countBothOneRightAndOneWrong() {
List cards = new ArrayList();

flashCardGame.playGame(cards, user);
}
```

This fails, but we can make it pass with a (if->while) transform.

```void playGame(List flashCards, User user) {
for (FlashCard card : flashCards) {
String question = card.getQuestion();
else
}
}
```

One thing left. We need to actually compare the answer in the flashcard to the response from the user.

```@Test
public void countThreeNewQuestionsTwoRightOneWrong() {
List cards = new ArrayList();

flashCardGame.playGame(cards, user);
}
```

We need to make a small change to the Mock.

```public class MockUser implements User {
if (question.equals("QR")) return "A";
else {
return question.substring(1);
}
}
}
```

And now we can make this pass with a simple (expression->function) transform.

```void playGame(List flashCards, User user) {
for (FlashCard card : flashCards) {
String question = card.getQuestion();