Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the functional programming recommendation for static methods influence testability?

The more I dive into functional programming I read the recommendation to favor static methods in favor of non-static ones. You can read about that recommendation in this book for example:

http://www.amazon.de/Functional-Programming-Techniques-Projects-Programmer/dp/0470744588

Of course that makes sense if you think about functional purity. A static function stands there and says: "I do not need any state!"

However, how does that influence testability? I mean, isn't it that a system with a lot of static methods becomes a pain to test (since static methods are hard to mock)? Or does mocks play a minor role in functional programming and if so: why?

EDIT

Since there are doubts if the book really makes that recommendation. I will quote a little more. I hope thats ok for Oliver Sturm.

Use Static Methods

Static methods is one of the basic ideas worth considering as a general guideline. It is supported by many object oriented programmers, and from a functional point of view, functions can be made static most of the time. Any pure function can be made static. (...)

Some may argue that the idea of always passing around all parameters means you're not exploiting the ideas of object orientation as much as you could. That may in fact be true, but then perhaps it is because object orientation concepts don't give as much consideration to issues of parallel execution as they should. (...)

Finally, a guideline to recommend: when you have written a method that does not require acces to any field in the class it lives in, make it static!

Btw, there have been good answers so far. Thanks for that!

like image 258
Christoph Avatar asked Nov 29 '11 09:11

Christoph


People also ask

Are static methods testable?

Well-defined Static methods are perfectly testable. You see, the problem isn't with the method itself, it's with the method's dependencies. If the method and its dependencies (and the dependencies' dependencies) are idempotent, then there's no problem.

Why static methods are hard to test?

Why static code is difficult to test? Because the logic is assigned to the class definition itself not to instances. Considering the fact that you can't overwrite a static method in Java, you can not overwrite a static behavior in a dummy implementation of the class for your unit tests.

Why use static methods?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

How functional programming mattered?

The contrast in style is apparent: functional programs are more declarative and often much shorter than their imperative counterparts. This is certainly important. Shorter and clearer code leads to improved development productivity and higher quality (fewer bugs).


2 Answers

One way of looking at this is that for functional programming you only need to mock state (by providing suitable inputs) that is required by the specific function. For OO programming you need to mock all of the state required for the inner working of the class.

Functional programs also have the side benefit that you can guarantee that repeating the same test with the same input will give the same result. In classic OO you have to guarantee not just the same input, but the same overall state.

In well architectured OO code, the difference will be minimal (as classes will have well defined responsibility) but the requirements for a functional test are still a strict subset of the equivilent OO test.

(I realise that functional programming styles can make use of OO via immutable objects - please read mentions of OO above as 'object oriented programming with mutible state')

Edit:

As pointed out by Fredrik, the important part about functional methods is not that they are static, but that they do not mutate the state of the program. A 'pure' function is a mapping from a set of inputs to a set of outputs (same input always gives same result), and has no other effect.

like image 90
mavnn Avatar answered Oct 22 '22 00:10

mavnn


I think that static methods per se is not the problem, the problem comes when they start to operate on static data. As long as the static method takes input as argument, operates on it and returns a result, I see no problems testing them.

Even when I am not pursuing a functional approach in my code, I tend to make methods static whenever I can. But I think very carefully before introducing static state, or a static type.

like image 28
Fredrik Mörk Avatar answered Oct 22 '22 00:10

Fredrik Mörk