How does TDD compare with functional programming languages?

How does TDD compare with functional programming languages ​​like F # and Erlang?

I actually did not work directly with the functional programming language, but from what I saw, you have two sides of the equation, and they must balance, as in algebra or accounting; it seems a bit like TDD, where you define your expected results as Assert statements (one side of the equation) and the rest of the functionality goes into a class extracted from the test (on the other side of the equation), except that the IMHO functional programming seems a little cleaner.

Both have similarities, or am I overdoing it a bit?

+6
erlang unit-testing tdd functional-programming f #
source share
5 answers

Software Development v Development Methodology


They are orthogonal.

TDD is a software development approach that focuses on ensuring correctness by developing tests against specifications before writing production code. Functional programming is a paradigm for the development and implementation of software.

+22
source share

I think that TDD and functional programming (FP) are different in that TDD is a methodology and FP is a programming paradigm.

I would say that helping FP use TDD as an FP encourages you to make things deterministic whenever possible. Deterministic functions are much easier to test than non-deterministic.

+9
source share
Chris is right in saying that they are orthogonal. However, there are some aspects of functional programming that make testing functional programs easier.
  • Functional programs consist of functions and ensure that the function will behave identically in all contexts. This means that when you test a function in unit test, you know that it will always work . You do not need to check if this works if you connect it to some other environment.

  • Functions take arguments and return results , and that’s the only thing they do. This means that you can usually avoid ridicule and similar tricks, because you don’t need to check if the function is calling any object. You only need to make sure that it returns the expected result for the given arguments.

  • Finally, there are some good automated tools for testing functional programs. For F # we have FsCheck (based on QuickCheck, known from Haskell). These advantages come from the various properties of functional programs.

So, they have both different goals and essentially different things, but they have a good relationship (maybe like tea and teapot :-) they are completely different things, but they work very well together!)

+6
source share

You are right that when writing a functional program, you can use equational reasoning to determine the definition of a function. However, these considerations usually do not exist in any confirmed form (for example, in tests), therefore it is usually not considered that a function is proved correctly, as from the point of view of a machine or a person. Of course, you can use TDD with functional languages ​​(for example, using any .NET compatible TDD library with F #) to make sure that the functions were received correctly, but there are other testing strategies that may be more unique for functional languages such as using QuickCheck to randomly check specifications.

+2
source share

I think that a similar feeling between the two stems from the fact that with both functions the functions must be deterministic. FP functions should not have side effects, and side effects in test functions for object-oriented code should be removed by entering stubs.

0
source share

All Articles