Selective Test Driven Development / Unit Testing
Digressing a bit… I love jsfiddle and things like dotnetfiddle since they allow me to prototype stuffs without having to go through the code, wait for compile and run to see what’s coming out at the other end, but that’s a different story. *Digression mode OFF*
I found that most of the time when I really feel that I need to fall back to TDD / up front unit testing is when dealing with compilcated logic like rules that depends on multiple if statements… like…
If it’s Tuesday and it is raining and you’re wearing red socks then do this
If it’s Tuesday and it is raining and you’re wearing blue socks then do that
If it’s Tuesday but it’s a leap year and it’s between January to March, and you are wearing either black, blue or red sock but also wearing sweater, then do something else.
You get my point…. some codes are just harder to hold in your head and get right. Complex boolean logic is one such thing. This type of code is a perfect candidate for starting with TDD.
I had to work on some brownfield code for a client which has this kind of logic to determine if a node in a treeview or the screen entry is enabled… what a headache :). Unit Testing came to the rescue. The result is… I am more confident now that the boolean logic that I put into the code works as expected since I coded enough test to cover every conceivable scenario of the business rule / requirement.
What do you do?