Chapter five of The Clean Coder overs the method of programming know as test driven development or TDD. As the name implies the the process of writing code is driven by test cases. The idea is as you determine what needs to be coded you write a unit test that will verify a specification is meet, once you have the test written you write the bit of program. As soon as you have written the piece of program you can run the test and almost immediately know if the code works. This test driven process could be taken to an extreme where all the test are written before any production code is even started, this obviously doesn’t make sense. To balance the test code and production code the following three rules should be followed.
“1. You are not allowed to write any production code until you have first written
a failing unit test.
2. You are not allowed to write more of a unit test than is sufficient to fail—and
not compiling is failing.
3. You are not allowed to write more production code that is sufficient to pass
the currently failing unit test.”
TDD has several benefits. One of the biggest is the fact that having a test suite that is up to date allows you to ensure that your latest changes have not broken something in another location. This is especially true if you stick to the three rules from above. The TDD process also means you will end up with very high code coverage which is a great way to ensure you program works as expected.
I have tried using the TDD process several times and find it a little hard to get used to although I know it is something that would be worth using. Many times I start a program and want to sit down and just start writing production code. The biggest pain in this is once I have written a fair amount of code and compile and run it there are often errors that I must go back and find then fix. If I were to follow the TDD strategy issues would be caught right away.
The sixth chapter is all about practice. In the beginning of the chapter the author focus on the history of computing and how far technology has come. It used to take a great deal of time to compile and test code, but now computers are both faster and cheaper. Most developers can afford to practice. This is something I really wish I had more time to do. I would especially like to practice with other languages. Today, most of the code I write is in Java as that is the language the school has chosen to teach object orientated programming with. I have, in the past, written programs in Python, C/C++, Lisp, Prolog, as well as a few others. The problem is without practice I forget the syntax. I can write a simple program in Python and C/C++ but would need to use a lot of references for more advanced programs and in the case of Lisp, Prolog, and Ruby I can’t remember where to even start.
From the blog CS@WSU – :(){ :|: & };: by rmurphy12blog and used with permission of the author. All other rights reserved by the author.