DaedTech

Stories about Software

By

Chess TDD 44: Starting the Climb toward En Passant

En passant is going to be a fairly complicated thing to calculate, given the way I’ve implemented this thing so far.  And, true to form, I only got a very thin slice going in this episode.  Still, it was measurable progress and it’s good that I was able to slice thinly.

What I accomplish in this clip:

  • Fixed a mistake in one of my tests that a viewer pointed out.
  • Got the first en passant test passing.

Here are some lessons to take away:

  • As always, peer review is king.
  • Finding a way to carve thin slices off of large problems is an art form and so important.  Without this, it’s easy to be overwhelmed by difficult problems.
  • When you’re writing a test and you see an unexpected behavior from production code, stop and clarify your understanding.  You don’t want to procrastinate with that.
  • Edge cases account for a lot of complexity in design, which makes it doubly important to have a comprehensive regression test suite.
  • Revisiting a design is really hard without automated tests to cover what you’re doing.  This tends to cause designs to calcify in untested codebases, even to the point of avoiding the addition of new functionality that users want.

By

Chess TDD 43: Pawns Good to Go

This episode was a lot of fun because all of the cards just kind of fell into place and I got the pawn done (with the exception of en passant). I had thought finishing up the pawn was going to take a number of episodes, but then there was a flurry of win. I’ll take it!

What I accomplish in this clip:

  • Finished up the implementation of black pawn movement.
  • Pretty well set with acceptance tests for pawn.

Here are some lessons to take away:

  • Be on the lookout in your code for overly complicated boolean conditions; always look to simplify.
  • If you can avoid creating more levels of inheritance and that sort of indirection, you should.  That sort of thing can be a helpful tool, but you pay a price in complexity.

By

Chess TDD 42: Finishing up White Pawn Movement

In this episode, fresh off the victory of getting pawn movement right for the white pawns, I start on the black ones by essentially reversing their movement.

What I accomplish in this clip:

  • Got the first acceptance test passing for black pawn movement.

Here are some lessons to take away:

  • Having one context per test class is a nice way to keep tests readable, focused, and organized.
  • You’ll probably never stop making dumb mistakes, so it’s good to learn to have a sense of humor about it.
  • Tests are very handy for confirming your understanding of the code base.  Feel free to tweak a value in a test just to see what will happen, and then put it back.
  • Instead of hopping quickly into the debugger, see if you can try process of elimination things to narrow down where the problem is.
  • If you find yourself in a class, typing the same conditional logic in every method, you have something that could probably be two classes.

By

Chess TDD 41: Finishing up White Pawn Movement

This post was the first of another three that I recorded together.  I wanted to tie up some loose ends and move on to implementing pawn movement for black, but I just wound up tying up loose ends.

On a meta note, I’m going on vacation out of the country for a week, so I may or may not be in a position to log in and push posts live this week. It may be a light week for DaedTech posts.

What I accomplish in this clip:

  • Got IsNonCaptureAllowed working properly

Here are some lessons to take away:

  • There’s nuance to when to drive your design/development with acceptance tests versus unit tests.  TDD is generally good when you want to iterate quickly to get a narrow implementation right.
  • There’s no harm in extracting out a method from a test so that you can save yourself typing in the next test you’re writing.  (As long as you’re green while you do it)
  • It’s not relevant to my series, per se, but it’s worth knowing what characterization tests are.
  • Look out for the sunk cost fallacy with your own code.  The fact that you’ve pulled out and spent time creating a method doesn’t mean that the method is actually beneficial or valuable.
  • Beware of iceberg classes.

By

Chess TDD 40: Diagonal Capture for Pawns

This post was the last of a series of 3 I recorded together, so it picks up right on the heels of episode 39 from a flow perspective.  I initially tied up loose ends from the previous episode and then got the pawn’s diagonal capture in place, at least for the white pawns (acceptance tests for black pawns won’t go well right now).  Fits and starts, but real progress nonetheless.

What I accomplish in this clip:

  • Finished the TDD implementation for IsCaptureAllowed
  • Implemented IsNonCaptureAllowed
  • Finally gave pawn diagonal capture ability

