In Test First Development keeping things simple is very important. It is so important that one of the core principles is to do the simplest thing to get a test to pass. Once the test is passing then you let the additional tests that are needed drive out the more complex behavior, as necessary. Recently while giving a presentation on test first development someone challenged code that I had written questioning if it was really the simplest thing that could work.
Here is the situation, please keep in mind that I have obfuscated things slightly to protect my client but this is very close to the real code:
Here is the test that I wrote:
1: public void testParseTransactionTotal() throws IOException {
2: parser.parse(cashRegister.getReceipts()); 3: Transaction transaction = 4: parser.getTransactions().get(0); 5: assertEquals(TEST_TRANSACTION_TOTAL, 6: transaction.getTotal()); 7: }Here is part of the code that I wrote to get the test passing. This is the part where the person in the class challenged if this was really the simplest thing that could work:
1: public BigDecimal parseTransactionDetail(String registerReceipt) {
2: return new BigDecimal(
3: registerReceipt.substring(41,61).trim())); 4: }It is a fair statement to say that this is not the simplest code that could work. At the moment in time that I wrote this code I was following the same implementation pattern that I had been using for other tests. In my haste I neglected to do the simplest thing first to just get the test passing, which in hindsight would have looked like this.
1: public BigDecimal parseTransactionDetail(string registerReceipt) {
2: return new BigDecimal("0");
3: }This code would have gotten my test working and failing. Instead I wrote the code that I knew would get the test working and passing. In my mind I streamlined the process. While this might be okay for me doing my own development (and even then you could argue against it) it is definitely not the right thing to do for a presentation to a new group of developers.
Lesson Learned! I will be more diligent to do things in the smallest steps possible, especially for any classes or presentations.


0 comments:
Post a Comment