Intro:

My mentor happen to share Uncle Bob’s blog post “Test First,” which has given me a great perspective on how TDD should be at the steering wheel of our code. I also read Corey Haines’, “Understanding the Four Rules of Design”and he too describes the importance of tests as they pertain to software design. 1 These two readings really get at the heart of what this post will focus on. Lets focus on why testing is an imperative, and why “Testing First” should be a priority for yourself and the software crafters around you.

Why Test?

Much of my testing before being introduced to RSpec was focused on writing code and testing outputs on the command line.It worked, but there were many instances where I would hit a snag later down the line, because I didn’t consider certain aspects of how things should and would interact. I’m not saying that this doesn’t happen when you use TDD as a methodology, but TDD does accomplish two very important things. It helps you to think small and it also provides you with some great documentation/specification of what your code is trying to accomplish.

For me, when it came to writing out an app, my mind would be racing about all the different components that the app would need in order to work. Now, I find that with TDD, if I open up RSpec and begin to write a test for my first method, it allows me to focus on the smaller aspects of my code. For instance, if I were to expect that my method returns a value in the end, well, there is my first test. I would write a test, allow it to fail and then write out the method that would allow my test to pass. This provides a great deal of focus, atleast for me, and alleviates some of the mind racing aspects of, “Oh! I need a method like this” or “What about if I tried this?!” nope… I’m just allowing the tests to steer me where I need to go on an individual method. Scientifically speaking, this is fantastic as your working memory can only hold four things at a time.

If someone were to send me a large codebase for an app, I would probably get lost in its functionality as I go from method to method. It is a lot to take-in. I’m sure I would eventually figure out how everything worked, but the whole process might be a little easier if the code was followed with some tests. The specs from the test would provide me with enough insight as to what each piece of code is trying to accomplish. Not only that, but if the tests were written well enough, I could probably replicate the code myself, which in itself is absolutely amazing. You could think of your tests as a step by step guide of how to build your app. When you think about it, it’s kinda beautiful.

Simply put, allow your tests to steer the path of your design to a completed product. Not only will your brain thank you for it, but so will the software crafters around you.

1 Haines, Corey. (2014, June 4). “Four Rules of Simple Design”. LeanuPub Publishing 2014