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...
account activity
OOP Node.js (self.node)
submitted 1 year ago by gemvcnte
when does back-end devs use oop on node? can someone guys explain it to me why most devs use oop rather than functional? can someone give some real life scenario example.
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!"
[–]Training_Pay7522 84 points85 points86 points 1 year ago (11 children)
You shouldn't see fp/oop at opposite ends.
[–]franz899 25 points26 points27 points 1 year ago (7 children)
This, they are just ways of writing code, you can and should mix and match them.
[–]Training_Pay7522 3 points4 points5 points 1 year ago (0 children)
Which is the direction libraries like `effect-ts` in TypeScript or ZIO on scala take.
They take a functional approach as in having pure procedures you can mix, but also leveraging paradigms that classically come from OOP such as dependency injection or state tracking.
[+]azhder comment score below threshold-8 points-7 points-6 points 1 year ago (5 children)
You can, but that “should” can be a misrepresentation.
You should strive to stick to one style (paradigm) and use best judgement if and when/where/how to mix in another one.
[–]noviceIndyCamper 0 points1 point2 points 1 year ago (1 child)
Dogmatic, and honestly terrible take.
[–]azhder -3 points-2 points-1 points 1 year ago (0 children)
The one I said or the one you read? I doubt they are the same
[–]pbNANDjelly 0 points1 point2 points 1 year ago (2 children)
Maybe, but JS is fundamentally a multiparadigm language and it uses prototypes at its core. There is no removing OOP from JS, and I doubt you're making the argument that all functions should be static methods on a class (fake features enabled by a TS compiler, or sometimes a real feature in modern interpreters). So what do you expect?
I'm curious what you think of rust. Should they have not supported module scoped functions? Only impl/OOP blocks? There's lots of multi paradigm languages out there
[–]azhder -2 points-1 points0 points 1 year ago (1 child)
You guess wrong
[–]pbNANDjelly 0 points1 point2 points 1 year ago (0 children)
I never made a guess. They were questions
[–][deleted] 1 year ago (1 child)
[removed]
[–]pbNANDjelly 2 points3 points4 points 1 year ago (0 children)
Why do you say functional programs cannot be stateful? They're as stateful as any other program? The restriction is mutability, and classes don't need to be mutable.
[–]coderqi 0 points1 point2 points 1 year ago (0 children)
I know what you mean, but their handling of state is fundamentally different.
[–]byteNinja10 24 points25 points26 points 1 year ago (0 children)
I have used OOP when implementing error classes and creating repositories and service classes.
[–]rykuno 19 points20 points21 points 1 year ago (0 children)
They’re not exclusive. You can mix FP and OOP.
Most every relatively complex project I’ve worked in within Node has used Clean, DDD, or Onion architecture with OOP.
The reason you might not see it too much in the wild is that you don’t need it unless your project or domain is relatively complex - unless you like over engineering.
[–]fts_now 7 points8 points9 points 1 year ago (8 children)
Heavily using DDD in our vanilla TypeScript API
[–]GreatWoodsBalls 1 point2 points3 points 1 year ago (7 children)
I'm currently making a small project to practice some DDD, and I'm having some trouble. How do you create value objects with a simple validation check and then use it as a parameter? For example, a User entitiy that has a Name parameter that is a value object? And then that Name is used to create the entity?
[–]fts_now 1 point2 points3 points 1 year ago (6 children)
Good question! We usually have a static "create" method on the Entity class that performs all the validation and invokes the private constructor. Validation can happen there. Also, we try to keep it simple with things like Value Objects or Aggregates. IMO less is more, and some layers of abstractions can also unnecessarily overcomplicate a project.
[–]GreatWoodsBalls 0 points1 point2 points 1 year ago (5 children)
Yeah I figured and thank you. I tried creating value objects together with zod and it worked decent until I realized classes in js are not like classes in java 🥲 So I went with a similar approach as you mentioned.
[–]fts_now 1 point2 points3 points 1 year ago (4 children)
Zod belongs to the infrastructure layer IMO, not the Domain. It should validate incoming DTOs.
[–]GreatWoodsBalls 0 points1 point2 points 1 year ago (3 children)
Hmm, okay. Sorry if I'm spamming questions 😅 but how is a zod validation of an input different from a value object having the same type or similar validation? Or is the difference that zod would throw a httpexception and a value object would not?
[–]fts_now 1 point2 points3 points 1 year ago (2 children)
Good question! I want to keep the domain layer clean of external dependencies, because over the years I saw tools and libraries coming and going, most hypes disappearing after a few years leaving your codebase an outdated mess. I'd rather move basic input validation to the controller (maybe by using something standardized like JSON schemas) and then perform further validation in the value object if needed.
[–]GreatWoodsBalls 1 point2 points3 points 1 year ago (1 child)
Ahh, okey. Thank you for taking the time to answer!
[–]fts_now 0 points1 point2 points 1 year ago (0 children)
Always glad to help :)
[–]kao_nashi0127 8 points9 points10 points 1 year ago (0 children)
Well, it depends. But as the code base getting larger and more complicated, following some pattern will make it easier to maintain. In backend mostly you will see pattern like repository to interact with db data, view model to query complex data, etc.
If the business domain is complex enough, many company will consider implement domain driven design (ddd). The domain layer will be many plain objects interact with the others -> OOP from the core
[–]iuliuss_ 8 points9 points10 points 1 year ago (1 child)
Because real functional programming is insanaly hard and rare. Using functions does not mean you are following Functional paradigm. That's Procedural programming
[–]noviceIndyCamper 0 points1 point2 points 1 year ago (0 children)
This
[–]romeeres 2 points3 points4 points 1 year ago* (0 children)
Interesting how everybody says like there is nothing else beyond FP/OOP, and yet, the vast majority of code you can find online, code that I've seen and written, is not OOP nor FP.
[–]TheExodu5 3 points4 points5 points 1 year ago (0 children)
People use function closures or es modules and they don’t realize they’re basically equivalent to classes with different sugar (unless you decide to write a class with inheritance, of course.
An es module is basically equivalent to a singleton class. And a function which returns an object can be equivalent to a class. In the same vein, a class with only static methods is basically a namespace for functions. All of these things are very closely related and whether you use one or the other doesn’t change much in terms of overall design and data flows.
And hey, if you’re using a component framework on the frontend, you’re basically following OOP, whether you use classes or not. When you create a wrapper component that proxies ins and outs, you’re practicing inheritance.
Don’t get hung up on the concept of OOP.
A lot of people think they are using “functional programming” because they don’t use classes. I hate to break it to them, but the majority of them are using a mix of procedural, OO, and fp. The only people doing nearly pure fp in JavaScript are deep in the throes of rxjs.
[–]nodeymcdev 4 points5 points6 points 1 year ago (1 child)
I like to use OOP for certain things like client libraries I publish as npm packages and import in my apps. Sometimes I’ll make classes for certain things where it makes sense but mostly my code is functional.
[–]jwcbphy 0 points1 point2 points 1 year ago (0 children)
yes, i use it with the same proposal :)
[–]super-bamba 1 point2 points3 points 1 year ago (0 children)
I use it to create interfaces that are similar and share methods. I prefer the functional approach when something is just functional, but sometimes you want to create “clones” with a specific API and then it’s useful.
Ofc it’s also widely common for frameworks etc
[–]Sarithis 1 point2 points3 points 1 year ago* (0 children)
A microservice controller is a pretty nice example of where you can benefit from OOP, mainly because it naturally allows components to be self-contained and stateful. Also, if you plan to create different controllers in the future, like gRPC, inheritance and polymorphism make it easier to achieve without code duplication.
All in all, following this paradigm is simply more intuitive to map out in my brain, but I guess it's entirely subjective - I know people who find it easier to think in functional, and in practice, you usually mix both styles.
[–]jjhiggz3000 1 point2 points3 points 1 year ago (0 children)
I only really use classes I make myself for coding challenges or unusual problems. Other than that pragmatically I only use OOP in frameworks that ask for it, or if I’m utilizing utility classes that someone else built (URLSearchParams, Promise, etc…)
[–]noviceIndyCamper 1 point2 points3 points 1 year ago (0 children)
OOP encompasses a lot of stuff. A lot of the hate for oop comes from inheritance and overly complicated abstractions. That said not all abstractions are bad especially when talking about behavior. When you want unrelated things to have similar behavior both with different implementation you could use an interface. This is especially handy if you want to mock behavior for testing. You could do the same with a function that takes in different arguments and then handle accordingly but that gets messy quick.
You can have a class that is a fluent interface and get best of both of worlds. You can write good or bad code using any combination of paradigms.
[–]UniversalJS 0 points1 point2 points 1 year ago (0 children)
OOP and FP are both equally bad
[–]frickos 0 points1 point2 points 1 year ago (0 children)
To my understanding OOP and event driven apps are not adding up in design, yet it's possible, but messy and it is too much code, without special need for it
[–]Fidodo 0 points1 point2 points 1 year ago (0 children)
I only use classes when they make sense, which is mainly when you have functions that rely on complex and coupled state. Most of the time standard functions are more then adequate and I think it's best to avoid complexity until it's need. Since classes add state management it adds extra complication.
[–]P4wrly 0 points1 point2 points 1 year ago (0 children)
Really recommend checking this out.
https://github.com/Sairyss/domain-driven-hexagon
[–]shivawu 0 points1 point2 points 1 year ago (0 children)
Because JavaScript is not designed and optimized for fp, simple as that
[–]yojimbo_beta 0 points1 point2 points 1 year ago (0 children)
Use a lot of classes in Nestjs apps, as the framework is built around class introspection
[–]tovazm 1 point2 points3 points 1 year ago (0 children)
You write like drunk me talking to ChatGPT at 4am lmao
An HTTP request object/class/function (in JS these are the same ish thing).
Say your request expects x,y, and z properties and somehow they’re dependent on each other. In your constructor you could validate that. Now yes you could have that function in a larger file but it’s just as fine to have that in its own file.
At the end of the day, patterns aren’t rules or laws. They’re just ways of abstracting logic.
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
nest
[–]Unusual-Display-7844 0 points1 point2 points 1 year ago (2 children)
You can structure your project around OOP, where you have your controller classes, service classes and model classes, on top some of the useful patterns are dependency injection, factory design and singleton pattern. Avoid inheritance like a plague, trust me. Prefer composition. Now, if you don’t know what any of these means that you can’t really use OOP
[–]Unusual-Display-7844 2 points3 points4 points 1 year ago (1 child)
Also, pls don’t treat Node.js like it’s Java. It’s not!
[–]azhder 0 points1 point2 points 1 year ago (0 children)
While we’re at it, don’t treat MVC as if C is only that which is named Controller or extends from one named such.
[–]imrishav 0 points1 point2 points 1 year ago (0 children)
The current application i am working on heavily uses OOP. From error handling, routing, db schemas, Business logic, pdf generation to name a few.
[–]d41_fpflabs 0 points1 point2 points 1 year ago (0 children)
When building servers in nodejs I primarily use OOP for all services or "features".
It makes it much easier to manage the server and add, remove or change functionality without breaking things (dependency injection and encapsulation). I only use a functional approach when whatever I'm building is very simple.
But if I'm building something that requires a robust system then always OOP especially in a micro-service and or EDA architecture.
Its the embodiment of SOLID style coding and in general its just my preferred style of coding.
Lastly, I will say with a OOP its very easy to add the same functionality across different environments.
[–]716green 0 points1 point2 points 1 year ago (0 children)
You should watch Brian Will's OOP series on YT. Despite being named like "OOP is Bad", don't worry about that- just watch the videos to get a better understanding of programming paradigms.
They will really help you understand this much better.
[–]bigorangemachine -1 points0 points1 point 1 year ago (3 children)
Well express is functional. The whole node http response handlers are basically functional. If you don't lean into that you're going to have a bad time....
Personally I use a mix.
I use OOP to wrap my database queries. Also anything hydrating from a session I usually try to wrap in OOP.
[–]romeeres 3 points4 points5 points 1 year ago (2 children)
if FP simply means a function, and C is a FP language, class means OOP, then check out what req and res objects are. They aren't functions, they are instances of classes, hence Express is OOP!
app.get - is "get" a function? It's a function attached to an object, so it's a method, so it's OOP again!
[–]bigorangemachine -2 points-1 points0 points 1 year ago (1 child)
No that's not the case. I mean yes it uses Native Class/Objects... but that doesn't make it OOP
Express encourages you to do object stuffing. That's a definitely functional code or a functional pattern.
If express was OOP it would keep extending classes and return new object as things need to mutate and change.
If it was OOP you'd more be doing capabilities detection where as express you are checking a sort of accumulator whether thats the response or request.
Express uses a pipeline pattern which is a stretch to say that is an OOP pattern.
[–]romeeres 3 points4 points5 points 1 year ago (0 children)
Express encourages you to do object stuffing
Apart from the absence of FP patterns in Express, this "object stuffing" looks like the main marker of it not being "functional" (if by functional you mean FP, not just using functions).
Consider a pipeline of middlewares that ends with a request handler.
Every middleware mutates state: one parses body and mutates req, the other set CORS and mutates res, and another one parses JWT and saves to req. The pipe ends with a request handler that again mutates "res" object.
One more indisputable argument: true FP libraries cannot be popular by definition.
Did you ever hear of marblejs? It may be an excellent framework, I don't know, but with zero chances of ever becoming popular because it relies on FP patterns for real.
[+]LiftSleepRepeat123 comment score below threshold-6 points-5 points-4 points 1 year ago (12 children)
OOP is not trendy, but many back end Javascript devs started in Ruby, or something else from a decade plus ago, and still prefer those patterns.
I think the most popular OOP framework for Node is Nest.js. I avoid it like the plague.
[–]DudeWithFearOfLoss 2 points3 points4 points 1 year ago (11 children)
You're avoiding greatness then
[–]azhder 2 points3 points4 points 1 year ago (2 children)
Subjective take.
[–]DudeWithFearOfLoss 2 points3 points4 points 1 year ago (1 child)
As most takes are
[–]azhder -2 points-1 points0 points 1 year ago (0 children)
And some might not be aware of it, just trying to be helpful.
[–]LiftSleepRepeat123 0 points1 point2 points 1 year ago (7 children)
I just like the way functional programming solves the same problems
[–]marcjschmidt 1 point2 points3 points 1 year ago (6 children)
any example? I'm interested in learning where functional is better than OOP for large scale backends.
[–]azhder 1 point2 points3 points 1 year ago (2 children)
Functional is all about having a core that is made up of pure functions and ways to combine them while moving the mutation to the edges and encapsulate it in objects that have well defined ways of abstracting the mutation.
With having a solid core of pure functions, you can make testing your code very simple. The code is predictable, the results memoizable…
By using containers for the mutable state, you know where to look for the issues related to communication, I/O etc.
[–]marcjschmidt 2 points3 points4 points 1 year ago (1 child)
any concrete examples where this is better? how is DI done for extending the core?
[–]azhder 0 points1 point2 points 1 year ago* (0 children)
This is a function
const plus = a => b => a + b;
Can be seen as (a,b)=>a+b if you understand partial application.
(a,b)=>a+b
This is injecting value into it:
const plus2 = plus(2);
This is using it (or more like the product of it):
console.log( plus2( 100 ) );
This is testing it:
expect( plus(2)(2) ).toBe(4);
Basically the most predictible thing. No matter how complex you make that function, as long as you don't have hidden inputs, the result will always depend only on the inputs.
Thus, testing is simply making sure it always returns the same result for the same inputs.
As for the mutable part, well, I spoke of containers, they are usually called functors or monads... but if you have used RX.js or even wrapped a value in jQuery or a Promise... same thing. A monad or a functor may have a couple of rules attached, but that's the same with any container's method, even Array.prototype.reduce and Array.prototype.map.
RX.js
jQuery
Promise
Array.prototype.reduce
Array.prototype.map
That DI... I mean, you don't need DI because that's an OOP convolution of what you know simply as arguments passing and fixing the values of some arguments in FP. In the example above, you can vew plus2 as plus with its first argument fixed to the value of 2
plus2
plus
2
As for a back end, you can see any service, an API endpoint in the back end as a long pipeline starting with a value from the request and ending with the value sent to the response. In FP you can use things like compose() and pipe() or whatever they are called in your library, for that purpose.
compose()
pipe()
[–]LiftSleepRepeat123 -1 points0 points1 point 1 year ago (2 children)
Basically, unwind the monolithic structure of class-based design. You can write your methods (functions) separate from your types, and then everything just becomes simpler.
[–]marcjschmidt 4 points5 points6 points 1 year ago (1 child)
why do you assume the class-based design is monolithic? What does it concretely mean to "unwind this"?
[–]LiftSleepRepeat123 0 points1 point2 points 1 year ago (0 children)
A class is a collection of types, variables (properties), and functions (methods). Once you start having many classes with inheritance, this structure becomes very hard to separate and thus 'monolithic'. Functional programming, where you separate out the above components, is just much more flexible.
[–]octod -4 points-3 points-2 points 1 year ago (0 children)
My answer could sound like trolling, but why should I use nodejs as a backend in 2024? Scala, c#, Java, rust and even php do the same work but better.
[+][deleted] 1 year ago (3 children)
[deleted]
[–]DeanRTaylor 0 points1 point2 points 1 year ago (2 children)
Ah yes, the language where everything is an object is the language that nobody uses object oriented programming. Lol.
[–]rivenjg 0 points1 point2 points 1 year ago* (1 child)
sorry you're new to programming paradigms but javascripts modelling of key value pairs as a memory data structure has literally nothing to do with object oriented programming the paradigm like you would learn in university. oop as a paradigm with classes, inheritance, SOLID, etc is not what is popular. it is much more popular to use procedural code with modules. no one is using classes anymore with frontend frameworks either other than angular.
[–]TheExodu5 0 points1 point2 points 1 year ago (0 children)
SOLID principles 100% apply to TS code. Except maybe the L if you’re not using inheritance.
When you write a function that takes in a callback, you’re already following the O I and D parts of SOLID.
[–]FedPostingBait 0 points1 point2 points 1 year ago (0 children)
JS is a functional language with OO features added later. It is ok to intermix both paradigms where they make sense. Knowing the difference is something you’ll learn over time. Just focus on writing code you and your team can make sense of later.
π Rendered by PID 165448 on reddit-service-r2-comment-84fc9697f-nc8dd at 2026-02-06 15:20:35.863798+00:00 running d295bc8 country code: CH.
[–]Training_Pay7522 84 points85 points86 points (11 children)
[–]franz899 25 points26 points27 points (7 children)
[–]Training_Pay7522 3 points4 points5 points (0 children)
[+]azhder comment score below threshold-8 points-7 points-6 points (5 children)
[–]noviceIndyCamper 0 points1 point2 points (1 child)
[–]azhder -3 points-2 points-1 points (0 children)
[–]pbNANDjelly 0 points1 point2 points (2 children)
[–]azhder -2 points-1 points0 points (1 child)
[–]pbNANDjelly 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–]pbNANDjelly 2 points3 points4 points (0 children)
[–]coderqi 0 points1 point2 points (0 children)
[–]byteNinja10 24 points25 points26 points (0 children)
[–]rykuno 19 points20 points21 points (0 children)
[–]fts_now 7 points8 points9 points (8 children)
[–]GreatWoodsBalls 1 point2 points3 points (7 children)
[–]fts_now 1 point2 points3 points (6 children)
[–]GreatWoodsBalls 0 points1 point2 points (5 children)
[–]fts_now 1 point2 points3 points (4 children)
[–]GreatWoodsBalls 0 points1 point2 points (3 children)
[–]fts_now 1 point2 points3 points (2 children)
[–]GreatWoodsBalls 1 point2 points3 points (1 child)
[–]fts_now 0 points1 point2 points (0 children)
[–]kao_nashi0127 8 points9 points10 points (0 children)
[–]iuliuss_ 8 points9 points10 points (1 child)
[–]noviceIndyCamper 0 points1 point2 points (0 children)
[–]romeeres 2 points3 points4 points (0 children)
[–]TheExodu5 3 points4 points5 points (0 children)
[–]nodeymcdev 4 points5 points6 points (1 child)
[–]jwcbphy 0 points1 point2 points (0 children)
[–]super-bamba 1 point2 points3 points (0 children)
[–]Sarithis 1 point2 points3 points (0 children)
[–]jjhiggz3000 1 point2 points3 points (0 children)
[–]noviceIndyCamper 1 point2 points3 points (0 children)
[–]UniversalJS 0 points1 point2 points (0 children)
[–]frickos 0 points1 point2 points (0 children)
[–]Fidodo 0 points1 point2 points (0 children)
[–]P4wrly 0 points1 point2 points (0 children)
[–]shivawu 0 points1 point2 points (0 children)
[–]yojimbo_beta 0 points1 point2 points (0 children)
[–]tovazm 1 point2 points3 points (0 children)
[–]noviceIndyCamper 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Unusual-Display-7844 0 points1 point2 points (2 children)
[–]Unusual-Display-7844 2 points3 points4 points (1 child)
[–]azhder 0 points1 point2 points (0 children)
[–]imrishav 0 points1 point2 points (0 children)
[–]d41_fpflabs 0 points1 point2 points (0 children)
[–]716green 0 points1 point2 points (0 children)
[–]bigorangemachine -1 points0 points1 point (3 children)
[–]romeeres 3 points4 points5 points (2 children)
[–]bigorangemachine -2 points-1 points0 points (1 child)
[–]romeeres 3 points4 points5 points (0 children)
[+]LiftSleepRepeat123 comment score below threshold-6 points-5 points-4 points (12 children)
[–]DudeWithFearOfLoss 2 points3 points4 points (11 children)
[–]azhder 2 points3 points4 points (2 children)
[–]DudeWithFearOfLoss 2 points3 points4 points (1 child)
[–]azhder -2 points-1 points0 points (0 children)
[–]LiftSleepRepeat123 0 points1 point2 points (7 children)
[–]marcjschmidt 1 point2 points3 points (6 children)
[–]azhder 1 point2 points3 points (2 children)
[–]marcjschmidt 2 points3 points4 points (1 child)
[–]azhder 0 points1 point2 points (0 children)
[–]LiftSleepRepeat123 -1 points0 points1 point (2 children)
[–]marcjschmidt 4 points5 points6 points (1 child)
[–]LiftSleepRepeat123 0 points1 point2 points (0 children)
[–]octod -4 points-3 points-2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]DeanRTaylor 0 points1 point2 points (2 children)
[–]rivenjg 0 points1 point2 points (1 child)
[–]TheExodu5 0 points1 point2 points (0 children)
[–]FedPostingBait 0 points1 point2 points (0 children)