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
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.
git stage -A git commit -m "working"
git reset --hard
- Check it out
- Put the batch files in the
- 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.
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
TCRyou want to do it often and this could become frustrating when it takes too long.
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.