TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Explain to me why functional programming is better like I'm stupid

4 pointsby jagtstronautabout 1 year ago
I have a coworker who really harps on functional programming. To me the block of .map(x).ifelseget().etc() is so much harder to read than defining and mutating variables, if&#x2F;switch statements, and loops. Also, if you get a stack trace, the line number doesn&#x27;t really tell you much if you are doing 4 things on a single line.<p>I have always wondered this since I have been confronted with it, but in an interview recently I was given a light docking for not using functional programming. Why is it better?

5 comments

kyproabout 1 year ago
&quot;Better&quot; is a very strong word to use without any qualifiers – better at what exactly?<p>In functional programming everything is predictable:<p>input -&gt; [some function] -&gt; output<p>Where in OO things are:<p>input -&gt; [some function + some hard to reason about state] -&gt; output<p>In OO functions tend to be &quot;impure&quot; because you typically have state which can always do something you don&#x27;t expect. This becomes even more problematic when you add parallelism to the mix because now state can mutate in ways that that is very hard to reason about.<p>With functional programming if something unexpected happens then you know that it&#x27;s the function&#x27;s logic rather than having to try to understand how some unexpected state is causing a function to do something you didn&#x27;t expect. For this reason functional logic much easier to test and verify because the surface area for unexpected results is dramatically reduced.<p>That said, functional programming is much less intuitive I think. We tend to naturally reason about the world as a collection of objects with state and actions, and when an action is performed state mutates.<p>Functional programming requires you get rid of the idea of objects which have mutable state and instead think about the world as a collection of processes which given stateT1, spit out stateT2.<p>I&#x27;ve noticed people from a math background find the purity of functional programming very appealing. Personally I tend to share that bias, but I still recognise that functional programming isn&#x27;t the best paradigm for most applications.<p>If you&#x27;re building a simple API or app for example, then I think the most important thing is writing code that&#x27;s easy to comprehend. If the domain is simple then the benefits of functional programming are relatively slim. It&#x27;s really only when you&#x27;re dealing with complex state and parallelism that functional programming really starts to have major benefits in my eyes. I think your observation that functional programming being harder to read is 100% correct, and this is its main trade off in my view.
评论 #39603164 未加载
proc0about 1 year ago
First the readability is an issue of the language not having FP as first class and also lacking the underlying concepts will prevent you from seeing the patterns.<p>The best metaphor I can think of... imagine a Rube Goldberg machine that works with water. The water starts on one side and moves through a bunch of contraptions, switches, and does a bunch of things at the same time, quite messy but it works. This is your typical programming experience. FP is like doing this but with pipes instead. You build all your pipes, then the water is sealed tight and the flow is controlled nicely.<p>That said, FP has strengths and weaknesses, but overall I would say it is definitely a superior paradigm, aside from the considerable learning curve and performance drawbacks.
jollyllamaabout 1 year ago
For one thing, your functions become easier to unit test, relatively speaking.
评论 #39594136 未加载
AnimalMuppetabout 1 year ago
First: You&#x27;re not stupid. FP advocates sometimes take the stance that you&#x27;re stupid if you don&#x27;t use FP, but that&#x27;s a &quot;them&quot; problem, not a &quot;you&quot; problem. <i>You&#x27;re</i> not stupid. If they can&#x27;t explain it to you, either they&#x27;re lousy at explaining, or FP is as clearly superior as they claim it is.<p>I think that FP matches how some people think - and <i>doesn&#x27;t</i> match how some other people think. When people that it fits learn FP, they have this &quot;aha!&quot; experience. They think if you don&#x27;t get it, you just haven&#x27;t learned enough to have the same experience. But in doing so, they are assuming that everyone else&#x27;s mind works the same as theirs does. I think that&#x27;s a false assumption. And I think it plays into the &quot;we&#x27;re obviously right, and anyone who can&#x27;t see it is unenlightened&#x2F;stupid&quot; smugness that FP advocates sometimes have.<p>Here&#x27;s what I have gleaned from various HN discussions in the past:<p>If you can think of your program (or a part of it) as a pipe, then FP is likely a good fit. If data comes in, gets modified, and goes out, that&#x27;s a decent match for FP. And if it is, that has some real benefits. Pure functions (ones that are <i>only</i> a function of their inputs) are easier to write, debug, and use than non-pure functions are.<p>But, as someone said here, &quot;real programs have state&quot;. Your program often is not just a pipe; there&#x27;s some state somewhere. In some cases, the state predominates. (This is often the case for OSes and embedded systems.) What are you going to do with the state? In procedural programming, you put it in a global somewhere. In OO, you put it in a class somewhere. In FP, you still have to put it somewhere. FP sometimes does some jumping through hoops to make it not look like you&#x27;re dealing with persistent state - but you still are, even if you wrap it up in something.<p>If you add multithreading, this gets far worse. FP is absolutely right that shared mutable state is evil, but if it&#x27;s necessary for the program you&#x27;re trying to write, I&#x27;m not sure that FP has a good answer. In OO, you put it in a class and protect access with semaphores. In FP, I&#x27;m not sure how you handle getting the newly updated state to the multiple threads that need to see it.<p>So FP isn&#x27;t &quot;better&quot;. It&#x27;s a better fit for some problems (and some programmers), and a worse fit for others.<p>All that said, in an interview, if they&#x27;re a place that uses FP, and you&#x27;re not an FP person, then it may be valid for them to dock you lightly for that.
评论 #39621413 未加载
Leftiumabout 1 year ago
I think your specific question can be answered by this video. It explains how <i>all</i> branching was <i>removed</i> by refactoring an imperative-style function into functional-style. The original code is on the top, the refactored code is on the bottom: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;GyYME8btMHE?t=1473" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;GyYME8btMHE?t=1473</a><p>Besides being shorter and easier to reason about, functional-style can be easier to test because there are fewer logical paths to test (statement&#x2F;condition&#x2F;branch&#x2F;etc coverage).<p>Of course, it&#x27;s still totally possible to write bad code in a functional-style!<p>Some little advantages of creating arrays in functional-style TS (vs declaring an empty array [] and filling it imperatively with `push()`):<p>- TS can autumatically infer the type of the array; no need to explicity type.<p>- Can declare the array as const&#x2F;readonly if it doesn&#x27;t change<p>---<p>The OP question was at the code-level, the small scale. However FP can be used at both the large and small scales. (And via traditional &quot;non-functional&quot;, &quot;inperative&quot; languages.) &quot;Functional programming&quot; is a paradigm and way of thinking; not just a programming language.<p>In fact, many of the good parts of FP have been borrowed by non-FP languages to the point people don&#x27;t consider them &quot;functional programming&quot; anymore[0]. (Like JS `.map()` and `.reduce()`. Even Excel macros now support first-class lambda functions[1].)<p>Another major pillar of FP is immutability (functional purity). Actually Grokking Simplicity[2] states it most elegantly: it&#x27;s not about avoiding mutation because &quot;mutation is bad.&quot; In fact, mutation is usually the desired result, but care must be taken because mutation depends on when and how many times it&#x27;s called.<p>So good functional programming isn&#x27;t about avoiding impure functions; instead it&#x27;s about giving <i>extra</i> care to them. After all, the purpose of all software is to cause some type of mutation&#x2F;effect (flip pixels on a screen, save bits to storage, send email, etc). Impure functions like these depend on the time they are called, so they are the most difficult to get right.<p>In general, I think these are the main promises of functional programming:<p>1. Program design&#x2F;code that is easier to reason about&#x2F;debug.<p>2. Programs that are shorter, yet more descriptive.<p>3. Programs that are more performant.<p>4. Programs that can be easily parallelized.<p>I wrote a whole article expanding on this: <a href="https:&#x2F;&#x2F;blog.leftium.com&#x2F;2023&#x2F;04&#x2F;more-performant-testable-code-with_28.html" rel="nofollow">https:&#x2F;&#x2F;blog.leftium.com&#x2F;2023&#x2F;04&#x2F;more-performant-testable-co...</a><p>---<p>Here&#x27;s a short list of my top introductions&#x2F;explanations of FP:<p>- Grokking Simplicity: <a href="https:&#x2F;&#x2F;www.manning.com&#x2F;books&#x2F;grokking-simplicity" rel="nofollow">https:&#x2F;&#x2F;www.manning.com&#x2F;books&#x2F;grokking-simplicity</a><p>- Functional Core, Imperative Shell: <a href="https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;18043058" rel="nofollow">https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;18043058</a><p>- ScottWlaschin: <a href="https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;21879368" rel="nofollow">https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;21879368</a><p>- Greg Young on Event Sourcing: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;8JKjvY4etTY" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;8JKjvY4etTY</a><p>- <a href="https:&#x2F;&#x2F;project-awesome.org&#x2F;stoeffel&#x2F;awesome-fp-js" rel="nofollow">https:&#x2F;&#x2F;project-awesome.org&#x2F;stoeffel&#x2F;awesome-fp-js</a><p>[0]: <a href="https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;21280429" rel="nofollow">https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;21280429</a><p>[1]: <a href="https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;26900419" rel="nofollow">https:&#x2F;&#x2F;hw.leftium.com&#x2F;#&#x2F;item&#x2F;26900419</a><p>[2]: <a href="https:&#x2F;&#x2F;www.manning.com&#x2F;books&#x2F;grokking-simplicity" rel="nofollow">https:&#x2F;&#x2F;www.manning.com&#x2F;books&#x2F;grokking-simplicity</a>
评论 #39621386 未加载