Why Does TDD Work for Functional Programming?

Coming from a Java developer, TDD is always important. It is actually easier to do it for functional programming. I'll show you how...

Imagine you have a blank canvas in front of you.

You’ve come up with the first requirement for a function you must write. This is likely the input(s) and output(s).

Write your first test to assume those inputs and outputs.

It will fail. Great!

Now you can write the structure of your function, be it a lambda or traditional method declaration.

Now the test passes.

Update the test to fulfill the requirement(s) and fix your function.

So on and so forth, as you would do in any TDD pattern.

How does this fit functional programming?

TDD works best when you have easy to reason about requirements. This applies greatly to functional programming.

Each function does just one thing, so your code is already going to be modular and single purpose.

In my non-functional coding, TDD helps break down requirements that get out of control into smaller pieces.

Thanks for reading!

Want more TDD?

TDD Requires More Than 100% Code Coverage
Let’s look at the definition of TDD: > requirements are turned into very specific test cases [https://en.wikipedia.org/wiki/Test_case], then the code is improved so that the tests pass This means that you write a test for the smallest possible use case, then write code to make it pass, and repeat. …

Comments powered by Talkyard.