3 ms·
The premise is not that there are no dependencies of any sort, or that you aren't allowed to pass complex structures from one to the other. Its more about split
by adamkl 7y ago
The premise is not that there are no dependencies of any sort, or that you aren't allowed to pass complex structures from one to the other. Its more about splitting logic (and by logic, he is referring to code with multiple, branching paths) from side-effects.
When you mix effectful code (rendering to the UI, accessing a database) with conditional logic, you need to resort to mocking (potentially complex) objects just to perform basic unit testing. He's simply stating that if you split those out into different functions (operations in his case) and use a simple function to integrate them together, it will make life easier, and I agree.
Its a very functional approach; get data, perform calculation, return data.
Like I said, I thought article did a good job of distilling an important idea into a simple pattern. It doesn't have to be an overarching architectural design; even just using it locally in a larger code base can make life easier.
*As an aside, I'd say there is a fundamental difference between:
'if a then b else c'
and
'other_function(a, quote(b), quote(c))`
The first, when tested inside the scope of its parent function needs multiple unit tests for full coverage, the second only needs one.
- tsimionescu 7y agoI think the last point is the most important - if there really is a difference between those two pieces of code, then the article does make some sense. If there isn't, the the article's pout is weakened. The reason why I think they are equivalent is that other_function may well be really reusable and have many different use cases, of which only one is right in my current use case. For example, it could be the 'if' special form in Lisp. That means, I can know for sure that other_function is itself perfectly correct, but still be calling it badly, so I still need to test whether my function does the right thing for any possible value of a, b and c.
- adamkl 7y agoTrue. I actually thought about the same 'if' special form when formulating my response (I've been playing a lot with Clojure lately, and your other_function looks a lot like a functional conditional when compared to an if/else), but figured that most people would be viewing this stuff through a more typical imperative lens, in which case, those constructs (if/else vs other_function) would likely be different.