Here are some lessons to take away:

  • Always look to refactor test code when it comes to readability and eliminating duplication.  These refactorings are not just for production code!
  • There’s a sizable contingent of people that don’t like seeing the Assert call factored out of unit tests.  Clearly a matter of personal preference, but you may get pushback in a shared code base.
  • In methods, preserve a consistent level of abstraction.  If you have complex two part conditional, don’t factor out one part and leave the other part — make them both conceptually similar.
  • Clean, readable code will look at least vaguely familiar to domain experts, even if they aren’t programmers.  They should see/read concepts that are familiar.
  • Keeping the lines of code low in a class isn’t a first class or all-encompassing goal.  A class can get ugly by having too many methods, even if all of those methods are small.

By

Chess TDD 39: Dipping a Toe into Complex Piece Movement

What I accomplish in this clip:

  • Finished implementing the bishop acceptance tests.
  • Introduced the concept of “capture move” and “non-capture move” to piece movement.
  • Made some good headway on the pawn’s diagonal capture capability.

Here are some lessons to take away:

  • I’ve mentioned this before in the negative a few times, but I’ll phrase it in the positive.  If you’re going to do refactorings, even trivial ones like deleting a dead method, do it with a green test suite.
  • Writing acceptance or integration tests, whether using them to drive design or not, tends to give you a good lesson in patterns of things that can go wrong.  The specific example I’m referring to here is how I noticed that movement for the black pieces from higher numbered rows to lowered number ones tended to be a problem across pieces.
  • Even if you’re not thrilled with a design, you can be productive and get things done with it when your test coverage is good since you won’t be afraid to change it later.  This helps prevent “coders’ block” or “paralysis by analysis.”
  • When you’re reasoning about implementation, allow yourself to stub out helper methods or methods on other objects that will ‘magically’ do what you want.  E.g. if I were writing a “CookDinner” method, I might just declare “TurnOnStove()” and stub it out, to be implemented later.  This practice of defining what before how can help you get toward the single responsibility principle.
  • I won’t belabor the metaphor of tech debt, but having a really high degree of test coverage and a lot of small, modular methods in the code base lets you live with tech debt a little more easily in the short term.  With a lot of crufty code around the problematic code, you’ll never want to touch anything when it comes time to pay it off.  With lots of coverage and good code elsewhere, getting rid of the tech debt at a time that suits you is easier.

By

ChessTDD 38: Bishop Tests and Being Wrong about Being Wrong

For this episode, I did something a bit different.  I actually recorded the coding for this and then two more episodes on its heels.  If you’re following along on github, you’d notice that I’ve checked in code for episode 40, though I still need to do the audio for those next two episodes.  In this particular episode, I get off to a nice start with testing for bishop and then get a bit derailed.  Luckily (spoiler alert), I get it straightened out in next episode.  And, for all 3 of these, I successfully kept to right around 20 minutes.

What I accomplish in these clips:

  • Implement part of the acceptance tests for bishop.

Here are some lessons to take away:

  • I find a lot of benefit in stripping everything but what I need in default templates I’m provided (e.g. the spec flow feature file, an web.config file, etc).  I prefer to be as deliberate as possible about everything that goes into the software work product.
  • No matter what you’re doing, if you do it brainlessly, it won’t go well.  I avoided copy and paste to avoid brainless mistakes, and just wound up brainlessly typing in the wrong thing by hand.
  • TDD lets you poke at and test the code to see if you can get to green (or red) without fully understanding what you’re doing, but if you do this, make absolutely sure you spend time immediately afterward writing tests or reasoning about the code so that you do understand it.
  • You almost certainly won’t stop yourself from making stupid mistakes, but you can develop techniques for discovering them more quickly and reducing their impact scope.

By

ChessTDD 37: Cleaning Up and Implementing Rook

Got back in the saddle with this episode, and had a shorter recording that basically went well.  Someone pointed out in the comments that I had reversed king and queen position for the black pieces, so I started off by fixing that, and then moved on to fixing a bug and finally implementing the Rook acceptance tests.  Wrapped it up in relatively short order, too.

What I accomplish in these clips:

  • Fixed my template for the full chess board.
  • Fixed the vertical version of that reverse bug from last time.
  • Implemented rook acceptance tests.

Here are some lessons to take away:

  • It’s important to understand past shortcomings of your code so that you can form good hypotheses about where you might have other potential weak spots.
  • If you’re going to run an experiment on your code, do it from a test that you’re writing instead of through the GUI or the debugger or something.  You’re running the experiment anyway, so you may as well capture the results in the form of an automated regression test.
  • A lack of test coverage for a line of code isn’t a problem in and of itself.  Coverage is a metric and not a goal.  The problem with having code not covered by tests is that there’s nothing to prove that the code was implemented in a thoughtful, deliberate way.  If you don’t have a test expressing what a path through the code is supposed to do, there’s no way to know if you’ve reasoned about that code.
  • Copying, pasting, and adjusting often winds up taking longer than typing by hand.  You spend more time staring dumbly at the screen trying to figure out what’s wrong than it would have taken you to hand type the code you need.

