DEV Community

Sudhir.js
Sudhir.js

Posted on

OOP or Functional or Both

When to use which type of programming Paradigm, OOP or Functional Based or Mixture of Both for Front End Javascript

Top comments (10)

Collapse
 
ben profile image
Ben Halpern

I think both is the most practical scenario most of the time. With a flexible programming language use the paradigm that best fits the situation.

In some frontend scenarios it probably does make sense to go hardcore functional though. It will likely be a slower, more complicated building process, but could result in really consistent code.

Collapse
 
thexdev profile image
M. Akbar Nugroho

Love this. Yesterday i wrote my React component with FP implementation and my custom library with OOP implementation to manage local storage and session storage in my web.

Collapse
 
johnkazer profile image
John Kazer • Edited

Both! For me the control logic is functional whilst representing physical graphical components is OO. I also try to use state machines (Xstate) as much as possible to represent behaviour and sequences of actions. Not sure if state machines are one or the other...

Collapse
 
pj profile image
Paul Johnson • Edited

I think in practice a lot depends on who are you working with now or who you might be working with in the future.

Just for yourself? Go nuts using functional concepts if that's what you want.
With a team that's got a lot of experience and knowledge of FP concepts? FP might be a good choice.
With a team of mixed skills, possibly where you don't know who will be hired in the future? Might be worth being more conservative in how you code, OOP, imperative with a sprinkling of functional concepts e.g. map/filter.

Collapse
 
adrianmarkperea profile image
Adrian Perea

Both! When it comes to data manipulation, FP is king. But when you need to encapsulate data and functionality, OOP wins.

E.g.

Say I'm calling an API which returns a list of non-trivial objects. I would wrap the objects with my own class (OOP) so my driver code wouldn't have to touch the data directly. If I'm manipulating the list as a whole, I would use functional array methods (FP).

Collapse
 
alchermd profile image
John Alcher

I'm gonna say 65% OOP and 35% FP. And that estimate is with various languages that I have used in production (JS, PHP, and Python). Going 100% either way is not a very good idea, IMO.

Collapse
 
sudhawan2 profile image
Sudhir.js

For DOM manipulation, OOP or FP

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

JavaScript is object functional, use both and move on with life πŸ˜…

Collapse
 
manishfoodtechs profile image
manish srivastava

Both. But you can switch if it gets more complicated in some modules.

Collapse
 
mikeyglitz profile image
mikeyGlitz

Functional or OO largely depends on what you're trying to do with Javascript or what frameworks you're using.
If argue that it's easier to do functional in React than other frontend libraries.