8 ms·
How to Lose Functional Programming at Work
- creakingstairs 4y agoI grimace whenever I think about the time where I went a bit ham on FP as a junior engineer. I genuinely feel sorry for whoever is maintaining that piece of code.
- muraiki 4y agoHa, I did the same thing, and in Perl nonetheless! My poor boss. I guess he forgave me since we’re still friends! Edit: A good book: https://en.m.wikipedia.org/wiki/Higher-Order_Perl https://en.m.wikipedia.org/wiki/Higher-Order_Perl
- pharmakom 4y agoAt least pure functions are amenable to gradual refactoring. Now an overzealous OOP junior... That’s dangerous!
- baby 4y agoI don't think one is easier to refactor than the other
- TurboHaskal 4y agoI'll take an over-engineered enterprisey Java monolith over a Haskell codebase written by type astronauts any time of the week. I mean, at least the former will be somewhat working and with proper IDE support.
- tome 4y agoHaskell programs are so phenomenally easy to refactor in deep and complex ways it's unreal. I have been astonished over and over again. (That said the state of automated, trivial refactorings (like renaming identifiers) is probably much better in other languages.)
- bazoom42 4y agoAre you saying you cant write bad code in functional style? Because then you have drank too deeply of the cool-aid. No fad, tool or paradigm will prevent inexperienced developers from writing bad code. Education, mentoring and code review can help though.
- pharmakom 4y agoNo - maybe read my comment again?
- bazoom42 4y agoPeople were saying the same thing about structured programming and object oriented programming back in the day. Lets say you have a huge overly-convoluted Haskell program. Somewhere deep down a call hierachy of pure functions you need to print something to the console. That is not easy to refactor. Or vice-versa you have a huge convoluted program where everything happens inside an IO monad because at some point something is written to the console. Now you realize you dont need to write to the console. Pure functions are great, but they are not a panacea.
- tome 4y ago> Lets say you have a huge overly-convoluted Haskell program. Somewhere deep down a call hierachy of pure functions you need to print something to the console. That is not easy to refactor. > Or vice-versa you have a huge convoluted program where everything happens inside an IO monad because at some point something is written to the console. Now you realize you dont need to write to the console. These problems are essentially completely resolved these days by a modern effect system like effectful. Basically, they allow you to do arbitrary effects deep down a call stack with minimal plumbing (you still have adjust the types, as you should: that's the point of effect tracking!) and also to remove effects, so you can easily convert between pure code and "effectful code that just so happens to do no effects". https://github.com/haskell-effectful/effectful https://github.com/haskell-effectful/effectful
- 4y ago
- grugagag 4y agoThat’s happening with OOP. I remember I picked up this codebase circa 2008 and this guy who worked on it before was learning OOP. Every little thing was a class, lots of pointless inheritance everywhere and nothing made much sense at all. Luckily the project was easy to scrap and rebuild.
- ilyt 4y agoI still think it's funny that half of the OOP tutorials are "imagine that hierarchy of real things unrelated to anything programming and pretend for a second what we make is sensible" instead of giving actual real useful use cases.
- grugagag 4y agoOnce a bad idea is introduced it’s hard to stop. Tutorials are copying from eachother and more and more are ingraining those ideas.
- pull_my_finger 4y agoI think that's the idea though. It's for domain modeling. What's wrong with OOP as an abstraction over your FP foundation?
- rcme 4y agoGenerally, OOP makes it very difficult to reason about what is happening. Sub-classes can override methods in subtly different ways, internal state can be updated, super classes require changes to implement certain things in sub-classes, etc. Composition over inheritance has been much more robust in my experience.
- pull_my_finger 4y agoSounds like most of your complaints are about things that even OOP advocates recommend against these days. Multiple inheritance is rare in practice, and even rare in language support. Some newer OO languages, like Pony for example, don't support sub-classing at all and instead uses interfaces to represent that sort of information.
- ourmandave 4y agoYou’ll know you’ve lost people when normally thorough PR reviews now look like, “<thumbs up>”.
- agentultra 4y agoIt’s hard to do FP in any language that isn’t designed for it. The reason for that is on a reasonably sized team you won’t be able to get consensus on adopting an FP style. There will always be the counter-argument that FP makes the code look weird, unmaintainable, unapproachable etc. I think the only way to adopt it is to either use a language where it’s the default or get consensus from the team. Otherwise you can sneak in bits here and there in a functional style, just don’t start implementing monads, functors, and Applicative. Anything beyond map and basic higher-order functions will scare the locals.
- grugagag 4y agoStateless functions can save a lot of headaches enabling one to pay more attention to state in fewer places, create good tests and so on.
- badpun 4y agoThey're pretty hard to use consistently in languages which aren't accomodating for them though, such as Haskel or Scala. In Java, C++ etc. you can't really program without majority of your functions having side effects - or at least I haven't yet seen anyone doing it.
- adamkl 4y agoIt’s true that people don’t do it, because of language conventions, but there is no barrier to entry when it comes to writing functions (or methods in these languages) that operate solely as transformations of inputs to outputs. You then orchestrate these “pure” functions with simpler side-effectful methods that handle the external dependencies (data fetching/writing etc). There’s no pushback because of “strange” FP patterns, code looks just like normal imperative code, but the tricky logic can be isolated in pure functions. The push back is only because of the strong OOP conventions.
- dack 4y agocreating and transforming immutable data structures can be a big pain in some languages though, and that's it's own barrier. java is a good example but it's getting better with record types etc
- 12345hn6789 4y agoIt's 100% a culture thing. If you're not hiring for FP roles multiple things are going to happen. 1. Nobody who knows F paradigms is joining. Also these people most likely won't want to learn either. 2. There will be a constant battle for all PRs. "What is this, what is a filter doing, why are we mapping when we could just for loop and break" If you have even an inkling to do FP, your only option (besides building your own team) is finding a company that respects it
- combinator_3 4y agogo easy on the combinators in JS land
- taeric 4y agoI find the constant conversation around code style extremely tiring. I can't claim it isn't important. I do get the impression we think it is more important than it is. I also think we frame things wrong. We push fp as being a declarative counter to imperative code. Feels more akin to switching coordinate systems. With so many examples of "look how easy this perfect sphere is in polar coordinates" when so many of us are working with rectangles. Or, at least, not perfect spheres.
- ilyt 4y agoIt's a tool to be used in parts where it makes sense. "Writing functional" (or any other paradigm really) code should never be a target
- pklausler 4y agoThe target, of course, is writing correct code. I find recursive functions over immutable values easier to prove to be correct than iterative loops over mutable storage.
- ilyt 4y agoThe target is frankly to get paid. Sometimes you're making a prototype. Sometimes you're making one off get some info out of data and never use it again. Provably correct app that now costs 10x and is essentially yet another CRM or CRUD app is a failure because competition already delivered theirs 2 years ago and ate your market. Go too far in another direction and you're living in house of cards with tech debt killing all velocity you had and users leaving over app having bugs or just being slow, while you're drowning in rotten spaghetti. I do think that "doing the minimum to do the job at hand" is wrong approach for near everything that will live more than two or three months, but there is definitely threshold on other side of that where you're essentially just gilding the lilly and wasting time. The key is to pick your battles, time is resource that thing that every other component of your infrastructure is going to use probably could use more of that time than random feature someone up top invented on a whim.
- mLuby 4y agoThe article's code formatting is making some words invisible for me, as seen here: https://imgur.com/a/gv3FDGY https://imgur.com/a/gv3FDGY
- mb7733 4y agoSame here, I was super confused. As a workaround you can put the site into dark mode at the top right.
- robertwpearce 4y agoShould be fixed now. Sorry about that
- robertwpearce 4y agoSorry about that :(
- mLuby 4y agoAll good—figured it's better to report it so you could fix it. Thanks.
- adamkl 4y agoI’m my opinion, how to win would be to push for more logic in stateless functions, full stop. This can be done in any language without introducing “foreign” FP patterns and instantly makes code easier to understand and test.
- robertwpearce 4y agoI'm with you
- CTmystery 4y agoThanks for stating this so clearly. I am always confused why data flow frameworks, DSLs, or other syntactic sugar inevitably gets entangled with functional programming. Once more logic is in stateless functions and easy to test and compose (as you stated), one can gradually introduce layers, sugar, and abstraction (should the team choose)
- lmm 4y agoOn the contrary, that is exactly what those patterns are for, indeed it's the only legitimate justification for using them! If you can make your functions simple and stateless without using "fancy" patterns you should do so. The point of things like monads is to make it possible to do that with functions that do otherwise effectful things.
- Tainnor 4y agoIf your functions are "stateless" (by which I assume you mean they don't depend on or modify outside state), then they are pure functions so... that's exactly FP?
- adamkl 4y agoThat’s my point. FP can be as simple as that, and applied to any codebase. It’s all the extra stuff (which is good) that typically “defines” FP that scares people away, and keeps them from seeing the utility of a simple, pure function.
- yakshaving_jgt 4y agoI don’t think it’s reasonable to treat TS and Elm as equivalent.
- robertwpearce 4y agoTS and Elm were included in a "Don't have static type checking" section, alongside Flow, ReasonML, and a blank space for all the others. The point I was going for was that there are numerous options for static type checking for the web out there.
- yakshaving_jgt 4y agoSure, but I still don’t think they’re equivalent. Not all type systems are created equal, and TS has to work under a wildly different set of constraints (like escape hatches) than Elm does.
- robertwpearce 4y agoIt's hard for me to tell if your account is a joke or not because of the "yakshaving" part, so I'll respond as if it isn't. I totally agree with you! I can't say that conversation belongs alongside this post, though.
- yakshaving_jgt 4y agoI appreciate that you assumed good faith. For what it’s worth, “yak shaving” is an analogy to what programmers often find themselves spending their time doing. https://seths.blog/2005/03/dont_shave_that/ https://seths.blog/2005/03/dont_shave_that/
- tetromino_ 4y agoThe linked website's CSS is unusably broken (at least in Chrome on Android): most of the text in example code snippets is white-on-white, thus invisible; and remains white-on-white even if selected. Fortunately, there is a toggle to change theme in the upper right corner which at least makes things readable.
- robertwpearce 4y agoAuthor here: I broke the CSS last night on light mode while making a few changes. Sorry about that!
- KronisLV 4y agoOn Chrome/Edge the page seems to open as expected and the PDF is previewed inline. On Firefox, a download immediately starts. Not a big issue or anything, just a slight difference in how browsers handle the page. The HTML on the page responsible for this: <iframe class="pdf" loading="lazy" src="/pdfs/2023-01-24-how-to-lose-fp-at-work.pdf" title="How to Lose Functional Programming at Work - PDF"> </iframe> Here's approximately what I've used myself in the past as an alternative: <object data="/files/my-document.pdf#zoom=110" type="application/pdf" class="my-style-class" title="My PDF document"> <!-- Fallback for older browsers that do not support the object tag --> <embed src="/files/my-document.pdf#zoom=110" type="application/pdf"> <!-- Fallback in case PDF display of any sort is not supported --> <noembed> <span class="my-warning-class"> It seems like your browser does not support displaying PDF files. You can download the file separately <a href="/files/my-document.pdf" download>here</a>. </span> </noembed> </embed> </object> (not too clean of a solution, but seems to work okay in most cases) The presentation/article itself is pretty nice, though. I really like the mentions of the human aspect of it all, how getting others onboard is quite important!
- robertwpearce 4y agoThat's really weird, and I'm sorry that happened. I'm on Firefox Developer Edition on macOS, and it displays inline for me, but that could be my settings. I'll take a look at using an embed tonight. Thanks for the feedback!
- KronisLV 4y ago> I'm on Firefox Developer Edition on macOS, and it displays inline for me, but that could be my settings. I looked into it and it indeed is the settings! To reproduce the behavior, you'd go to General > Applications, then find PDF and if it's set to "Open in Firefox" then it will render inline, whereas if it's set to any of the other options, then it won't: - Open in Firefox: will show the PDF inline, which is probably your setting - Always ask: opening the webpage will result in a prompt for what to do with the PDF - Save file: will always download it - Use OS default application: will download it and then open it This doesn't seem to be the case with the alternative approach, which just renders it inline always (when the browser in question supports the functionality), though this is definitely a bit of interesting behavior otherwise!
- marcosdumay 4y agoThe idea of writing point-free code in Javascript seems absurd to me. On Haskell, the odds of a point-free rewriting increasing legibility are about as large of it reducing. And Haskell has all the syntax features that favors it. Javascript has none of those features.
- pklausler 4y agoMy general rule with point-free code is that it can be more readable when it's meant to convey that what's going on is a composition of components with no additional functionality.
- jim-jim-jim 4y agoHaskell level FP in TS is absurd on the whole. I respect the idea of fp-ts, and it's hardly the library's fault, but it is remarkably ugly. Type safety is only half the appeal of FP; it's also usually concise and pleasant to read. You lose that trying to hammer the concepts into a javascript shaped hole.
- baby 4y agoI didn't know what "point-free style programming" was so: > Tacit programming (point-free programming) is a programming paradigm in which a function definition does not include information regarding its arguments, using combinators and function composition [...] instead of variables. from https://stackoverflow.com/questions/944446/what-is-point-free-style-in-functional-programming https://stackoverflow.com/questions/944446/what-is-point-fre... I guess it's the fact that in programming languages like OCaml you write functions like that: let some_fn arg1 arg2 = ... and so you can partially apply them: * using `some_fn` without any argument is manipulating a function with two arguments * using `some_fn arg1` returns a new function that only takes one argument * using `some_fn arg1 arg2` applies the entire function
- omegaham 4y agoNote that in OCaml, you can't get too screwy with point-free programming because of the value restriction. It is possible to compose functions in a point-free manner, but those functions themselves have to have points if you want them to be generic. Standard example: let last xs = List.fold_left (fun x y -> Some y) None xs This is of type last : 'a list -> 'a option = <fun> Neat, `'a` is generic. Let's η-reduce out the `xs` and make the function point-free (ignoring the lambda): let last = List.fold_left (fun x y -> Some y) None This doesn't work the way that we want: last : '_weak1 list -> '_weak1 option = <fun> The moment that we call this weakly polymorphic function, its type is fixed and is no longer generic. In the toplevel: # last [1;2;3];; - : int option = Some 3 # last['x';'y';'z'];; Error: This expression has type char but an expression was expected of type int Haskell, of course, is totally happy to let you do point-free mania with Kleisli arrows and all of the rest of it.
- xwdv 4y agoThese negative logic type blogs are such an annoying style of writing and very click baity. Obviously no one would want to lose functional programming at work on purpose, so why not just write a straight “How to Win…” article instead? Why make me have the cognitive overhead of reading the opposite of what you say?
- robertwpearce 4y agoThis originated as a fun talk at the Auckland Functional Programming meetup. Given there is so much "here's how to get your team into FP" content out there already, this felt like an engaging approach and was a nice talk to share with folx. I disagree that it's annoying and click baity — else I wouldn't have went to all the effort — but if it's not for you, it's not for you.
- xwdv 4y agoObviously my original comment was meant to be read as the opposite of what I said, given the tone. Since there’s so many other comments out there heaping praise this felt like a more engaging approach. Though I guess if you’re not paying close attention you’d dismiss it as negative intent.
- robertwpearce 4y agoAh, I missed it — probably because I haven't seen any of these "heaping praise" comments yet
- psnehanshu 4y agoI enjoyed reading this post. To me it is a mix of sarcasm and roasting.
- bern4444 4y agoI like FP - I think that engineering and writing code in a way that would also let it be written in all the craziness of full FP style means you found the right abstraction - but I would never write it that way. A nice example from the article: const processData = composeP(syncWithBackend, cleansePII, validateData) async function processData(data) { await validateData(data) const cleansedData = cleansePII(data) await syncWithBackend(cleansedData) return data } // or for the Promise-chainers… const processData = data => validateData(data) .then(cleansePII) .then(syncWithBackend) .then(() => data) I would always write this as the 2nd option - it's always going to be the easiest to debug[0] and the abstractions it leverages are equivalent to the composed version in the first example or third example. When I write code that leverages FP principles (pure functions, composition, immutable data structures, etc) I know I've found the right abstraction - but there's never a need to actually abstract it all the way. That's the job of a good compiler. I'm nearly always writing code for other people to later debug, fix, extend and understand and rarely for the computer - don't make your future self's job harder than it needs to be. [0] https://sambernheim.com/blog/debug-driven-development https://sambernheim.com/blog/debug-driven-development
- kagevf 4y ago> You're probably bulking at the 3 repeated loops “balking”?
- dhosek 4y agoIs it just me, or does the bad advice format of the article make it hard to see tell the good advice is? In my early days teaching LaTeX classes, I used to have an example of incorrect input in the first day’s material (forgetting to leave a space after a command). Even though it said before the example that it was incorrect, invariably, at least one person in each class would type it into their computer and have the error returned to them. I removed the example because it only hindered the teaching process. I feel like articles like this which use opposite-speak in making their point do the same thing.
- kagevf 4y agoI agree, it’s confusing. Plus, it looked like negative and positive advice was mixed, when referencing a talk.
- sweetjuly 4y agoIt's a bit more analog, but one piece of advice I got in a teaching seminar was to only ever write true things on the board. This is important because students often zone out at just the wrong time and when they come back and realize they missed a bit of class, they'll glance at the board and furiously start copying down whatever you wrote without knowing the context. If it turns out what you wrote wasn't true, well now a whole bunch of students copied down the wrong thing and are going to be extremely confused why the example they copied verbatim from class isn't working.
- robertwpearce 4y agoThat's an interesting point. When I taught, I took this same approach — thanks for reminding me of it.
- toastal 4y agoIt’s always a terrible idea to go against the ergonomics and idioms in a language. Shoehorning JavaScript into something purely function is just as bad of an idea as trying to do object-orientation in Haskell. If you believe in certain patterns, pick the right tool—in this case a compile-to-JS language—for the job. Either make the case for rewriting certain modules, or find a job/team that supports your goals and principles.
- eyelidlessness 4y agoI don’t think I’ve ever thought a blog post—not its author, just the post itself—is a jerk until now. I read most of it but bailed because it felt like being bullied for no apparent reason.
- robertwpearce 4y agoWow, I'm horrified that the post was received this way. How did it make you feel bullied? Was this because of it using imperatives with "do" and "don't"? edit: the real-talk takeaways at the end are worth reading, I think (I'm biased, though)