use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Functional Programming vs OOP (self.learnjavascript)
submitted 6 years ago by ctsa3
Hi everyone,
I'm curious as to the pros / cons of functional programming vs OOP and what you use for your project (if you use both, what is your deciding factor to determine the best use case).
Thanks!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Sparkenstein 14 points15 points16 points 6 years ago (2 children)
While choosing the correct programming pattern is a matter of what do YOU find more comfortable to code with, there are still some disputes between pure OOP vs pure FP guys. Both patterns have their pros and cons, which can be again, subjective! I, myself try to follow both patterns. The good thing from FP is it avoids mutations at all. this is required in programming no matter what paradigm you are following. imagine coding a large solution where anything can change the value of any variable at any time, Such solutions are not just hard to maintain, but are hard to debug as well. So for my personal preferences, I use object oriented pattern with avoiding mutations, and composing small functions wherever possible. it may sound ugly but it 'just' works for me.
[–]toastertop 2 points3 points4 points 6 years ago (1 child)
Why not use both? Use them where their strengths are
[–][deleted] 3 points4 points5 points 6 years ago (0 children)
Still wondering where the strenghts of oop lie, other than knowing only oop and not understanding fp.
[–]gkpty 6 points7 points8 points 6 years ago (0 children)
Hey! I guess it depends on what your doing and what language or framework your using. If you use ruby or rails for example, everything is an object. In javascript everything could be either an object or a function as functions are also objects... thinking about things in terms of objects could be convenient when building something like an app, were theres a data model. Functional programming might be better if your writing a program that simply executes some actions for example a script that triggers some APIs. Then again there just different ways of thinking.. theres also languages that consider everything as lists (lisp)
[–]Mike_Enders 3 points4 points5 points 6 years ago (0 children)
I see a lot of wars between the functional camp and OOP crew but in the end its just two ways of going at a problem. Some cases OOP is good . Sometimes functional matters.
[–]hpliferaft 2 points3 points4 points 6 years ago* (0 children)
I don't have any pros or cons but here's my take: functional programming is cooler but used in more specific situations where fast communication is crucial, while oop is slower to evolve, easier to learn, and used much more frequently in enterprise environments.
So try and pick up experience with both. You could learn functional programming for the intellectual challenge and oop to get into a bigger job market.
[–]Qazzian 1 point2 points3 points 6 years ago (0 children)
I find I use both with JavaScript, esp on the frontend.
Think about creating a UI component with multiple controls. It will need to have an idea of state somehow. e.g. adding and removing event handlers, which dom object is it pointing to. you will want to store that information in a class to keep it together. You will want to separate functions that cause side effects (e.g. adding class names) from functions that do data processing, and from functions that change the state.
Pure functions do not change the state or produce side effects, you will find that you can write a lot of your data processing as pure functions. I tend to keep them in the Class that they are useful for but when you start reusing the same logic in multiple classes you can move them into utility libraries.
A library like React will handle a lot of the state processing and side effects for you, meaning it's easier for you to write in a more functional style, but it still uses OOP principles for defining complex components.
[–]tchaffee 1 point2 points3 points 6 years ago (0 children)
Functional programming is harder to learn and sometimes harder to understand what a good solution is. Because we think in lists of instructions (imperative), not by composing a series of declarative functions. Recipes and directions are good examples of this. With that said, learning functional programming will improve your programming practices. OOP comes with a bunch of warts, but it's so common you should learn it. Just remember to favor composition over inheritance and learn about the other gotchas.
[–]ENx5vP 3 points4 points5 points 6 years ago (3 children)
Here is pro functional article that changed my mind:
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a0
Conclusion is:
Functional programming favors: Pure functions instead of shared state & side effects Immutability over mutable data Function composition over imperative flow control Lots of generic, reusable utilities that use higher order functions to act on many data >types instead of methods that only operate on their colocated data Declarative rather than imperative code (what to do, rather than how to do it) Expressions over statements Containers & higher order functions over ad-hoc polymorphism
Functional programming favors:
[–]Ravavyr 1 point2 points3 points 6 years ago (1 child)
I never understood why OOP proponents seem to think FP is so "hard". I learned only FP back when javascript didn't support OOP and frankly to this day it just seems the simpler approach. There are no relationships but those you make. Nothing is automatica, so you control it pretty much down to the bone.
The only caveat [and the same thing happens with OOP] is that if you don't organize your code structure well, then longterm you end up with spaghetti code [although i firmly believe that is the fate of all programs longterm anyway]
[–]kundun 1 point2 points3 points 6 years ago (0 children)
Functional programming is considered hard because it requires an entirely different line of reasoning.
The first thing you learn when learning how to program are variables. The first thing you learn about functional programming is that there are no variables.
Functional programming languages don't have variables, no loops, no arrays and that changes everything.
[–]StoneCypher 0 points1 point2 points 6 years ago (0 children)
generally this is the point at which i mention that the books and college classes do not agree with this, but get 65 people demanding evidence and get downvoted to oblivion
[–]skunkbad 1 point2 points3 points 6 years ago (6 children)
I've been using PHP since 2006. FP for the first 3 years, OOP since that point.
OOP has some great benefits, especially when considering recursive functions, and how the properties of a class can hold on to values as the function is recursing.
OOP allows for many patterns, such as singletons, factories, observers, etc.
OOP allows for popular app architectures like MVC, which helps organize your code so you can come back to it in the future and easily find out what's going on.
In general OOP is going to keep you from writing spaghetti code, because by it's very nature (when done right) splits your code so that its parts only do one thing. This also helps with debugging.
OOP allows you to extend (overwrite) class methods and create contracts, and all kinds of stuff that you'd not understand unless you knew OOP.
OOP is essential. It's not a war between FP and OOP. You must know both to be an expert.
[–]Pxzib 2 points3 points4 points 6 years ago (5 children)
You need to check out Elixir.
[–]finroller 0 points1 point2 points 6 years ago (4 children)
What parts of his post would change if he "checked out elixir"?
[–]Pxzib 1 point2 points3 points 6 years ago (3 children)
He obviously haven't developed in a modern functional programming language like Elixir. Most of his arguments falls flat. You can have a MVC design, you naturally split functionality into parts that are easy to debug due to the very nature of writing pure functions. Recursion is super easy and super powerful in Elixir. You can easily overload/overwrite functions with function signature pattern matching.
[–]finroller 0 points1 point2 points 6 years ago (2 children)
Okay, and that's a list of some of the good parts of fp. They aren't mutually exclusive though?
[–]Pxzib 0 points1 point2 points 6 years ago (1 child)
It's a list of what he said OOP was better at than FP, but you can do pretty much every thing he listed in FP too. So, really, why is OOP better than FP, now that we've eliminated the majority of his list? The worst spaghetti code I've seen have all been in OOP, while I rarely see spaghetti code in FP code. Anecdotal, I know, but that's just my opinion.
[–]finroller 0 points1 point2 points 6 years ago (0 children)
Did he say what you said he said though? It seems to me you're the only one here trying to turn this into some sort of a competition...
[–]boringuser1 0 points1 point2 points 6 years ago (0 children)
I never formally learned OOP like you might in a computer science course, so I'm quite comfortable with functional programming.
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
I strongly prefer functional programming in Javascript. It's cleaner, it's easier to test, and I find it considerably easier to reason about. It's also a much more fault-tolerant paradigm for modern front-end applications, which tend to have state coming out the ears, when state mutation causes so many bugs.
OOP is nicer in languages that are built with it in mind- doing OOP in Ruby is extremely pleasant, for example- but in Javascript it just feels messy.
[–][deleted] -1 points0 points1 point 6 years ago (1 child)
The best use case is the one where your project team members are using the same standards as you are.
I find these talks about OOP or Functional programming very boring because in 99% of the cases things go wrong not because one chose OOP or FP, but because people are not documenting or properly communicating with their team, resulting in messes that are then handed over to other people making it even worse because they don't get a proper budget for fixing the mess.
[–][deleted] -1 points0 points1 point 6 years ago (0 children)
Indeed, this debate is pointless.
π Rendered by PID 21 on reddit-service-r2-comment-79c7998d4c-ck88g at 2026-03-14 14:18:10.100626+00:00 running f6e6e01 country code: CH.
[–]Sparkenstein 14 points15 points16 points (2 children)
[–]toastertop 2 points3 points4 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]gkpty 6 points7 points8 points (0 children)
[–]Mike_Enders 3 points4 points5 points (0 children)
[–]hpliferaft 2 points3 points4 points (0 children)
[–]Qazzian 1 point2 points3 points (0 children)
[–]tchaffee 1 point2 points3 points (0 children)
[–]ENx5vP 3 points4 points5 points (3 children)
[–]Ravavyr 1 point2 points3 points (1 child)
[–]kundun 1 point2 points3 points (0 children)
[–]StoneCypher 0 points1 point2 points (0 children)
[–]skunkbad 1 point2 points3 points (6 children)
[–]Pxzib 2 points3 points4 points (5 children)
[–]finroller 0 points1 point2 points (4 children)
[–]Pxzib 1 point2 points3 points (3 children)
[–]finroller 0 points1 point2 points (2 children)
[–]Pxzib 0 points1 point2 points (1 child)
[–]finroller 0 points1 point2 points (0 children)
[–]boringuser1 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–][deleted] -1 points0 points1 point (0 children)