3 ms·
I wish! Unit tests that verify the in-order invocation of several different void methods have nothing to do with reinvention of purity. I think a better argum
by mbrock 13y ago
I wish!
Unit tests that verify the in-order invocation of several different void methods have nothing to do with reinvention of purity.
I think a better argument would be: the best way to write testable units is to write pure functions. This argument was made for example in Hughes's Why Functional Programming Matters. But it's not really a question of OO vs FP.
There's a seemingly important pattern that I don't know if there's a common name for. It's something like "Replace Side-Effect With Value." Or maybe "Inversion of Side-Effecting." This is relevant even in Haskell. Either you write your whole program as a big messy monad transformer stack with IO at the top -- I don't know how to unit test that, much less prove anything -- or you define the application logic as pure functions that give instructions to a simple driving "side-effect interpreter."
XMonad, the Haskell window manager, is written in this latter way, with lots of unit tests too. There's a cool video walkthrough of how the code works -- http://www.youtube.com/watch?v=63MpfyZUcrU http://www.youtube.com/watch?v=63MpfyZUcrU -- and a document called Guided tour of the xmonad source -- http://www.haskell.org/haskellwiki/Xmonad/Guided_tour_of_the_xmonad_source http://www.haskell.org/haskellwiki/Xmonad/Guided_tour_of_the....