In a Hanselminutes episode from December last year I heard preeminent programmer and agile advocate Kent Beck talk about his intriguing approach to creating working, high-quality code. In line with the steps this approach involves he calls it test && commit || revert.
In lieu of the TDD circle of life (write test – test fails – write code – test passes – refactor – test fails …) test && commit || revert has you commit each time when after a code change your unit tests still pass. If they don’t you have to revert your changes and start afresh.
While this idea might sound counterintuitive at first, it not only helps with making sure that you only ever commit working code but in a “Zen mind, beginner’s mind.” it can provide the additional benefit of urging you to discard preconceived notions and approach the problem at hand from a fresh perspective. Additionally, this technique will motivate you to work in smaller increments, which is beneficial to both faster delivery and reducing the impact and therefore risk a particular code change has.