By

ChessTDD 36: Acceptance Tests for Queen Movement

This episode went smoothly until I discovered a bug.  Philosophically, I suppose this is the natural state of software development.  For my purposes here, though, hunting down the bug caused the episode to balloon to 26 minutes, so I’m going to try a new thing so as to keep the videos a reasonable length of time.  I’m splitting it into 2 videos: parts A and B.  Please let me know if this approach is preferable to sometimes having long videos or not; if you leave feedback, I’ll more likely do it the way you prefer, since I’m just trying to go with what people like.

What I accomplish in these clips:

  • Created a couple of code snippets in CodeRush to get rid of the hand typing in the specflow scenarios.
  • Wrote acceptance tests for the queen’s movement.
  • Squashed a subtle bug (or at least half of one).

Here are some lessons to take away:

  • Projects go better when there are more eyeballs on them.  Run things you’re doing by other people and see if they have suggestions that might help.  They may think of things that never would have occurred to you and might later seem obvious.
  • Whenever you make mistakes copying and pasting, it’s a crapshoot whether fixing them takes more time than you would have spent hand-typing or not.  In my experience, most of the time you don’t come out on the winning end, and wholesale copy-paste obscures your understanding.  This is why I try to avoid the practice.
  • What I find is that unit tests should be very directed and specific about system behaviors.  But acceptance tests let you put on your exploratory testing hat, dreaming up scenarios in which users might use this thing that could potentially break it.  For you unit testing newbies, fight the urge to write unit tests with lots of assertions that cover a lot of ground.  You can express that in your acceptance tests.
  • Once again, don’t do low-hanging fruit refactorings (e.g. deleting dead code) when you have red tests.  It might seem like it’s not a problem, but it will come back to haunt you at some point.
  • Another example in this episode of finding a bug with a failing acceptance test, and drilling in to get closer by writing failing unit tests.  This is an excellent and helpful practice.
  • TDD facilitates Eureka moments where you try something you think might work and you see all of your tests go green.  However, just like trying something in your code and seeing the application magically behave correctly next time you run it, it’s important to cement your understanding of why it worked.  Don’t program by coincidence, even if you have a green test suite backing you.  Keep writing tests and/or reasoning about the code until you’re sure you understand what’s happening.  (Writing tests provides an excellent way to prove your understanding to yourself).

By

ChessTDD 35: Acceptance Tests for Knight Movement

Things are really starting to flow with the acceptance tests now. In this episode, not only did I mercifully not uncover any important bugs, but I defined knight movement in acceptance tests in a way that I feel pretty good about. I’m learning as I go about using SpecFlow, which is cool, and as the cherry on the sundae, I actually got episode length back under control with a 17 minute episode.

 

What I accomplish in this clip:

  • Fixed the poor naming I left off with last time.
  • Wrote acceptance tests for the knight’s movement.

Here are some lessons to take away:

  • Naming is so, so, so important.  It may seem like a trivial thing, but leaving a method about chess piece movement where “origin” and “destination” were reversed would, sooner or later, cause someone a serious headache.  It would also probably make them hate me when they looked at the history.  Make sure your names are good.
  • You have to do a fair bit of fumbling when you’re figuring out a new tool/tech/framework on the fly.  Stick to your principles and be sensitive to the idea that there’s probably a better way to do a lot of the things that you’re trying to do.  Ask people, and read a lot if you can.
  • There’s a lot of out of the box stuff that comes when I make a SpecFlow feature, but I just get rid of it.  For me, it’s helpful to eliminate everything that I’m not using so as not to be confused about what’s mandatory and what isn’t and also not to be confused later about what functionality I’m even using.
  • An interesting tradeoff emerges in my use of SpecFlow.  Duplication is, by and large, pretty bad in a code base.  But, in the case of visualization, showing the chess board for each individual acceptance test may be helpful in that it makes it crystal clear what’s going on in each test.  There are probably various strategies to try optimizing for minimized duplication and maximized visualization simultaneously, but it’s worth bearing in mind that everything in software development is a matter of tradeoffs and it’s best to be deliberate about whatever choice you make.

Acknowledgements | Contact | About | Social Media