ChessTDD 33: Scenario Housekeeping
Having fixed some bugs in the last few episodes, it would have been nice to make some progress with functionality, but there was housekeeping to be done first. I did some refactoring for board setup using the tables, compacting methods, and made the implementation of the moves checker correct. This will put me on much more sustainable ground as I go forward implementing game scenarios.
What I accomplish in this clip:
- Fixed incorrect/incomplete implementation of checking for moves.
- Refactored BuildBoardFromTable method.
Here are some lessons to take away:
- When you make a test go red, don’t then take the opportunity to do a refactoring — even a small or inconsequential one. Go back to green and then do it. You want to be taking actions in batches as small as possible. Doing 2 things at once is a recipe for confusing cause and effect.
- I’m not sure how others feel about this, but I did something in this video that I do from time to time. I had a green test covering an implementation that was too permissive — too easy to get right. So I altered the test temporarily to green in a situation where it should have been red. I then modified the production code to get the expected red, then reverted the test and verified that it was green. This is the equivalent of writing another test, framed in the negative, and then taking that test from red to green. I shortcutted that process because I didn’t want that other test to be left around when I was done.
- I consider baking the names of types into class, method, and variable names to be a bad practice. You might change the type they use and forget to update the name accordingly, and you’re also leaking implementation details.
- A refactoring may well never seem perfect to you. If you make sure it seems cleaner or better as compared to where it was, that’s progress. Stick a pin it it and make a note to revisit later. Not only is this good for avoiding diminishing returns on a given day’s effort, but it also removes you from the problem so that you can better assess readability later.