Nowadays, it’s hard to find someone who hasn’t heard about Test-Driven Development (TDD). But many people argue and/or try to understand why it’s so popular and why it’s popularity grows. The answer, as I see it, lies within business’ requirements for software applications always changing. Developers are focusing on speed-to-market solutions and looking for ways to boost the frequency of software releases. Continuous delivery trend requires more efficiency in development processes. That is why many developers are considering using automated unit tests or going further into integrating TDD.
Test-Driven Development is the practice of writing a test for a piece of required functionality, before writing any implementation code. This test should fail when first run, and then, you write the code to get it to pass. It doesn’t have to be the most perfect code as long as the test passes. Once it does, you can safely refactor your code.
There are opposite opinions about Test-Driven Development. Some developers like and sometimes suggest to use at any project. Other ones say it increases development time and gives little profit for the development processes. True, it’s not a good idea to use TDD in a rapid prototype project, where the main requirement is to show something quickly. And it’s fair, because of a lack of statistical evidence, it’s hard to say TDD definitely delivers. But we must not forget there’s no such thing as a one-size-fits-all solution in software development. And TDD must not be considered as such.
But even if TDD doesn’t fit your needs, I consider this technique is good to learn as for many developers it can give new understandings of project design and the new ways of coding more effectively. Below, I will describe the main benefits, why I think so, and what will be useful to learn from TDD.
Better Understanding of the Program Design
Often, I have seen the same mistake young developers make: they write code before thinking how it will be integrated into the project and how it will be used.
When writing tests, first you really need to think about what you want from the code, how it will be used, or how other code will integrate with it. You think about interfaces and plan your work before implementing the real code. And this is a good thing to learn, even if TDD doesn’t fit your current project.
There is another developer’s problem that Test-Driven Development also helps fight. While working on one part of the logic, they switch to another part to make updates without finishing current changes.
The nature of TDD is to keep your focus on the specific part you are working on, without jumping around other parts of the code. It’s good for productivity, as it reduces the context switching overhead, because if you do so, you always leave some part of the code unfinished. And this may cause longer development time and even more bugs.
Better Code Quality
Testing a piece of code forces the developer to define what that code is responsible for. If it can be done easily, the code’s responsibility is well-defined, with high cohesion. And if you can unit test this code, you can bind it to the rest of the project as easily as you can test it. It has low coupling to other pieces around them. High cohesion and low coupling are used to define a good maintainable design. In such way, we create SOLID code.
Flexible and Modular Code
TDD teaches us to be more focused on a small part of code. But one more effect comes out of this. Developers think in terms of isolated parts of code that can be integrated later with other parts.
It’s close to code quality, but I prefer to separate it for one reason. Microservices architecture become handier each day on big and complicated products. Using this architectural approach for each service, we can use a tool mostly suitable for solving a specific business requirement and the quality of the product rises. And the ability to see and plan such services before you write code is a very useful skill for every developer.
After you have your tests passing, they will have your back from now on. Because every time you want to be sure new changes don’t break previous logic, you just need rerun your tests. Easy and quick enough, isn’t it? You can also use TDD if working with legacy code or code someone else has already written. Use it for the new code you are about to write. At least if something goes wrong, you will know for sure if there are problems in your code. Such things can save a lot of time debugging and trying to figure out what is going wrong.
So, using TDD saves a lot of time and efforts for code refactoring and writing new code.
Up to Date Documentation
Besides everything above, TDD tests can serve as the documentation for a developer. From my experience, I know few developers who like coding and writing documentation to that code at the same time. If you need to know how a class or specific code works, you can read through the tests and find this out. This is possible because, when you write tests, you plan and think how you want to use the class. So, you can see all expected inputs, required methods and what outcome to expect. This comes in very handy when others are coming to work on the project.
Perhaps, we can have such info from the interfaces as well. Interfaces will not validate in seconds that your new updates broke the class but tests will.
Anyone can name other benefits regarding TDD, but these are the most interesting and important to understand. If summarizing everything, speaking about TDD benefits in few words, we can say TDD provides productivity, quality, and confidence to the products. Learning TDD and its strong side will help us produce more high-quality and flexible code updates by using less time for all this. And, unless we are building prototypes or a quick demo project, anyone who hasn’t used TDD should try it.
Got a project idea? Let’s talk business
Also published on Medium.comments powered by HyperComments