Do it …go on…I dare ya!

Test Driven Development

RoarzOnRailz
thelearningcurve

--

What the heck is it & why should i do it?

TDD Despite sounding like some sought of STI actually stands for Test Driven Development. Its essentially a style of programming used to help create Agile code that en-twines 3 main elements. Testing, Coding and Refactoring. It looks a little something like this.👀

AGILE

I guess before we go to much further i should answer the question…… what on earth is agile… 🤠 Thanks for asking partner !

I wont go to far in depth about AGILE in this post as i wanted to specifically talk about TDD. But i will give you the general gist. The short of the long is that agile is somewhat of a methodology almost a mindset or a manner in which to approach Software development. It should produce working, workable/flexible programs that meet clients needs.

It was written by 17 authors after a meeting at a ski resort in Snowbird Utah in 2001. Each industry experts who wanted to create a uniform methodology to create software. They agreed on a few things 🤷‍♂️

The Agile manifesto was written on 4 main values and 12 main principles.

VALUES

  1. Individuals and interactions over processes and tools

2. Working software over comprehensive documentation

3. Customer collaboration over contract negotiation

4. Responding to change over following a plan

If you want to know more about agile and the 12 principles check out this link https://www.agilealliance.org/agile101/

OK!…OK!… i get it….back to TDD ← — Not an STI! 😆

TDD

Basically here is the gist

  • write a “single” unit test describing an aspect of the program
  • run the test, which should fail because the program lacks that feature
  • write “just enough” code, the simplest possible, to make the test pass
  • “refactor” the code until it conforms to the simplicity criteria
  • repeat, “accumulating” unit tests over time

So i get it!….it seems backwards! Why on earth would i write a test to fail?….Whats the point of writing tests when i don’t even have code yet?

As i have learnt in my recent assignment at Coder Academy. TDD is actually a really handy tool and if you do choose to encompass it into your coding there are definitely some benefits.

I was the hero…i didn't need TDD…..I knew what code i wanted to write…i Knew my code would work….and it did!

We were tasked with writing a terminal application in ruby. As some people may have seen in my previous post ← click. I decided to write a text based Llama battle game….Cause why not ! 😆 i decided not to write unit tests before piecing my code together. Although i had working code that did what i personally wanted to do there were a few things i hadn't considered. First of all my code wasn't DRY in other words i was repeating myself a lot! which isn’t a very readable professional outcome. My code was not AGILE. If i wanted to change anything or add anything there was so much to do. It wasn't going to be an easy task.

😈 A forgivable sin for someone who hadn't written or looked at a piece of code 3 weeks ago

After some mentoring and a push in the correct direction i decided to go back and write unit-tests for the methods within the classes i had created.

It took a bit of doing and ultimately it resulted in re structuring and re writing a lot of code in order for it to be correctly testable. The result ? My script was a lot DRYer. If i need to change or add anything its going to be a hell of a lot easier…making my code more AGILE. This became my REFACTORING.

Although not perfect and bearing in mind that I am only a beginner I am proud of the effort I put into making these changes and ended up with a much more professional result. I learnt the lesson. Even though I wrote the tests after the code which created a lot more work in restructuring 🤦‍♂️. The late attempt at using TDD still did its job and made a more AGILE and DRY code. Mission success! 💪

In summary what could i have done better & what did i learn?…

I think being my first assignment it wasn't a bad way to learn the lesson. Ultimately i don’t think i had the experience with piecing code together to write the single tests before writing the code. But i have learnt the benefit of TDD with proven results in my own work. I believe that moving forward with more experience it will become an easier task to write these tests and then write my code to make the tests pass which will ultimately create less work with a little more effort to begin with. It then simply a matter of refactoring the code.

I guess another overall benefit is that your piecing your code together in small sections.This keeps your code re workable which should ultimately make required changes a lot easier right?…Sounds awfully AGILE to me 😎

In short….just TDD pals…its worth the learning curve!

--

--