9.4.2 Test-Driven Development (TDD)
Appearance
9.4.2 Test-Driven Development (TDD)
Imagine you're building a LEGO spaceship. Instead of just building it and hoping it flies, what if you first write down exactly how you expect it to fly (e.g., 'it should hover 2 inches above the ground') and then build it to meet that expectation? Test-Driven Development (TDD) is a coding approach where you write tests before you write the actual code. Here's how it works:
- Write a failing test: You write a small test for a new feature or a fix, and since the code for it doesn't exist yet, this test will fail.
- Write just enough code to make the test pass: You then write only the necessary code to make that specific test work correctly.
- Refactor (clean up) the code: Once the test passes, you improve the code's design and make it cleaner, without breaking the test.
This cycle helps ensure that every piece of code you write has a purpose and is working correctly. It's like having a clear goal for each small part of your program before you even start building it.
Bibliography:
- Beck, Kent. Test-Driven Development: By Example. Addison-Wesley Professional, 2003.