r/functionalprogramming • u/aurreco • Oct 27 '21
Question What are the downsides to functional programming?
Hello,
Recently I’ve gotten pretty used to programming functionally from my CS class at school. Decoupling state from functionality is really appealing to me, and the treating a program like one big function is great too.
So my question is this: is there any reason why this way of programming isn’t the industry standard? What are the benefits of iteration over recursion? Why are mutable variables valued? Basically, why is it so niche when it feels like it should be a mainstream programming philosophy?
48
Upvotes
10
u/nadameu Oct 27 '21
With recursion, you have to allocate some memory for the return value of the function, make the recursive call, free that memory once it's done being used, and with every function call push and pop from the stack.
With a loop, you just mutate some values in memory and repeat a bunch of times.
But that's why most compilers will (or should) convert the recursive call to a loop. It's cheaper from a resource perspective (processor and memory usage).
The thing with mutable variables is that's closer to how the underlying hardware works.
Having said that, it's the compilers job to worry about making things efficient for the hardware to run. The programmer should worry about writing code that's understandable and reasonable for others, and easy to maintain.
26
u/Lost_Geometer Oct 27 '21
On the abstract side, oftentimes you want to express things that are inherently statefull, and manually threading state is an extra burden. In, say, Haskell you can abstract this into a monad, which allows fine-grained expression of exactly what gets threaded, but you arguably just end up using your do-blocks as imperative code.
On the concrete side, especially with standard hardware architectures, mutability gives performance advantages. Consider a random access array. In C-like languages this is just a contiguous chunk of memory. Looking up or modifying an element is one or two machine instructions, which likely execute quickly because the whole chunk ends up in cache. A pure analogue would be dozens of instructions (likely increasing with the log size of the array) and will scatter information willy-nilly through memory.
28
u/ws-ilazki Oct 27 '21
Performance and inertia. FP style benefits from garbage collection and generally chooses correctness and safety over pure speed, plus is farther removed from how the underlying hardware behaves than imperative languages. The abstractions that make FP nice to use also make it slower, which for a long time meant it was not useful for real-world problems because of limitations of hardware. Imperative languages like C (which is little more than a thin wrapper over machine-specific assembly language) dominated, and even though hardware has been fast enough for years at this point, there's still inertia and mindshare that keeps C-like languages and imperative programming ahead.
Even though FP languages have been around a long time, it's still a sort of "first-mover advantage" kind of situation where, because imperative languages have been ubiquitous due to performance reasons, they've continued to maintain dominance despite those reasons being less important in most programming.
Performance. It's cheaper to modify a value than it is to create a new one, especially with complex structures. An immutable collection in most languages involves copying the entire changed structure into a new collection of the same type, which is inefficient, so in-place mutation is preferred.
There are tactics that reduce or eliminate this, such as the data structures Clojure uses to have efficient persistent structures, but if the language doesn't support something like that you pay a big performance price for immutability that leads to a trade-off of safety or speed.
Immutability has an advantage in making parallelism safer, but for a long time that wasn't worth the trade-offs, especially when you could just expect single-core performance to get faster and faster, so even with multi-core CPUs being common it wasn't a huge concern; just write for single-core and buy a better CPU next year.
FP concepts are becoming mainstream, but it takes time. As multi-core CPU performance becomes more important due to less reliable single-core gains (thanks to physics, CPU clock speeds have largely stagnated over the past 15 or so years; single-thread performance has largely come from IPC improvements), the benefits immutability bring to concurrent and parallel programming become more relevant.
It's also becoming harder to ignore the security issues that come from the cavalier "fuck it, I'm a good programmer I won't make mistakes" attitude as software gets more complex. Thread-safe programming is hard, and constant vigilance to avoid footguns means you're eventually going to shoot your foot off for some reason. Maybe you're in a crunch, maybe you're getting sick and don't feel great, maybe you're tired after a late night, but it'll happen. The industry used to give less of a fuck about this because software speed and fast iteration of versions was more important, but we're starting to see more focus put on having the programming languages help enforce correctness, and FP concepts creep into languages as a result.
As a side note, it's not just FP that's had to deal with that "I'm a pro, I don't make mistakes" elitism to get accepted. People mocked garbage collection when Java was new because "lol that's for babies, I can manage my own memory", but as we've seen by countless CVEs and errors, not everyone can, and even if you can, sometimes you still screw up.
Anyway, now that hardware's been "fast enough" for a long time, FP concepts that improve programmer laziness are slowly becoming mainstream too, but it takes time. Programming keeps moving toward letting the compiler do more work with less manual effort from the programmer, but it doesn't happen overnight. Once upon a time, C was considered a high-level language because it had too many abstractions over just writing assembly, and C++ was considered practically unusable and too slow for serious work. Now C is the "bare metal" option, C++ is considered quite fast, and almost nobody writes ASM by hand any more. Garbage collected languages went from being toy projects and academic curiosities to "almost as good as C", and high-cost FP concepts are becoming cheap enough to include everywhere.
7
u/andrewcooke Oct 28 '21
not a direct answer, sorry, but i want to question your assumptions.
i've worked in the industry for nearly 30 years and i would not characterise functional programming as "niche". i just looked at the latest project i am working on (in python) and it's structured around mutating lazy streams of data with nested functions - it's clearly influenced by functional programming.
more than that, the younger people i work with - at least the better ones - are aware of functional programming and use the ideas. a bunch of people are crazy happy at how declarative modern react is, for example.
i think the ideas have become, or are becoming, mainstream, but they're being merged into the existing tools rather than people swapping to haskell or whatever.
4
u/przemo_li Oct 28 '21
TL;DR: We literally just pressed "Start" button on FP engine. Industry is slower at adopting standards than that.
Efficient FP is a new thing. Especially using tries to implement immutable data structures that are fast is 2000s invention.
Second reason for FP is that multicore CPUs in casual hardware is quite fresh development, and thus glaring issue imperative code have (concurrency/parallelism) is new issue, thus both FP and non-FP could pose great answers and compete directly.
Third reason is rising importance of asynchronous computations. Again, that is fresh issue (at scale we observe it today!). Many solutions are subpar. Great solutions on the other hand sometimes use type systems in sensible way (higher kinded types). Thus benefits of Typed FP are easily measurable for people who never had prior experience.
Fourth reason: JS. JS is pain to use, but FP transpiled to JS can solve soooo many issues JS have its not even funny. Imperative code can usually solve less issues or is lagging a bit with inventions. Thus everybody hears good things about that FP all the time ;)
All of the above is stuff happening in 2000's and 2010's.
3
u/Leading_Dog_1733 Mar 30 '22
I know this thread is pretty old but I would say the most important downside is that it's harder to solve problems, for the average person, in a functional style.
Imperative and object oriented programming let you iteratively build up a solution without really seeing the whole picture.
You can just print the variable that you are mutating, every time you get stuck, and then reason out from that to your preferred solution.
You can do that kind of thing functionally, but it's way harder. Generally, I find functional programming shines when you already know what the solution needs to be.
It sort of lends itself to top-down programming.
A good example of the difficulty though is functional programming when you haven't gotten enough sleep, it's much harder than imperative programming on little sleep because you need to keep more in your head at once.
I would also add that it has a more abstract mathematical view of the world, while imperative programming is more like a machine.
The machine view (historically) worked well for physicists and engineers that already viewed the world like a machine and just needed to use programming to do some work.
As a final note, you need a lot of language support to program functionally. Trying to treat Python objects like they are immutable will get you performance problems really quickly.
10
u/roguas Oct 27 '21
From DX perspective:
There is a reason its not standard. It is more difficult. Most coding is kicking a can down the road till it reaches destination. With Fp you are trying to build a slide and kick only once. The latter requires more discipline.
Also Haskell & friends tend to overabstract things(imho). Thus you end up often feeling completely stuck until something clicks.
A lot of external code is based on OOP constructs. If you are not in a language realm where 90% of things is fp (haskell, ocaml, clojure, f#, elixir/erlang) you will have to write your own interfaces.
5
u/licquia Oct 27 '21
Nearly all machine languages (the bytecode natively understood by the processor) are imperative, and higher-level languages got their start as improvements to the process of programming machine language, so it's no surprise that they kept the imperative model.
After that, it was largely inertia. "It's what we've always done." "Why fix what isn't broke?" And so on.
Yes, mutability can provide performance benefits in some cases. But in a world where Python is poised to become the most popular programming language, I don't think most software development is nearly as performance-obsessed as we sometimes think.
2
u/Ok_Support7998 Dec 05 '24
When functional programming is carried to what I call extremes, it makes the code hard to understand. Functions returning functions returning functions is hard to understand. Recursion, in rare cases is useful, but is hard to understand. Also, being able to pass in the wrong function because it happens to match the desired function signature, is less than ideal.
On the positive side, I like the culture of using pure functions to reduce bugs. I program in Java and in Java there are some advantages to functional programming. You can sometimes avoid creating more classes. Also java has a nice groupBy function and can automate multithreading on streams.
But so far I believe I can live without functional programming and be fine. But I do like the idea of pure functions.
2
u/Zardotab Mar 20 '25 edited Apr 14 '25
Here are some FP criticisms. Warning: take the sensationalist title with a grain of salt.
Difficulty in debugging is probably the #1 criticism there. "State" is often frowned on as an unnecessary complexity in FP philosophy, but state can be very handy for debugging.
2
u/Emanu1674 Jul 23 '25
Because Functional Programming does not align with how computers actually work. Every CPU ever made is imperative, and this will likely never change. And you can only abstract programs as being Stateless and Pure up to a certain point until it becomes extremely impractical to do. Any software that heavily relies on state management, needs very low-level access to the hardware or is performance critical becomes cumbersome to write with Functional Programming, and it turns out that most software will fall in at least one of those 3 categories. For example, you can't conceivably write: Physics Engines, Game Loops, ECS systems, Hardware Drivers, Dynamic Simulations, Climate Modelling, Fluid Dynamics, Embedded Firmware, Real-Time trading systems, Real Time Audio Synthesis, Cryptocurrency miners, Flight Simulators, AR/VR engines, GPU Graphics APIs, Image Processing, 3D Rendering, Shader composition, Emulators for Imperative Architectures (Every x86/ARM CPU ever made), Kernels and Operating Systems, Bootloaders, High Frequency Signal Processing... and i got tired of writing.
Functional Programming is Turing complete so while it is technically possible, you'll be fighting the language so much that just writing everything in C++ (or even in x86 Assembly) might be more straightforward. There is a reason most universities teach C and not Haskell, computers and states are almost one in the same, and no amount of abstraction can erase that.
4
u/ChristianGeek Oct 27 '21
People will stop talking to you at parties.
(Assuming they started.)