TDD

Test-Driven Development

I just finished a three-day training course on Test-Driven Development (TDD) by Industrial Logic. It was jam packed with really great information. Luckily they gave us online access to all the material so that i can review it later. It was too much to digest in the class. I recommend that course to anyone.

Often we think that TDD is simply “write the test first, see it fail, then write code until the test passes”. It is much more. How you do things actually really matters. Some major points I took from the training are:

Refactoring:

  • You have to stay green in your tests while refactoring. Yes, constantly.
  • Make tiny, little, small changes.
  • Go from bottoms up. Make changes that don’t break your code. Making a big change that gives you a red rash (breaks your code in many places) is wrong. First refactor “downstream” so that you go red as little as possible.
  • Develop a good code smell nose.
  • Buy Joshua’s book
  • Learn all refactoring keystrokes in your IDE

Microtesting

  • They should run in microseconds (or milliseconds)
  • Less than 10 lines of code
  • Run them often (after every little change)
  • Install and use the Infinitest plugin (That is one sweet plugin)

TDD

  • Reduce the thing you are building into the absolute minimum
  • Write one test for the simplest case
  • Make the test pass quickly (This might mean that your code is less than optimal)
  • Refactor your code while your test is green
  • Integrate
  • Create another little microtest that pushes the limit. (It will be red)
  • Code for that
  • etc…

After doing this several times in the training, I thought that I could probably write code faster with my old method. But, with TDD I get really good test coverage early. That is valuable. Also with TDD I only write code that is needed for features. And, I could deploy at any moment.

I haven’t really done this with production code yet, but I believe this is a good approach. I will definitely give it a try.

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp WhatsApp Me