When you do TDD, “fake implementations” or “wrong code” are OK, as long as they pass all the tests you have so farTanzer is using this as a stepping stone to introduce Uncle Bob's heuristic: as the tests get more specific, the code gets more generic.
But when do you stop to fake? When do you start writing “real code”?
But there is another answer, which I eventually learned from a comment written by Kent Beck:
Do you have some refactoring to do first?Here is Tanzer's passing implementation:
And that's fine for our test calibration; we have successfully demonstrated that the test can distinguish the correct behavior from an incorrect behaviod in this specific case.
But... the current implementation implicitly describes two pieces of domain knowledge that we can make explicit.
- The length of the hint should be the same as the length of the secret word.
- The initial representation of the hint should conceal all of the letters in the secret word, which is to say it should be entirely composed of the unrevealed letter token "_".
From there, we might notice that the secretWord we are using in the hint method is the same that was passed to the constructor, and extract that duplication. Or we might decide that the creation of the hint of the correct length is a single idea that can be extracted into another function, and do that.
You can start writing the real code as soon as you have a green bar.
Because I was reviewing Saff and Boshernitsan today, I have been thinking about Beck's Money demonstration. Translated into Python, Beck's first test looks like
Riddle: what's the simplest implementation that will pass this test? There are probably several different answers, but the simplest I can come up with looks like:
No implementation, no variable names. Just 10. It's clear to me that this is "wrong code", in Tanzer's sense. But we don't need more tests to make it better, we can immediately refactor (in Beck's sense) to restore sanity to the implementation.
If we were being very small and deliberate in our refactoring, the refactoring sequence might look like:
Like "triangulation", small and deliberate steps are not required - they are a technique to practice so that you can get small when larger steps aren't working.
No comments:
Post a Comment