My Profile Photo

Matthias Lischka

S O F T W A R E   D E V E L O P M E N T

TCR - Test && Commit || Revert

Yesterday I learned a new game in the Coding Dojo Vienna. It’s an idea from Kent Beck* and an awesome tool to train correct refactoring.

You will need a codebase with existing tests for refactoring and some scripts to set up your tooling.

Done right your tooling will:

  • tests your code and
  • commit the changes when all tests pass
  • but reset all changes when the tests fail

Setup

I had this setup done for Windows and .Net Core. All you need are some small batch scripts and your code in a git repository.

This should be very easy for every programming language and OS. Check out ladders.js by dtanzer and codecop for some JS example.

test.bat

dotnet test

commit.bat

git stage -A
git commit -m "working"

revert.bat

git reset --hard

We used Emily Bache’s famous Tennis Refactoring Kata:

  • Check it out
  • Put the batch files in the csharp folder
  • commit the batch files! or they will get reverted as well when you fail at your first try
  • start refactoring
  • whenever you feel it run the following command in your CMD
 > test.bat && commit.bat || revert.bat

This will run dotnet test and commit your changes when all tests pass or hard reset your changes when you made a mistake.

Limits

While this is an awesome tool for a Coding Dojo and for training it has some clear limitations.

  • You can not do TDD in the classical approach. You can’t write red tests first - they will get reverted.
  • You want your tests to be fast. Since your work is at risk every time you hit TCR you want to do it often and this could become frustrating when it takes too long.

Why?

It trains you to work more cleanly. You are more likely to use failsafe refactoring tools like R# when your work is at risk.

Trust me, it will be a funny experience. There will be moment when you hit > test.bat && commit.bat || revert.bat and are not sure if it will pass or reset all your code.

It for sure did reset our code at times. That were the most interesting moments. Some tests fail, you don’t know why exactly and all your code is gone. So you can’t just dirty fix the test but rather you have to rethink your approach. How could you fail at refactoring? Done right refactoring should never change the behavior of the code at hand.


*) Kent Beck is the author of “Extreme Programming Explained”, “Test Driven Development”, “Implementation Patterns” and more. All must-read books.