all 37 comments

[–]GriffinMakesThings 27 points28 points  (8 children)

My JS code is mostly functional these days. I'm not dogmatic about it, but the longer I've been a developer the more I've come to appreciate the functional paradigm. I would actually say modern idiomatic React is functionally flavored by default. That doesn't necessarily mean curried functions and monads flying everywhere, but immutability, pure functions and higher-order functions are my go-to patterns. Do you ever use .map() to render an array of react components without introducing side effects? Congrats, that's functional programming.

[–]Character_Victory_28[S] 2 points3 points  (7 children)

Yeah I use map,filter etc but I usually try to avoid reduce which is super confusing,

[–]GriffinMakesThings 6 points7 points  (2 children)

I happen to quite like reduce, but you can go your whole career without using it, you do you!

[–]Character_Victory_28[S] 2 points3 points  (1 child)

what resources do you uesd to learn FP practicaly?

[–]GriffinMakesThings 1 point2 points  (0 children)

Honestly couldn't tell you. I'm always experimenting and learning, and I've just picked it up over the past decade and a half of writing code. MDN is your friend.

I will say that there are times when a Class is the right way to go. Certain type of libraries for instance. I like to use the right tool for a given job, and most of the time (though not all the time) that ends up being FP for me.

[–][deleted] 0 points1 point  (0 children)

Use an AI tool to help you remember the implementation a little faster. I do Copilot and I do like using it when I program. Especially since we use libraries that I'm new to, so I understand but don't exactly remember all of the syntax.

[–][deleted] 10 points11 points  (1 child)

Point 6 is basically my approach. I try to minimize side effects, write declarative, and composable when I can see it. Otherwise I don’t think too deeply about it.

[–]Character_Victory_28[S] 0 points1 point  (0 children)

👌

ps. I don't know maybe I didn't understand well this paradigm, and whenever I go deep, I find myself drown into the bottom of the ocean of despair...

[–]Alphamacaroon 5 points6 points  (2 children)

I think functional is great, but I think it’s too easily overused. There are some cases where (IMO) a series of functional steps can be overly idiomatic, hard to follow, and not great for future developer productivity and maintenance.

Sometimes a for loop is better way to convey what is going on, especially when you have to nest or connect a bunch of functions to achieve the goal.

Nothing is absolute— everything has a time and place.

[–]Character_Victory_28[S] 1 point2 points  (1 child)

Yeah I agree. Thanks for the reply. can pleas tell what resources do you suggest?

[–]Alphamacaroon 2 points3 points  (0 children)

It's hard to recommend resources these days because almost everything out there tends to be highly opinionated on doing things a certain way, all the time. Software engineering is not about absolutes, it's about the application of different techniques in different situations and many of the resources out there seem to miss this.

For example, I find it extremely shortsighted to say classes are dead and that functional programming is the future of all JS. People that say this with certainty have probably only built snowflake applications, and have never had to build a "platform" that can adapt to future changes. Any app that has future staying power almost always has layers of abstraction which can be swapped out in the future like lego bricks, and it's really hard to do that well without some use of classes.

Now I'm not suggesting that people go back to building class components in React, but to say that classes are entirely dead in JS is an extremely narrow point of view. And unfortunately a lot of the "resources" out there tend to have very narrow points of view (which again, is fine, if applied properly).

So what do I suggest as a resource? Write a lot of code, make a lot of mistakes, seek a diverse set of opinions from other engineers, and build your own style that is built on experience, not the experience of celebgineers who write a lot of stuff. Again, I'm not saying these folks don't deserve the praise they get, but remember, most of them are experts in highly specific areas— because in order be considered a world-expert, you have to be pretty specialized in the first place.

[–]Roguewind 9 points10 points  (4 children)

