At Codica, we apply various software development methodologies, depending on project needs and customer’s preferences. We decided to create a series of articles devoted to the project development approaches we work with, to show you our approach and provide a glimpse of our working processes.
There are several approaches to building an application. One of them is creating high quality and maintainable code which follows from the usage of Test Driven Development (TDD).
There exists quite a common opinion that TDD is more time- and cost-consuming. However, our experience shows that it is the case for the short-term only. TDD approach has obvious advantages in the long run, which attracts both customers and developers.
To begin with, let’s talk about what TDD is, and then discover why test driven development can be the best option for your project.
What is TDD?
Test Driven Development is a software development practice enabling developers to create proper specifications about how their code should be written and implemented. Fundamentally, TDD is a practice when a programmer writes a functional test before building a code.
The idea of TDD originated in the middle of the 20th century, and since then has been applied in many areas. TDD was afterward rediscovered in 2002 by Kent Beck, an American Software Engineer and the creator of Extreme Programming methodology. He described it in his book Test Driven Development: By Example.
The original description of TDD was in an ancient book about programming. It said you take the input tape, manually type in the output tape you expect, then program until the actual output tape matches the expected output. After I'd written the first xUnit framework in Smalltalk I remembered reading this and tried it out. That was the origin of TDD for me. When describing TDD to older programmers, I often hear, "Of course. How else could you program?" Therefore I refer to my role as "rediscovering" TDD. - Kent Beck
In case you’re interested, you can find extensive research about Test Driven Development evolution in the article You won’t believe how old TDD is.
The image below shows a typical test driven development cycle.
This process includes the following stages:
- Writing a test that fails because of code absence (red).
- Writing a code after which test is passed (green).
- Refactoring - checking the code structure and its improvement without changing its external behavior. The expected result of refactoring is obtaining a perfectly written code.
By repeating this process for every single piece of functionality, a developer obtains full test code coverage, which will provide excellent design and easy application maintenance in the future.
What are the advantages of TDD approach?
Let’s take a closer look at the Test Driven Development methodology, and explore the main gains your project will get from this approach implemented.
1. Better program design and higher code quality
When writing tests, programmers first have to define a goal they will achieve with the code piece. Developers estimate an experience it will give and the way it will match with other pieces of the entire code. A developer keeps in mind everything, from an interface to a work plan.
Besides, TDD strongly evolves with the DRY principle. It says programmers should avoid repeating code in different system parts. The principle invites developers to use small classes and functions for particular requirements. This helps clearly define the system objects and keep the codebase successive.
Next, when developers build a code with the TDD usage, it levels up the quality. You know why? Because all the possible mistakes and errors are already taken into account. Here, developers write the necessary tests, and the code, therefore, to avoid all the failures. Consequently, the code appears to give better results.
Finally, when your code has a clear structure and fits the test requirements, it’s very simple, straightforward and brief. There are no massive code blocks stalling the solution functionality. Even if any, programmers refactor the code to simplify it the whole structure.
In a nutshell, there’s a huge advantage in all of the TDD aspects. A beautifully-structured code is easier to modify, extend, test, and maintain. The cleaner and simpler the code is, the fewer efforts you’ll have to put into modification or updates. And this directly contributes to your project’s success.
Subscribe to our monthly newsletter.
2. Detailed project documentation
When writing tests for particular requirements, programmers immediately create a strict and detailed specification. It already includes all the likely users’ actions.
In this way, we already have a scenario determining all the way forward. You don’t need to create any heavy documentation, which relieves both you and programmers of additional efforts.
For the unit test to be comprehensive and behave as appropriate, programmers proceed from user’s stories. The results hit with simplicity and consistency, thus making the code as close as possible to all the requirements.
3. TDD reduces the time required for project development
You would probably make a conclusion that TDD postpones your project delivery date. But is it really so?
According to the study held by Eric Elliott, founder of Parallel Drive, maintenance of an application made without Test Driven Development implementation may take twice as much time as of an application created with TDD.
How does it work?
To begin with, the TDD approach allows identifying any issues very fast due to fast feedback.
When writing a code without TDD, programmers need some time to understand whether the code will work right or not. With Test Driven development, when tests fall where they should pass, developers understand that something’s wrong immediately. Consequently, it saves time during the project development phase, and a team can fix the code right away when detecting a breakage.
Secondly, one of the key differences the TDD approach offers is fewer bugs or errors. Having fewer bugs, you will spend less time to fix them - it’s as simple as that.
Which means that less time is spent on the bug fixing or maintenance stage if compared with other development methodologies used.
Additionally, provided that the code quality is significantly higher with TDD, this will, of course, reduce the time spent on the code maintenance.
So we can say that in the long run, TDD contributes to faster project completion.
4. Code flexibility and easier maintenance
When writing code, developers strive to get fewer bugs, which considerably influences on business spending.
According to the IEEE Software publication, implementation of Test Driven Development reduces the percentage of bugs by 40 - 80 percent, which consequently means that less time is required for fixing them.
Being a part of TDD process, refactoring stands for optimization of existing code to make it more readable and easy to introduce. It encourages a pure and attractive code structure. Sometimes, even non-developers can read the code and understand its message.
Equally important component is the appearance of the whole structure. Even at the stage of building tests, developers respect clean interface. It means that the actual code already includes this part, and it matches both a client’s and users’ needs.
5. With TDD you will get a reliable solution
With TDD, both you and developers can be sure about the reliability of the developed solution. That is, tests help to understand if everything goes right after refactoring or adding a new feature.
Without TDD, developers go blind with the latest changes, and they are not quite sure how recent development will work with the perfect code built previously. And any new changes can break the solution.
Essentially, it’s yet another example of the fact that TDD allows being Agile and worrying less about the effects of further changes.
6. Save project costs in the long run
And last but not least - with TDD implementation, the cost of a project development will significantly decrease.
This is the result of all the advantages mentioned previously, namely the following:
- The code that you will get will be maintainable, flexible and easy to extend.
- The solution will be reliable for any further enhancements or development.
- You will have ready documentation that can be used further on for any purposes.
- The quality of the product developed with TDD is significantly higher.
Considering all the above, with TDD such project phases as bug fixing and maintenance will take less time, consequently, this will mean smaller budget required.
Additionally, a study held by IBM System Science Institute shows that bugs fixing cost varies depending on the stage discovered. Below you can see the statistics showing costs of code maintenance created without TDD.
Defects found in testing were 15 times more costly than if they were found during the design phase and 2 times more than if found during implementation.
Is TDD used in Agile methodology?
Test Driven Development goes hand in hand with the Agile software development method. Moreover, TDD provides a great contribution to Agility. Let’s see how this works.
One of the Agile ingredients is Extreme Programming or XP, which gave rise to unit tests and TDD.
Extreme Programming technique allows developers to write unit tests so that each of them carefully checks a small piece of a software code. Thereby, teams verify whether everything operates properly. Consequently, TDD enables developers adding, editing or deleting all the features swiftly and efficiently.
The figure below shows the adoption of the TDD methodology by Agile teams in their work (as of 2010). As we can see, Developer TDD was used by 53% of the teams questioned.
To wrap this up, we can certainly assume that implementation of Test Driven Development significantly improves projects results.
Additionally, we can certainly say that developers who work with TDD approach, have more structured thinking and usually provide better estimates. This, of course, contributes to the advantages of implementing this approach in project development.
There is a variety of best practices strengthening the final code along with TDD, such as CI, Code Reviews, Refactoring, and others. But to get a solution which really meets all your requirements, you should consider other obstacles, apart from the technology.
At Codica, we have expertise in all the best practices, including TDD, applying them as appropriate.
Our case-by-case approach and vast experience allow us to choose the best techniques which will perfectly suit your particular project.