Photo by Talha Hassan on Unsplash

TDD WTF

Thu Apr 15 2021

Test-Driven Development (TDD) is a very popular coding method currently. But what is TDD?

Essentially, it means that a coder first writes a test for an expected result, which fails immediately, then writes the minimum amount of code necessary to make the test pass. It seems backwards, but I recently dove into this style, and honestly, I love it.

Also known as the "red-green-refactor" method (because when the tests fail the results are usually displayed in red type and when they pass in green, so you see red first, then green, then you refactor the code as needed), this style actually helps keep your code minimal and efficient. This means less (or no) spaghetti code, and no dead code.

Starting can seem intimidating, though, especially for new coders. "How can I know what to test for if I haven't written it yet?" The trick to understanding this, though, is that you are not writing tests based on nothing. You write tests that look for the results you want your code to produce.

Then it's simply a matter of figuring out how to get there.

RSpec (for Ruby) is a great introduction to TDD, and I will cover that in my next post.

Happy coding!