TDD as if you Meant It: I care about Behavior and not about Representation (Episode 2)
About
Intentional Primitive Obsession
In the first episode I added some tests where I represent state as strings. This is an intentional approach to hide the complexity of the concepts with primitives. Since with TDD as if you Meant It I am not allowed to add any new classes, I need to start the problem by using primitives. I could have used an array to define the Board concept, but that already means that I am taking more complex design decisions.
Triangulating
I am focusing on triangulating on the concept of GameResult in order to have enough proof in order to extract it to a method. The typical proof I am searching for is duplication. I apply the Rule of Three to spot duplication and then to generalize my code.
Deductive vs Inductive
When doing evolutionary design I am deductive or inductive.
Inductive: I start from small concepts and I generalize them. Whenever I can can generalize some higher order concepts from the code I have, I extract them. These higher order concepts are usually a crystallization of the raw primitives.
Deductive: when doing design up-front I am deductive. I start from some bigger idea and then I try to prove it with code.
Tests
/*
* X 0
* X
* */
@Test
public void forTwoByTwoBoardXWinsOnColumn(){
// Arrange
String board = "two by two with X on left column";
String expected = "X won";
// Production code
String gameResult = board == "two by two with X on left column" ? "X won" : "Nobody won";
// Act
String actual = gameResult;
// Assert
assertEquals(expected, actual);
}
/ *
* Introduced no new notion
* 0 X
* X
* */
@Test
public void forTwoByTwoBoardXWinsOnRightColumn(){
// Arrange
String board = "two by two with X on right column";
String expected = "X won";
// Production code
String gameResult = board == "two by two with X on right column" ? "X won" : "Nobody won";
// Act
String actual = gameResult;
// Assert
assertEquals(expected, actual);
}
Video
Check the video below with the first episode:
What’s Next?
Check the next episode on TDD as if you Meant it here: http://blog.adrianbolboaca.ro/evolutionary-design
On the same page you can find more ideas on Evolutionary Design.
Credits
Many thanks to Keith Braithwaite for creating the concept of TDD as if you Meant It
Teddy bear thanks to Erik Talboom for all the pairing, discussions that lead to so many twists we discovered together with TDD as if you Meant It.
Special regards to JB Rainsberger for the fun pairing we did using TDD as if you Meant It