by michaelteter · 3y ago · 0 comments
Writing tests is much easier and requires much less code, and bugs are much easier to track down (since you have less surface area of "what might have modified this object I now hold?").
Since a functional approach also goes well with single-responsibility-principle and composability, you can end up with multiple times more actual functions. They're all (ideally) very conceptually simple, but the larger number of more specialized functions means you have to work a lot harder on choosing meaningful names. And to write meaningful names of specific functions, you can end up with some quite long names. This is actually quite ok as it makes things much more readable, but some people have some strong negative reactions to seeing 4-6 word function names. But at least in this case (Java being the most famous counter scenario) each function probably has more real value as it's not just OO layer artefacts.
This is most clearly seen with conversation functions between types defined in different libraries. A collection class might have a method like "from_Array".
I recently wondered why Casey Muratori (RAD Game tools, Handmade Hero etc.) seems to write C++ in a very C like fashion plus a few select features.
Here's a discussion: https://hero.handmade.network/forums/code-discussion/t/453-w...
One of the takeaways is that he prefers the simplicity of C but uses operator overloading for similar reasons as you describe. One example includes addition of vectors, which is certainly a thing you do often in game and graphics related code.
You can find this pattern in a lot of places if you are a little bit flexible with your definition of "functional".
For example, why would we not consider some combination like SQL and PHP to be hinting at this exact same kind of archetype? You've got a crusty outer shell of yucky hackarounds (PHP) that talks to this (ideally) well-normalized & clean data store (SQL). Assuming you "stuff as much as possible" into core part of the solution, you could swap the outer shell for anything you desire without much headache.
From the outside it's stateful, because well you are interacting with a DB.
From within the PHP codebase, it's imperative and requires discipline and extended knowledge of any API you call to understand whether something mutates.
The only time you can view it as actually stateless and somewhat functional is when you test or debug an integration from idempotent requests to responses without interleaving mutations.
This holds for a Haskell webapp too.
For example, I find in TypeScript, most libraries and the language itself are not written with immutability in mind so there's constant gotchas. So while I strongly prefer immutable data structures, going against the grain to use immutability everywhere usually isn't worth the frustration. That for me doesn't mean immutability is a bad idea though, it's more that the libraries and languages support is lacking.
Fundamentally the user isn't functional - if you ask them what they want to do they'll say different things at different times. You can have a 100% purely functional programming language that works like a calculator (with no memory) - the user can put in expressions and the language will evaluate them - but generally users want their language to be able to do things that are non-idempotent and non-time-symmetric and so on. You can, and should, push that part to the edge, but it needs to be there somewhere.
2) Not all frontends have functional APIs, you may be stuck writing a lot of FFI glue code.
3) Front end code often gets pushed to devs with different experience and skill sets, in my experience they’re probably less likely to have functional or hard CS backgrounds.
4) Harder recruiting if all the devs need to be proficient with FP.
I personally don’t find any of these reasons compelling enough to negate all of the benefits of FP.
Answers saying FP isn’t suited for IO or for interaction with users are rhetorically interesting but contradict my experience.
I wouldn't be qualified to argue the merits of FP on frontend, but on the backend it really seems to have value.
That way you can use pure functions on the inside. Pure functions don't allow for side effects.
For a functional shell, your program's entry point would be composed of pure functions describing effectful actions to take. The necessary computations get bubbled up to the entry point during evaluation, at which point the runtime / compiled program executes them.
(At least, this is the abstracted perspective you should view the purely functional source code from. The final program itself in practice is, of course, effectful throughout).
In .net, most UI stuff is easy on C# and semi-painfull on F# because of limited tooling and examples. I don’t see any theoretical reason why a usable functional language could not be used everywhere. The ecosystem maturity and tooling are the limiting factors.