3 ms·
Nothing about pure functional programming requires unit testing all of your functions. You can decide to unit test larger or smaller units of code, just as you
by foldr 2y ago
Nothing about pure functional programming requires unit testing all of your functions. You can decide to unit test larger or smaller units of code, just as you can in any other paradigm.
- ninetyninenine 2y agoIn pure functional programming a pure function is unit testable by definition of what a pure function is. I never said it requires functions to be tested. Just that it requires functions to be testable. In other paradigms do not do this. As soon as a module touches IO or state it becomes entangled with that and NOT unit testable. Is it still testable? Possibly. But not as a unit.
- int_19h 2y agoHow do you unit test a local function that is a closure in pure functional code?
- ninetyninenine 2y agoOf course you can't unit test things with restricted scope. f(x) = x + 2 + 4 How do you unit test x + 2 or (+ 4) even if the operation is pure? You can't. Because it's not callable. It's the same thing with the closure. The only things that are testable are things on unrestricted scope. AKA global scope. Think about what happens if you have a "closure" on global scope. If you really want to test it then your "unit tests" which typically live on global scope, need to be moved to local scope. That's just the rules of scope. There is one special case here. If the parent function returns the local function as a value. But even in this case the parent and local function have to be treated as a unit. The unit test will involve first calling the parent, then calling the local. The parent and child function form a "unit" thanks to shared state and the parent is essentially "moving" the local function into global scope. Generally best practice is to use combinators if you want to maximize the granularity in which you can modularize your logic. I would even argue that closures stradle the line between pure and impure, so I actually avoid closures whenever possible.