All code is guilty until proven innocent
Test Driven Development (TDD) is a software development process in which a developer writes test of their code before writing the code itself. The test-first concept encourages repetition of very short development cycles with the writing of just enough code to pass the test.
The main idea of the approach is to catch all bugs before they can make any harm to the program in development. It gives the developer an extra space to think about what they are implementing before they actually start implementing it. The divine mantra of Test Driven Development is “red/green/refactor”, where red means fail and green means pass.
Test Driven Development lifecycle
TDD was originated by software engineer Kent Beck, who described TDD lifecycle in his book “Test Driven Development: By Example”:
- Add a test
- Run all tests and see if the new test fails
- Write the code
- Run tests
- Refactor code
- Repeat
Pros and cons of Test Driven Development
- Comprehensive test coverage – all written code is covered by at least one test
- The greater level of confidence in the code
- Well-documented code
- Understanding the requirements before writing the code
- Continuous integration
- Increased productivity
- Reinforced mental model of the code
- Focus on design, interface, and functionality of the program
- No need in tedious debugging
- More stable system
Still, Test Driven Development has several cons:
- More code is required with TDD than without TDD
- False sense of security due to a large number of tests
- Maintenance overheads
- Additional time for an excessive number of tests
- Requires to know how to set up and use a testing environment
- Takes time and efforts to become proficient in TDD
- Too much TDD makes the code more complicated than necessary
- Discourages big picture design
- Higher cost
Follow the leaders
Many giant companies value Test Driven Development approach for its ability to increase accuracy and productivity, enhance confidence in the code, create roadmaps and logical paths for the product in development, as well as to make the code more modularized, extensible and flexible. Among them are big fishes like Google, Facebook, Spotify, Etsy, Buffer, and many more.
Your most satisfying code
A Ruby on Rails community is well known for their not compromised approach to a code quality and it treats TDD as a part of their conventions. Regardless of the fact that Test Driven Development may seem counterintuitive at the first glance, it can greatly help achieve good quality code and great test coverage. It helps make the code better structured, simpler, and better understandable and considerably reduces the number of bugs that pop up unnoticed.