Hacker News new | ask | show | jobs
by evocatus 1510 days ago
Correct. In more mainstream language, quoth Gary Bernhardt, "functional core, imperative shell."

Functional programming is a convenient fantasy, a highly restrictive and controlled environment that allows us to make large assertions about bodies of code - "no network IO can take place here"; "your inputs will most assuredly be numbers that can be added together."

It's the equivalent of assuming the cow is a sphere [0]. A useful mental model, that ultimately breaks down upon contact with the "real world."

Hence the imperative glue code / monadic actions wiring all of the pretty, perfect abstractions together.

[0] https://en.wikipedia.org/wiki/Spherical_cow

3 comments

I think it's more like, out of this actual, physical cow, we are going to carve a perfectly spherical cow plus some... "other" parts.

That is, you can make chunks of your application functional - they just can't be chunks that touch the exterior. It's not a "mental model" - it's something you construct in the code.

Now, you may not be able to do that with all the "interior" code, either. Parts may have too much intrinsic state for functional programming to be a useful approach. But for other interior parts, hey, you like functional? Make it so.

Pure, deterministic code is easier to test than impure, non-deterministic code. There's no test environment setup for the former. No need to setup networking, DNS, PKI, etc. No need to have containers. For the "functional code" you just furnish inputs and compare to expected outputs.

Sort of like test vectors for cryptographic functions.

You still have to be careful to test all the edge cases (assuming you can't test the full domain of each function), naturally. But the fact that the functional core doesn't need setup means the tests of it have less startup and teardown overhead and so will generally run faster (unless they take so much time that setup overhead is in the noise).

As for the "imperative shell", you may be able to mock everything w/o having to change it, though you could also set up a test environment with all the external things it needs.

If they type system (or at worst norms of the language/community) enforces it in parts of the code, that's _much_ more than a mental model.