I’m currently reading the excellent Growing Object-Oriented Software, Guided by Tests. Although I have read less than half of it so far, this book has already changed the way I think about Test-Driven Development. It covers a broad range of TDD topics and their intersection with OO, but for now I want to focus on a very small part of this landscape – the naming of unit tests.
Names are important. Class names, method names, variable names – good names inform us, bad names mislead. I’m sure we all know that naming stuff can be tricky. However, a little discipline in choosing names can go a long way to improving the quality of unit tests.
Consider a tiny class I wrote recently while performing the leap year kata:
public class Year { public Year(int year) { // constructor type stuff } public boolean isLeapYear() { // code to check for leap year } }
I could have made isLeapYear()
static, and in fact I have done so in previous attempts. But it isn’t the design choices of the interface to Year
that I want to discuss.