The Basics of Test-Driven Development with a Focus on Data Structures
An introduction to a software development approach that highlights the importance of testing your code.
Learning about Test-Driven Development (TDD) is a great place to start if you want to improve your software development skills.
In this article, we'll cover the basics of TDD, focusing on data structures to make it a bit more fun 😁. Specifically, we will implement the basic functionalities of a Trie the way that is usually done when applying the TDD principles.
By the end of this article, you'll better understand how TDD works and how it can help you write better code.
Let's start!
What is Test-Driven Development?
Let's start by defining what Test-Driven Development is:
Test-Driven Development (TDD) is a software development process that aims to ensure the quality of the software by running a set of test cases more frequently. This is the opposite of creating the software and then the tests which the software will be tested against.
The philosophy behind this approach is to first implement a set of tests for every new feature being added to the software. These tests are expected to fail as the new feature has not been implemented yet. Once it is implemented, all previous tests and the new ones should pass.
By doing this, you "ensure" that the new feature is correct and all previous ones were not affected by adding this new one. If you do this continuously, every new addition is guaranteed to increase the value of the software.
Another important thing that the TDD approach enforces is that, to write proper tests, we need to have a solid understanding of every new feature being implemented. We cannot afford to know only some minor details of what we have to implement and then go and improvise along the way.
If we do it like this, we might as well compromise the quality of our tests because we can be biased by all the hacks we had to do to make our implementation work. Never write tests in a way that accepts whatever flaw the implementation might have.
Methodology
Let's summarize the steps you follow on TDD:
Add a new test:
This test should pass if, and only if, all the specifications defined in the feature are met.
Focus on the requirements before coding.
Run all tests:
The new test should fail.
Because it fails, it shows that new code is needed for the new feature.
This will also validate that the testing mechanism is working correctly and that the new test is not flawed in a way that will always pass.
Implement the feature:
The code you write should be the simplest one that ensures the new test passes.
Run all tests:
All tests should pass.
If they don't, the code should be revisited, and changes will be made until the tests pass.
Refactor as needed:
Go back and improve the code if needed.
Rely on the tests to ensure everything works fine after the refactoring.
Without further ado, let's see an example of a new "feature" to add using this methodology.
The New Feature
Let's assume we are working on a project consisting of a collection of data structures. We are tasked to add a Trie with the basic functionalities. How should we proceed?
Keep reading with a 7-day free trial
Subscribe to Algorithmically Speaking to keep reading this post and get 7 days of free access to the full post archives.