Post navigation

TDD Chess Game Part 2

Alright, welcome to the inaugural video post from my blog. Due to several requests over twitter, comments, etc, I decided to use my Pluralsight recording setup to record myself actually coding for this series instead of just posting a lot of snippets of code. It was actually a good bit of fun and sort of surreal to do. I just coded for the series as I normally would, except that I turned the screen capture on while I was coding. A few days later, I watched the video and recorded narration for it as I watched it, which is why you’ll hear me sometimes struggling to recall exactly what I did.

The Pluraslight recordings are obviously a lot more polished — I tend to script those out to varying degrees and re-record until I have pretty good takes. This was a different animal; I just watched myself code and kind of narrated what I was doing, pauses, stupid mistakes, and all. My goal is that it will feel like we’re pair programming with me driving and explaining as I go: informal, conversational, etc.

Here’s what I accomplish in this particular video, not necessarily in order:

Eliminated magic numbers in Board class.

Got rid of x coordinate/y coordinate arguments in favor of an immutable type called BoardCoordinate.

Cleaned up a unit test with two asserts.

Got rid of the ‘cheating’ approach of returning a tuple of int, int.

Made the GetPossibleMoves method return an enumeration of moves instead of a single move.

And, here are some lessons to take away from this, both instructional from me and by watching me make mistakes:

Passing the same primitive/value types around your code everywhere (e.g. xCoordinate, yCoordinate) is a code smell called “primitive obsession” and it often indicates that you have something that should be a type in your domain. Don’t let this infect your code.

You can’t initialize properties in a non-default constructor (I looked up the whys and wherefores here after not remembering exactly why while recording audio and video).

Having lots of value type parameter and return values instead of domain concepts leads to constant small confusions that add up to lots of wasted time. Eliminate these as early in your design as possible to minimize this.

Sometimes you’ll create a failing test and then try something to make it pass that doesn’t work. This indicates that you’re not clear on what’s going on with the code, and it’s good that you’re following TDD so you catch your confusion as early as possible.

If you write some code to get a red test to pass, and it doesn’t work, and then you discover the problem was with your test rather than the production code, don’t leave the changes you made to the production code in there, even if the test is green. That code wasn’t necessary, and you should never have so much as a single line of code in your code base that you didn’t put in for reasons you can clearly explain. “Meh, it’s green, so whatever” is unacceptable. At every moment you should know exactly why your tests are red if they’re red, green if they’re green, or not compiling if the code doesn’t compile. If you’ve written code that you don’t understand, research it or delete it.

No matter how long you’ve been doing this, you’re still going to do dumb things. Accept it, and optimize your process to minimize the amount of wasted time your mistakes cause (TDD is an excellent way to do this).

So, here’s the video. Enjoy!

A couple of housekeeping notes. First, you should watch the video in full screen, 1080p, ideally (click the little “gear” icon between “CC” and “YouTube” at the bottom of the video screen . 720 will work but may be a touch blurry. Lower resolutions and you won’t see what’s going on. Second, if there’s interest, I can keep the source for this on github as I work on it. The videos will lag behind the source though (for instance, I’ve already done the coding for part 3 in the series — just not the audio or the post, yet). Drop me a comment or tweet at me or something if you’d like to see the code as it emerges also — adding it to github isn’t exactly difficult, but I won’t bother unless there’s interest.

Great video. Any chance you could increase the font size in future videos, just slightly, to make things more readable?

Themba Masina

WOW nice video as @Jon said, please increase the font so that we can code with you.

Thanks again for this effort.

Jacob

Very cool. Keep it coming

Mauricio

Nice video, what tool do you use for refactoring?

Simon Lawrence

I wanted to practice my hand at BDD, since that is what we use at work and thought Chess would be the perfect candidate for this, i started about three weeks ago, just working on my lunch breaks and have enjoyed learning from my mistakes. It will be interesting to me to see how your progression and choices differ from my own. The question is, do i watch yours now and risk influencing my decisions or wait until the end

That’s an interesting line of thought, though I think there’s kind of a spectrum between “class as unit” TDD and so called “inside out TDD.” For instance, I frequently don’t bother writing unit tests for POCO-type objects or other simple things if I’m extracting a class, letting the existing tests cover the new class. I think that’s somewhat of a hybrid approach that I’ve developed.

http://www.daedtech.com/blog Erik Dietrich

Thanks all, for the kind words. Would have replied sooner, but I’ve been laid up with food poisoning the last few days, so I’m just now getting off the bed-rest.

http://www.daedtech.com/blog Erik Dietrich

That’s a good idea — will do. When I record Pluraslight videos, they have to be recorded at 1024×768, which forces a lot of ‘zoom.’ There’s certainly more than enough screen real estate to zoom in a bit.

Quagmire

Another nice post… It would be interesting if you put the code on Github.

Simon Lawrence

I put my progress up on github, don’t know if you’d be interested in seeing it or not. I am at the point where i have most of the logic for the pieces done and am now trying to get the special moves like castling in. Also don’t forget En Passant :), i didn’t even know it existed until i started doing this.

Will do at some point here. I’m currently in the home stretch on my next Pluralsight course, so free time is at a premium right now, but I’m aiming to be done with that in a week or so and should have a little more time then.

Simon Lawrence

No rush at all, don’t worry if you don’t have time, I know time is valuable :). Good luck with getting your course done.