Old Guy New Trick

An old guys journey to learn how to code.

Tests Saved My Ass (more than once!)

Author: John on February 11, 2016

When I first started learning Ruby I attended a meetup at the Hash Rocket office in Jacksonville, FL.  While I had done some programming in the past, and was actively programming in Perl for the day job, I was totally new to current day programming practices.  At that meetup, my first one in fact, the talk was about TDD/BDD.  I didn't have a clue.  I sat, listened and took a lot of notes.  Automated tests, cool.  Test Driven Development, what?  Behaviour Driven Development?  Red-Green-Refactor?  Just picture my mind blowing up about then.

I liked what was discussed at the meetup and I could see how using TDD and/or BDD could be beneficial.  In my small, single task Perl script world we didn't use automated tests. Just TAYG - test as you go.  Fast forward a few months and I was building a Rails application for the day job.  I knew that I should have tests, but being new and not really knowing how to do TDD/BDD, I continued along the comfortable path of TAYG.  

I met a guy at our local Code and Coffee, Shaun, and I reached out to him for some help.  First thing he looked for was tests.  I looked down and said, "...I don't have any."  He busted on me for a bit about how I should have tests.  But I just wanted to get this one thing fixed as the application was about done.  He helped me out, but never let up on me regarding tests.

Going forward, I've used tests, via RSpec and Cucumber, to drive and support projects I am working on.  The first application that I did at the old job was re-written from the ground up - all using TDD.  I got push back from my co-workers at the time because they didn't want to be bothered (saw it as a waste of time,) but I stood my ground.  I then wrote a second application for that job in Rails, again via TDD.  The clients of the app wanted some changes so I updated the application.  And then I ran my tests.  Sometimes my changes were fine, and all tests came back green.  But once in a while, I did make a breaking change.  And my tests were there to point that out to me.

Keep in mind, both of those previous examples were fairly small applications and nothing compared to the project I am on now.  I really don't know what I would do, other than go insane, without a comprehensive test suite.  

I am currently working on a complex code base for an auction applicaiton.  We use RSpec for our unit testing and Cucumber for our integration tests. Our application doesn't use a database so the RSpec tests are super fast.  The Cucumber tests take time, but they are very thorough.  And both types of tests have saved my ass on more than one occasion.

Recently we have been working on optimizing the application.  To assist with that optimization, we converted our most heavily used views from HAML to ERB.  We have also been converting frequently used strings to  Constants. ( Perhaps a topic for another post, but these changes, along with some others, have greatly improved our performance!)  After making changes across many files, I kicked off our test suite.  The tests caught typos and other mistakes that were made during the conversion.  If not for our tests, I may not have found the error on my own.  The error may have also snuck by QA.  

Speaking of errors sneaking by....  We have a sister auction project that a different team is working on. They do not have a full test suite like we do.  A change was made and broke something, but the issue wasn't found until the software was released.  Bad, very bad.  :(

Morale of the story - use automated test for your code.  Even if you are new to programming, I strongly encourage and urge you to begin using TDD and/or BDD and not TAYG as soon as possible.  In my opinion, learning how to test should come second to learning the language first.  Then you can go check out the tools/frameworks that will enhance your productivity.

Learn Something New Everyday

Last Edited by: John on February 11, 2016