3 ms·
Not according to jon carmack. He stated he switched to pure functional programming in the intro which is basically stating all his logic is in the form of unit
by ninetyninenine 2y ago
Not according to jon carmack. He stated he switched to pure functional programming in the intro which is basically stating all his logic is in the form of unit testable pure functions.
- foldr 2y agoNothing 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.
- nuancebydefault 2y agoI found this [] article of Carmack. While reading, I understood there is a large set of gray shades to pureness of "pure functional" code. He calls being functional a useful abstraction, a function() is never purely functional. [] https://web.archive.org/web/20120501221535/http://gamasutra.com/view/news/169296/Indepth_Functional_programming_in_C.php https://web.archive.org/web/20120501221535/http://gamasutra....
- ninetyninenine 2y agoWhen people say pure functional programming they never mean the entire program is like this. Because if it were your program would have no changing state and no output. What they mean is that your code is purely functional as much as possible. And there is high segregation between functional code and non functional code in the sense that state and IO is segregated as much as possible away into very small very general functionality.
- tightbookkeeper 2y ago> pure fp No he didn’t.