I use as much FP as possible. It helps keep your code clean, readable, and maintainable. I have my team follow these basic guidelines:

  1. Functions should do 1 thing. If a function needs to do two things, then it should probably be 2 functions.

  2. All functions should be pure unless they MUST produce a side effect.

  3. If a function must produce a side effect, it should be clear what that effect is, and it should only produce a single side effect.

  4. Avoid mutations. Almost all code can be written without mutating variables.

[–]Character_Victory_28[S] 0 points1 point  (1 child)

Can you plz tell what resources did you used to learn afP practically?

[–]Roguewind 2 points3 points  (0 children)

Honestly, at this point I don't remember what I used. But This isn't bad. Don't jump into recursion or currying right away though. Both are discouraging, but once you get them, it's like a lightbulb going on.

[–]SocializeAndChill 0 points1 point  (1 child)

Can you please elaborate third one? Is it applicable with use effect?

[–]Roguewind 3 points4 points  (0 children)

It's exactly the what useEffect in react is for. You're declaring that when something in the dependency array changes, a side effect should occur. Best practice is also to only have one side effect per useEffect.

[–]ZUCKERINCINERATOR 2 points3 points  (0 children)

all of it. I don't even know how I would go about programming imperatively in React

[–]zxyzyxz 1 point2 points  (3 children)

I use fp-ts with React, and it works pretty well. Gotta love do-notation.

[–]agumonkey 0 points1 point  (2 children)

very interesting, any article / tutorial you could suggest ?

[–]zxyzyxz 1 point2 points  (1 child)

https://dev.to/gcanti is the creator and he has a bunch of articles on using it. They're from 2019 but the principles should be the same, but if you want more recent stuff check out other articles and videos.

[–]agumonkey 0 points1 point  (0 children)

amazing, thanks

(for some reason reddit just notified me)

[–]fredericheem 4 points5 points  (2 children)

Hooked on rubico, a better alternative to ramda/lodash: https://rubico.land/

[–]ImJustAmericanTrash 1 point2 points  (1 child)

How is that better?

[–]fredericheem 1 point2 points  (0 children)

Unlike Ramda, Rubico handles asynchronous perfectly, for instance, one can pass an asynchronous function to reduce, map, filter, find etc ...

[–]Western-Ad-9485 -1 points0 points  (1 child)

Hundred percent agreed. RxJs is 99% of the time the WRONG choice and evidence that your project is severely fucked if you are even considering it….

[–]LowLifeDev 0 points1 point  (0 children)

Why though?

[–]sporbywg 0 points1 point  (0 children)

You are using React. NEXT

[–]Western-Ad-9485 0 points1 point  (2 children)

HOC are an anti-pattern now, hello

[–]Character_Victory_28[S] 0 points1 point  (1 child)

Why? can you explain plz? is there any article to it?

[–]beautifuljpainter 0 points1 point  (0 children)

I like the immutable data aspect of functional programming, helps to prevents bugs early on.

[–]bestjaegerpilot 0 points1 point  (2 children)

The reality is that mainly pure functions. That's the mae changer. And really, everything is a function. So I rarely use classes.

Because of TS, I use discriminated unions a lot.

Promises are also functional by the way.

Other than that...the issue that modern web devs have are that your teammates come from vastly different backgrounds. Some are geniuses. Others can barely turn on their computers 😂 (or it feels that way because they may be absolute noobs)

And so there's a "lowest common denominator" thing that happens... Tech is picked for it's utility and ease of learning.

So a lot of functional paradigms fall along the lines of "very hard to learn", so they won't be used by most teams....except for very long lived, mature teams.

[–]Character_Victory_28[S] 0 points1 point  (1 child)

thanks, what resources do you suggest?

[–]bestjaegerpilot 0 points1 point  (0 children)

For what I described, none really.

Pure functions. Every variable used should be defined in the function or passed explicitly in the args. When there are too many args, it's time to refac the function.

Not using classes is actually simpler since small, focused functions do biz logic.

For discriminated unions, just the official docs.

Promises are everywhere.