DaedTech

Stories about Software

By

ChessTDD 18: RemovePiece and Housekeeping

In this episode, I managed to pry myself away at a slightly shorter length of recording time.  You can seem me at the end wavering and and wanting to continue, but I know myself, and I’d have been hustling to wrap up under 25 minutes next thing I knew, so I thought I’d call it at a reasonable stopping point.

Here’s what I accomplished in this clip:

  • Implemented RemovePiece.
  • Made the public interface of Board a little nicer.
  • Minimized the usage of the array in favor of readable abstractions.

Here are lessons to take away:

  • Make yourself feel the pain of duplication.  Around 3:15 I fought off the temptation to copy and paste the setup of another test class to the new one, but I retyped it instead.  Copy and paste is like taking morphine and then walking on lava — you’re easing your pain when you should be letting it inform your actions to improve the situation.
  • Having identical setup in multiple classes is a test smell that’s making subtle suggestions.  Maybe the tests should be in a single class or, perhaps, there should be something in production code making that setup easy, if it’s really that common.  (I view the alternative, a lot of helper code in tests, not to be a better option).
  • At around 7:20, I did something subtle.  I looked for references to Board’s _pieces array and looked to eliminate as many references to it as possible.  The idea here is to minimize the use of a primitive abstractions in favor of the more descriptive ones I’ve created (GetPiece, RemovePiece, MovePiece).
  • More compact isn’t always better.  If you inline a variable, check for readability.  The beauty of a robust test suite is that you can really whip things around to maximize readability and feel good that you’re not breaking anything.

8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Robert Bonham
Robert Bonham
9 years ago

Random Question: Maybe this is more a reflection of my workplace than software design, but how do you bid/design the ‘unknown’. Let’s say your boss came to you and said, “our new project includes a web based game of GO, do you think 2 weeks is enough time to write it?” (http://en.wikipedia.org/wiki/Go_(game)) Questions: How do you give early on estimates? How do you know you know enough to begin? or do you learn as you test? How do you know if you’re over your head? and need to go 3rd party (library or consultant)? Whining: I was asked to code… Read more »

Erik Dietrich
9 years ago
Reply to  Robert Bonham

This is probably a lot of ground to cover for a post comment, but two points that I can offer: (1) I don’t really do estimates in the traditional sense. I frame it more by saying, “I’m pretty sure that I can give you X in 2 weeks, but we’ll see how it goes. After that, there’s a good chance I can give you Y in 4 weeks, but that’s less certain. And, I can maybe have Z in 6 weeks, but that’s getting pretty speculative.” The key is to flip the sort of traditional waterfall/command-control script of “how many… Read more »

Robert Bonham
Robert Bonham
9 years ago
Reply to  Erik Dietrich

Thanks so much for taking your time to answer. Since you offered, I e-mail you in the next day or two to bounce a couple further questions (it may be a book in itsself) I have. Thanks again.

ps. your answers help quite a bit

Peter DiSalvo
Peter DiSalvo
9 years ago

I’m learning a lot from watching your process. Thanks 🙂

Erik Dietrich
9 years ago
Reply to  Peter DiSalvo

Glad to hear it! When we first talked about this, I didn’t realize what the scope would turn out to be. It’s been fun, though, working on it 20 minutes at a time.

Jeff S
Jeff S
9 years ago

In Board.MovePiece, I believe your intention of removing the null piece check was because you were going to be calling AddPiece (which does the null check) instead of SetPiece. After backing out your changes to get back to green and then re-removed the null check again, you forgot to replace SetPiece with AddPiece. The tests still pass because RemovePiece also does a null check and throws the exception, so Board_MovePiece_GivenChessBoardWithoutPawns_Should.Throw_Exception_When_Attempt_Is_Made_To_Move_Nonexistent_Piece still passes. While technically, an exception is thrown and the game is halted, isn’t this still somewhat a coding by coincidence thing? It happens to work, but not quite the… Read more »

Erik Dietrich
9 years ago
Reply to  Jeff S

That’s a good catch. I’m looking at the code on github because I don’t have access to my machine with VS on it right now (packed away for traveling), and it took me a minute to get there, but yeah, the subtle potential problem that you mention exists in the current code base, to be sure. I’ll say a few things on this: (1) Programming by coincidence? I wouldn’t call it that, personally. To me, programming by coincidence is “don’t know why that worked and don’t care,” or a refusal or inability to reason about your code. That’s not what… Read more »

Jeff S
Jeff S
9 years ago
Reply to  Erik Dietrich

Thank you for the detailed response.